在WGS84坐标系下,计算两点(经纬度坐标)之间的距离(单位:km)。
import math
def LLs2Dist(lat1, lon1, lat2, lon2):
R = 6371
dLat = (lat2 - lat1) * math.pi / 180.0
dLon = (lon2 - lon1) * math.pi / 180.0
a = math.sin(dLat / 2) * math.sin(dLat/2) + math.cos(lat1 * math.pi / 180.0) * math.cos(lat2 * math.pi / 180.0) * math.sin(dLon/2) * math.sin(dLon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
dist = R * c
return dist
验证:
x1 = 37.779388
y1 = -122.423246
x2 = 32.719464
y2 = -117.220406
dist = LLs2Dist(y1, x1, y2, x2)
print dist
输出结果为:
642.185478152