Skip to content

Instantly share code, notes, and snippets.

@vgauthier
Created October 31, 2012 12:37
Show Gist options
  • Save vgauthier/3986827 to your computer and use it in GitHub Desktop.
Save vgauthier/3986827 to your computer and use it in GitHub Desktop.
Python: Distance with Spherical Coordinates
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