Created
June 6, 2012 17:43
-
-
Save rochacbruno/2883505 to your computer and use it in GitHub Desktop.
Calculate distance between latitude longitude pairs with Python
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
#!/usr/bin/env python | |
# Haversine formula example in Python | |
# Author: Wayne Dyck | |
import math | |
def distance(origin, destination): | |
lat1, lon1 = origin | |
lat2, lon2 = destination | |
radius = 6371 # km | |
dlat = math.radians(lat2-lat1) | |
dlon = math.radians(lon2-lon1) | |
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \ | |
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2) | |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) | |
d = radius * c | |
return d |
Thanks for this, can anyone explain what are 'a' and 'c' assignments , can we have better names for these variables ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the eloquent and easily understandable code!