Skip to content

Instantly share code, notes, and snippets.

@swilcox
Last active August 29, 2015 14:08
Show Gist options
  • Save swilcox/68f0ffbe063484238ec5 to your computer and use it in GitHub Desktop.
Save swilcox/68f0ffbe063484238ec5 to your computer and use it in GitHub Desktop.
distance between 2 coordinates
import math
def distance(origin, destination):
#assumes origin and destination are (lat, long) pairs where lat and long are decimals
lat1, lon1 = origin
lat2, lon2 = destination
#radius = 6371 # km <-- use this if metric
radius = 3961 # miles <-- use this if english / miles
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 round(d,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment