Skip to content

Instantly share code, notes, and snippets.

@darthlukan
Created August 4, 2014 12:01
Show Gist options
  • Save darthlukan/4114cec7ddf328bed422 to your computer and use it in GitHub Desktop.
Save darthlukan/4114cec7ddf328bed422 to your computer and use it in GitHub Desktop.
Haversine formula for getting the distance between two points given latitudes and longitudes (formats {lat: float, lon: float}).
from math import radians, cos, sin, asin, sqrt
def haversine(alat, alon, blat, blon):
R = 6371 # Earth's radius in KM
alat, alon, blat, blon = map(radians, [alat, alon, blat, blon])
clon = blon - alon
clat = blat - alat
a = (sin(clat / 2)) ** 2 + cos(alat) * cos(blat) * (sin(clon / 2)) ** 2
c = 2 * asin(sqrt(a))
distance = (R * c)
return distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment