## calculate distances between two points into km
def distances(lat1,lon1,lat2,lon2):
from math import sin, cos, sqrt, atan2, radians
# approximate radius of earth in km
R = 6373.0
# conversion
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
# differences
dlon = lon2 - lon1
dlat = lat2 - lat1
# calculate distances
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c
## Calculate the distance between Lyon and Paris
from haversine import haversine
lyon = (45.7597, 4.8422) # (lat, lon)
paris = (48.8567, 2.3508)
haversine(lyon, paris)
# >> 392.2172595594006 # in kilometers
haversine(lyon, paris, unit='mi')
# >> 243.71201856934454 # in miles
haversine(lyon, paris, unit='nmi')
# >> 211.78037755311516 # in nautical miles