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…). 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:
Note haversine script accepts the coordinates in both tuple “()” and list “” form. Additionally, the script can be downloaded from my GitHub located (here).