def haversine(pt_a, pt_b):
"""
`pt_a` and `pt_b` are tuples with two float numbers each, i.e.
representing lng/lat pairs:
(99,100) (-42, 85)
The geo distance between the two points is returned in kilometers
"""
from math import radians, cos, sin, asin, sqrt
lng_a = radians(float(pt_a[0]))
lat_a = radians(float(pt_a[1]))
lng_b = radians(float(pt_b[0]))
lat_b = radians(float(pt_b[1]))
# haversine formula
dlng = lng_b - lng_a
dlat = lat_b - lat_a
whatevs = sin(dlat /2 ) ** 2 + cos(lat_a) * cos(lat_b) * sin(dlng / 2) ** 2
c = 2 * asin(sqrt(whatevs))
r = 6371 # Radius of earth in kilometers.
# return the final calculation
return c * r
评论列表
文章目录