Created
August 4, 2014 12:01
-
-
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}).
This file contains 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
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