# Calculating the Distance Between Two GPS Coordinates with Python (Haversine Formula)

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…). This can lead to errors of up to 0.5%. But for the type of work I have been doing recently, that’s just fine. More accurate methods exist. 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).