Created
April 19, 2012 20:32
-
-
Save signed0/2423962 to your computer and use it in GitHub Desktop.
Haversine Distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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