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

September 7, 2016

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.

\( \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).