def earth_distance(lat1, lon1, lat2, lon2):
""" Distance in meters between two points specified in degrees. """
x1 = calc_rad(lat1) * math.cos(degree_to_radian(lon1)) * math.sin(degree_to_radian(90-lat1))
x2 = calc_rad(lat2) * math.cos(degree_to_radian(lon2)) * math.sin(degree_to_radian(90-lat2))
y1 = calc_rad(lat1) * math.sin(degree_to_radian(lon1)) * math.sin(degree_to_radian(90-lat1))
y2 = calc_rad(lat2) * math.sin(degree_to_radian(lon2)) * math.sin(degree_to_radian(90-lat2))
z1 = calc_rad(lat1) * math.cos(degree_to_radian(90-lat1))
z2 = calc_rad(lat2) * math.cos(degree_to_radian(90-lat2))
a = (x1*x2 + y1*y2 + z1*z2)/pow(calc_rad((lat1+lat2)/2), 2)
# a should be in [1, -1] but can sometimes fall outside it by
# a very small amount due to rounding errors in the preceding
# calculations (this is prone to happen when the argument points
# are very close together). Thus we constrain it here.
if abs(a) > 1:
a = 1
elif a < -1:
a = -1
return calc_rad((lat1+lat2) / 2) * math.acos(a)
评论列表
文章目录