Last active
August 29, 2015 14:11
-
-
Save pythonizame/01305ff7672075e17d97 to your computer and use it in GitHub Desktop.
Calcular distancia (Km's) entre 2 Geopuntos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Función que nos permite calcular la distancia entre 2 geo puntos | |
""" | |
import math | |
# Punto 1 | |
lat1 = 20.694462 | |
lon1 = -102.656250 | |
# Point 2 | |
lat2 = 31.742183 | |
lon2 = -115.839844 | |
def calculate_distance(lat1, lon1, lat2, lon2): | |
if ((lat1 == lat2) and (lon1 == lon2)): | |
return 0 | |
try: | |
delta = lon2 - lon1 | |
a = math.radians(lat1) | |
b = math.radians(lat2) | |
C = math.radians(delta) | |
x = math.sin(a) * math.sin(b) + math.cos(a) * math.cos(b) * math.cos(C) | |
distance = math.acos(x) # in radians | |
distance = math.degrees(distance) # in degrees | |
distance = distance * 60 # 60 nautical miles / lat degree | |
distance = (distance * 1852) / 1000 # conversion to KM's | |
distance = round(distance) | |
return distance; | |
except: | |
return 0 | |
distance = calculate_distance(lat1,lon1,lat1,lon2) | |
print "La distancia es: {0} Kms".format(distance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment