Skip to content

Instantly share code, notes, and snippets.

@utsengar
Created March 23, 2014 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utsengar/9719720 to your computer and use it in GitHub Desktop.
Save utsengar/9719720 to your computer and use it in GitHub Desktop.
Find distance between two lat/lon
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# 6367 km is the radius of the Earth
km = 6367 * c
return km
print haversine(-121.929017, 37.298950, -121.916919, 37.335763)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment