Great-circle distance using haversine formula

1.4k Views Asked by At

I've been trying to calculate the distance between two locations following the haversine formula. I believe the formula is:

  • $\Delta_\mathrm{lon}=\mathrm{lon}_1-\mathrm{lon}_2$
  • $\Delta_\mathrm{lat}=\mathrm{lat}_1-\mathrm{lat}_2$
  • $a=\sin^2\dfrac{\Delta\mathrm{lat}}{2}+\cos\mathrm{lat}_1\cos\mathrm{lat}_2\sin^2\dfrac{\Delta_\mathrm{lon}}{2}$
  • $c=2\mathrm{atan2}(\sqrt{a},\sqrt{1-a})$
  • $d=Rc$

With $R$ the radius of the Earth and $d$ the distance between two points given by their longitude and latitude.

However, I'm stuck with $\sqrt{1-a}$.

Can anyone please help? Any help is much appreciated. Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Here is an example: distance at mean sea level from Santiago de Chile to Prague. Coordinates from Wikipedia. Earth radius taken as 6,371 km.

  • Santiago de Chile: 33°27'S 70°40'W
  • Prague: 50°05'N 14°25'E

First, convert coordinates to decimal degrees, then radians. Take care of the signs (east longitude and north latitude are positive).

Degrees (lat/lon pairs):

  • Santiago de Chile: -33.45, -70.67
  • Prague: +50.08, +14.42

Radians (lat/lon pairs):

  • Santiago de Chile: -0.5838, -1.2334
  • Prague: +0.8741, +0.2516

Now simply apply the following formula, where $\varphi$ stands for latitude and $\lambda$ longitude.

$$d=2R\arcsin\sqrt{\sin^2\dfrac{\Delta\varphi}{2}+\cos\varphi_1\cos\varphi_2\sin^2\dfrac{\Delta\lambda}{2}}$$

Here $\Delta\varphi=1.4579$ and $\Delta\lambda=1.4850$.

The expression under the radical, that you call $a$ in your question, equals roughly $0.6884$. The distance $d\simeq12,469 \;\mathrm{km}$.

Seems close enough to the distance computed by Google.

Now try to redo this computation with your own tools. If you have a handheld calculator, it's more than enough.


Remark: while the formula with atan2(y,x) is useless, it's not wrong either. You can easily check that for $0<a<1$, $\mathrm{atan2}(\sqrt{a},\sqrt{1-a})=\arcsin\sqrt{a}$. So you should find the same result with this formula.


While we are at it, here is how to derive the haversine formula, using the vector dot product. The points are given by their spherical coordinate $\varphi$ (latitude) and $\lambda$ (longitude). The cartesian coordinates are thus given by

  • $x=R\cos\varphi\cos\lambda$
  • $y=R\cos\varphi\sin\lambda$
  • $z=R\sin\varphi$

The two vectors are $\overrightarrow{OM_1}$ and $\overrightarrow{OM_2}$, with $O$ being the sphere center.

$$\overrightarrow{OM_1}\cdot\overrightarrow{OM_2}=||\overrightarrow{OM_1}||\cdot||\overrightarrow{OM_2}||\cdot\cos(\overrightarrow{OM_1},\overrightarrow{OM_2})$$

In cartesian coordinates, the dot product can be written

$$R^2\cos(\overrightarrow{OM_1},\overrightarrow{OM_2})=R^2\cos\varphi_1\cos\varphi_2\cos\lambda_1\cos\lambda_2\\+R^2\cos\varphi_1\cos\varphi_2\sin\lambda_1\sin\lambda_2+R^2\sin\varphi_1\sin\varphi_2$$

The first two terms can be simplified using the formula $\cos(a-b)=\cos a\cos b+\sin a\sin b$. The factor $R^2$ can also be removed as it appears on both sides. We will also denote the angle $(\overrightarrow{OM_1},\overrightarrow{OM_2})$ by $\theta$. Thus:

$$\cos\theta=\cos\varphi_1\cos\varphi_2\cos(\lambda_1-\lambda_2)+\sin\varphi_1\sin\varphi_2$$

It could be used as is, but when the distance is small (a common case), the cosine is very close to $1$, where it's flat. So taking the arccosine will not be very accurate. To transform the formula, use the relation $\cos a=1-2\sin^2\frac a2$.

$$1-2\sin^2\frac\theta2=\cos\varphi_1\cos\varphi_2\left(1-2\sin^2\dfrac{\lambda_1-\lambda_2}{2}\right)+\sin\varphi_1\sin\varphi_2$$

$$1-2\sin^2\frac\theta2=\cos(\varphi_1-\varphi_2) -2\cos\varphi_1\cos\varphi_2\sin^2\dfrac{\lambda_1-\lambda_2}{2}$$

$$1-2\sin^2\frac\theta2=1-2\sin^2\frac{\varphi_1-\varphi_2}2 -2\cos\varphi_1\cos\varphi_2\sin^2\dfrac{\lambda_1-\lambda_2}{2}$$

And finally

$$\sin^2\frac\theta2=\sin^2\frac{\varphi_1-\varphi_2}2 +\cos\varphi_1\cos\varphi_2\sin^2\dfrac{\lambda_1-\lambda_2}{2}$$

Now, we can compute the angle $\theta$, and the distance is $d=R\theta$.

So

$$d=2R\arcsin\sqrt{\sin^2\frac{\varphi_1-\varphi_2}2 +\cos\varphi_1\cos\varphi_2\sin^2\dfrac{\lambda_1-\lambda_2}{2}}$$

(it's ok to take the arcsine because the angle $\theta/2$ is necessarily in $[0,\pi/2]$)


A last note about terminology. Why haversine? In the old days, special names were given to some trigonometric functions that are seldom used nowadays. The versine is defined by $\mathrm{versin}\,a=1-\cos a$. And the haversine is "half the versine", or

$$\mathrm{hav}\,a=\frac{1-\cos a}{2}=\sin^2\frac{a}{2}$$

The formula written above with squares of sines can be written more concisely with the haversine:

$$\mathrm{hav}\,\theta=\mathrm{hav}\,(\varphi_1-\varphi_2) +\cos\varphi_1\cos\varphi_2\mathrm{hav}\,(\lambda_1-\lambda_2)$$

Apart from conciseness, there is another advantage. In the old days, there were no electronic calculator and computations were made with tables. Squaring the sine of half the angle means more calculations than necessary. Thus, for navigation and surveying purposes, there were also tables of versines or haversines, further simplifying the computations.