I'm trying to calculate the distance between two geodetic points in two different ways. The points are:

A:(41.466138, 15.547839)
B:(41.467216, 15.547025)

The distance between the two points is pretty small (about 130/140 meters)

1) I have used the Haversine forumla that using this site gives me the following result:

enter image description here

So the mesured distance using the Haversine formula is 137,7 meters.

2) I have tried to calculate the distance transformating the geodetic coordinates (latitude, longitude) in cartesian coordinates (x, y). I have calculated the (x,y) coordinates for A and B:

R =  6371; //radius of the earth
X = R * Math.cos(lat) * Math.cos(lon); // lon and lat are in radiants (which is not the same as provided above, must multiply by Pi / 180)
Y = R * Math.cos(lat) * Math.sin(lon);

So I obtained:

A: (4599,392641683213, 1279,6610297820203)
B: (4599,334350798723, 1279,574411410788)

NOTE: It assumes that the altitude is negligible in A and B.

Now that I have the cartesian coordinates I can calculate the distance using Pitagora's theorem, so:

|Xb - Xa| = 4599,392641683213 - 4599,334350798723 = 0,05829 Km
|Yb - Ya| = 1279,6610297820203 - 1279,574411410788 = 0,08662 Km

distanceAB = sqrt(0,05829^2 + 0,08662^2) = 0,1044 Km = 104,4 m

As you can see the distance calculated with the above methods generate different values:

137,7 m for with the former, 104,4 m with the latter.

Why this happen?

  • $\begingroup$ Note that your X/Y equations above are the same (both are cos & cos), but your computed values are not, so you probably have some errors there. $\endgroup$ – Mark Ping Jan 13 '14 at 17:02
  • $\begingroup$ Calculate angle $\theta$ between vectors. Geodesic is $\left\vert\,R\theta\,\right\vert$. $\endgroup$ – Felix Marin Feb 16 '14 at 23:17

The Haversine formula gives the "as-the-crow-flies" distance, i.e., the great circle distance along the surface of the earth. If you take the Euclidean distance between two points in $\mathbb{R}^3$, you are finding the straight-line distance, which will cut through the earth. That being said, if the points are close together, the results should be very similar: the discrepancy in your case is due to another mistake you made, which is ignoring the $z$-coordinate in the Euclidean calculation. The $z$-coordinate doesn't correspond to the altitude; it's the altitude at the North Pole, but at the equator it's the north-south direction. Using $$ \begin{eqnarray} x &=& R\cos\theta\cos\phi \\ y &=& R\cos\theta\sin\phi \\ z &=& R\sin\theta, \end{eqnarray} $$ you find $$ \begin{eqnarray} (x_A,y_A,z_A) &=& (4599.39264, 1279.66103, 4218.73156) \\ (x_B,y_B,z_B) &=& (4599.33435, 1279.57441, 4218.82138); \end{eqnarray} $$ and the Euclidean distance between them is indeed $137.7$m.

| cite | improve this answer | |
  • $\begingroup$ You are right! Thank you :) $\endgroup$ – GVillani82 Jan 13 '14 at 17:05
  • $\begingroup$ Could you help me if i'd like to get the distance with a height? So I have coordinates with the data of height in that point. If I use this formula, but add the height to the R in the 3rd (z), will that be OK? I mean z = (R+height)*sin theta. $\endgroup$ – androbin Mar 30 '17 at 7:49
  • 1
    $\begingroup$ @androbin: you would replace $R$ with $R+h$ in all three coordinates. $\endgroup$ – mjqxxxx Mar 30 '17 at 14:09

You say two "geodetic points" which might imply that you are interested in calculating the shortest distance on the surface of the WGS84 ellipsoid. This link solves this geodesic problem giving an answer of 137.689 m. The link also gives a reference to a paper describing how this calculation is done.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.