Skip to content

Instantly share code, notes, and snippets.

@Khalefa
Forked from gopass2002/Haversin.py
Created May 28, 2018 05:20
Show Gist options
  • Save Khalefa/bd3de07aa4186a9c8a1ad6ce674ba6b6 to your computer and use it in GitHub Desktop.
Save Khalefa/bd3de07aa4186a9c8a1ad6ce674ba6b6 to your computer and use it in GitHub Desktop.
Haversine
import math
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
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 d
print distance((37.494685, 126.958941), (37.496055, 126.953834)), 'kilo meters'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment