TL;DR - By making a few geometric assumptions, the Haversine formula provies an exceptionally simple way of calculating distance between two latitude/longitude pairs. This tutorial will show you how to implement your own version in Python.

A lot of my posts recently have focused on the analysis of spacial data coming from either the GPS on my phone (collected through Strava) or geo-tagged tweets. Because of this I ended up writing my own Python module for calculating the distance between two latitude/longitude pairs. This is accomplished using the Haversine formula. While more accurate methods exist for calculating the distance between two points on earths surface, the Haversine formula and Python implementation couldn’t be any simpler. Below is a breakdown of the Haversine formula.

Haversine:

$$a=\sin^2\left(\frac{\Delta\phi}{2}\right) +\cos(\phi_1)\cdot\cos(\phi_2)\cdot\sin^2\left(\frac{\Delta\lambda}{2}\right)$$

With:

$$c=2\cdot arctan2\left(\sqrt{a},\sqrt{1-a}\right)$$

and:

$$d=R\cdot c$$

Where:

$$\phi$$ = latitude
$$\lambda$$ = longitude
$$R$$ = 6371 (Earth’s mean radius in km)

Much of Haverines’ simplicity comes from the underlying assumption that Earth is a perfect sphere (which it isn’t…). Because of this, it can lead to errors of up to 0.5%. More accurate methods exist but at the expense of computational complexity. Below is the Python implementation:

Example usage:

Note haversine script accepts the coordinates in both tuple “()” and list “[]” form. Additionally, the script can be downloaded from my GitHub located (here).