質問の緯度経度であらわされる2点の間の地点を求めるプログラムをJavaで作ってみました。(^_^;
実行結果のr=0.5のとこと値を比べてみると、あってるみたいです。o(^-^)o
0.5 : 54°21′44″, -4°31′50″
Midpoint: 54°21′44″N, 004°31′50″W
※参考URL
http://www.movable-type.co.uk/scripts/latlong.html
Haversine formula:
● Haversine1.java
/* * Haversine1.java */ import static java.lang.Math.*; class Haversine1 { static final int R = 6371; // km public static void main(String[] args) { double lat1 = dmsToDec(50.0, 3.0, 59.0); double lon1 = -dmsToDec( 5.0, 42.0, 53.0); double lat2 = dmsToDec(58.0, 38.0, 38.0); double lon2 = -dmsToDec( 3.0, 4.0, 12.0); double d = getDistance(lat1,lon1,lat2,lon2); double b = getDirection(lat1,lon1,lat2,lon2); System.out.printf("%4.1f [km]\n",d); System.out.println(decToDms(b)); System.out.println(); for(int i=0; i<= 10; i++){ double r = 0.1*i; System.out.printf(" %2.1f : %s, %s\n",r, decToDms(getLatitude(lat1, r*d, b)), decToDms(getLongitude(lat1, lon1, r*d, b))); } } public static double getDistance(double lat1, double lon1, double lat2, double lon2) { double dLat = toRadians(lat2-lat1); double dLon = toRadians(lon2-lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); double a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2); double c = 2 * atan2(sqrt(a), sqrt(1-a)); double distance = R * c; return distance; } public static double getDirection(double lat1, double lon1, double lat2, double lon2) { lat1 = toRadians(lat1); lat2 = toRadians(lat2); lon1 = toRadians(lon1); lon2 = toRadians(lon2); double dLon = lon2 - lon1; double y = sin(dLon); double x = cos(lat1)*tan(lat2)-sin(lat1)*cos(dLon); double brng = toDegrees(atan2(y, x)); return (brng>=0.0) ? brng : brng + 360.0; } public static double getLatitude(double lat1, double d, double brng) { lat1 = toRadians(lat1); brng = toRadians(brng); double lat2 = asin( sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(brng) ); return toDegrees(lat2); } public static double getLongitude(double lat1, double lon1, double d, double brng) { lat1 = toRadians(lat1); lon1 = toRadians(lon1); brng = toRadians(brng); double lat2 = asin( sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(brng) ); double lon2 = lon1 + atan2(sin(brng)*sin(d/R)*cos(lat1), cos(d/R)-sin(lat1)*sin(lat2)); return toDegrees(lon2); } public static double dmsToDec(double h, double m, double s) { return (h + m / 60.0 + s / 3600.0); } public static String decToDms(double x) { String sign = " "; double hh, mm, ss; if(x< 0){ sign="-"; x*=-1; } ss = x; hh = floor(ss); ss = (ss - hh) * 60.0; mm = floor(ss); ss = (ss - mm) * 60.0; return String.format("%s%.0f°%02.0f′%02.0f″",sign,hh,mm,ss); } }
●実行結果
968.9 [km] 9° 7′11″ 0.0 : 50°03′59″, -5°42′53″ 0.1 : 50°55′35″, -5°29′44″ 0.2 : 51°47′10″, -5°16′06″ 0.3 : 52°38′43″, -5°01′56″ 0.4 : 53°30′15″, -4°47′11″ 0.5 : 54°21′44″, -4°31′50″ 0.6 : 55°13′12″, -4°15′50″ 0.7 : 56°04′37″, -3°59′08″ 0.8 : 56°55′60″, -3°41′40″ 0.9 : 57°47′20″, -3°23′23″ 1.0 : 58°38′38″, -3°04′12″
- 作者: 中山清喬,国本大悟
- 出版社/メーカー: インプレス
- 発売日: 2011/10/07
- メディア: 単行本(ソフトカバー)
- 購入: 6人 クリック: 28回
- この商品を含むブログ (24件) を見る
- 作者: 川場隆
- 出版社/メーカー: 秀和システム
- 発売日: 2009/10/23
- メディア: 単行本
- 購入: 14人 クリック: 162回
- この商品を含むブログ (33件) を見る