Wednesday, March 8, 2023

Latitude Longitude Distances

Suppose we have the latitude and longitude of two points on the earth's surface, then how can we work out the distances between them?
One approach would be to make the following assumptions:
  • The earth is a sphere.
  • We are interested in the distance along the earth's surface.
  • Altitude can be ignored.
  • The distance from the equator to either pole is 10,000 km.



To calculate the distance we could first find the cartesian coordinates of the two points. We set the origin to be the centre of the earth. For each point on the earth's surface: with latitude \( \phi \) and longitude \( \theta \): \[x = r cos( \phi ) cos ( \theta ) \] \[y = r cos( \phi ) sin ( \theta ) \] \[z = r sin(\phi )\]

If we have two points A and B, with coordinates \( (x_a, y_a, z_a) \) and \( (x_b, y_b, z_b) \)
then we can use a 3 dimensional version of the Pythagorean theorem to determine (d) the straight-line distance between the points using the equation: \[ d^2 = (x_a - x_b)^2 + (y_a - y_b)^2 + (z_a - z_b)^2 \] But that distance is not quite the answer that we are looking for. We seek the distance along the surface of the earth, not the straight-line distance which cuts through the earth.
We let the angle between the points be \( \lambda \), when we know that, we can multiply it by the radius to get the distance along the surface, which is the arc-length.
Looking at the diagram above we can see that: \[sin ( \lambda / 2) = \frac{d/2}{r} \] and so we can evaluate \( \lambda \) using: \[ \lambda = 2 arcsin \left( \frac{d}{2r} \right) \]
Now it is time to try out some numbers:
Inputs
Latitude Longitude
Point A
Point B

Result
Distance km

If you change one of the inputs above and hit enter, then the result will be updated.
The latitude figures are degrees north of the equator. For degrees south, use a negative.
The longitude figures are the degrees west of the prime meridian. For degrees east use a negative number.

If you're interested in the javascript code used to do the calculation, then have a look at this in github.

An alternative approach is to use the Haversine formula.