- Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathhaversine.py
17 lines (15 loc) · 568 Bytes
/
haversine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
frommathimportradians, cos, sin, asin, sqrt
defhaversine(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))
r=6371# Radius of earth in kilometers. Use 3956 for miles
returnc*r