Skip to content

Instantly share code, notes, and snippets.

@mayroncachina
Created September 29, 2014 23:24
Show Gist options
  • Save mayroncachina/9c8d79df50315ca68a32 to your computer and use it in GitHub Desktop.
Save mayroncachina/9c8d79df50315ca68a32 to your computer and use it in GitHub Desktop.
Haversine formula - Exemplo de calculo de distância
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
#EXEMPLO
>>> seattle = [47.621800, -122.350326]
>>> olympia = [47.041917, -122.893766]
>>> distance(seattle, olympia)
76.386615799548693
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment