Great Circle Distance
-
Hallo,
ich möchte die Distanz zwischen zwei Koordinaten im WGS84 berechen.
in Matlab wirds so gemacht:
function rng = greatcircledist(lat1, lon1, lat2, lon2, r)% Calculate great circle distance between points on a sphere using the % Haversine Formula. LAT1, LON1, LAT2, and LON2 are in radians. RNG is a % length and has the same units as the radius of the sphere, R. (If R is % 1, then RNG is effectively arc length in radians.)
a = sin((lat2-lat1)/2).^2 + cos(lat1) .* cos(lat2) .* sin((lon2-lon1)/2).^2; rng = r * 2 * atan2(sqrt(a),sqrt(1 - a));
Ich dachte ich kann die Formel 1 zu 1 übernehmen, was aber bei mir leider nicht funktionert.
float distance( float lat0, float lon0, float lat1, float lon1 ) const { const float DEG2RAD = 3.14159265f / 180.0f; const float R_EARTH = 3443.918f; lat1 = lat1/3600000.0f; lon1 = lon1/3600000.0f; float dist; float a = pow(sin((lat1-lat0)/2), 2) + cos(lat0) * cos(lat1) * sin((lon1-lon0)/2); dist = r * 2 * atan2(sqrt(a),sqrt(1 - a)); return(dist); }
Beim debuggen hat VS 6.0 für dist den Wert -1.#IND0,also keine Zahl oder undefiniert.
Ersetze ich die Formel durch s.u., ist der Wert falsch:dist = acos( sin(lat0*DEG2RAD)*sin(lat1) + cos(lat0*DEG2RAD)*cos(lat1)*cos((lon1-(lon0*DEG2RAD))) ); // distance in rad
Wert von dist ungleich dem matlab Wert.
Sieht jemand, was an der Formel oben falsch ist?
Ich weis echt nicht mehr weiter, hoffe auf Hilfe!
vg
-
Schau dir mal die Bibliothek proj an. Dort ist die Großkreisrechnung implementiert
http://proj.maptools.org/ Gruß Huck