Skip to content

Instantly share code, notes, and snippets.

@signed0
Created April 19, 2012 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save signed0/2423962 to your computer and use it in GitHub Desktop.
Save signed0/2423962 to your computer and use it in GitHub Desktop.
Haversine Distance
'''See http://en.wikipedia.org/wiki/Haversine_formula for more details'''
from math import sin, cos, sqrt, asin, radians
RADIUS_EARTH_M = 6371200.0 #The Earth's mean radius in meters
def haversine_distance(a, b):
'''Operates on two lat,lngs and returns the distance in meters'''
lng1, lat1 = map(radians, a) #lng, lat to radians
lng2, lat2 = map(radians, b) #lng, lat to radians
sin_dlat_over_2 = sin((lat2 - lat1) / 2.0)
sin_dlng_over_2 = sin((lng2 - lng1) / 2.0)
f = sin_dlat_over_2 * sin_dlat_over_2
f += cos(lat1) * cos(lat2) * sin_dlng_over_2 * sin_dlng_over_2
return RADIUS_EARTH_M * 2.0 * asin(sqrt(f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment