Created
October 31, 2012 12:37
-
-
Save vgauthier/3986827 to your computer and use it in GitHub Desktop.
Python: Distance with Spherical Coordinates
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
def distance(lat1,lon1,lat2,lon2): | |
import math | |
# Earth Radius in KM | |
R = 6371 | |
dLat = math.radians(lat2-lat1); | |
dLon = math.radians(lon2-lon1); | |
lat1 = math.radians(lat1); | |
lat2 = math.radians(lat2); | |
a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2) | |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) | |
d = R * c | |
return d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment