【Python学习】指定两点地理位置经纬度的距离计算


指定两点地理位置经纬度的距离计算

 1 #coding=utf-8
 2 
 3 from math import *
 4 
 5 # input Lat_A 纬度A
 6 # input Lng_A 经度A
 7 # input Lat_B 纬度B
 8 # input Lng_B 经度B
 9 # output distance 距离(km)
10 def calcDistance(Lat_A, Lng_A, Lat_B, Lng_B):
11     ra = 6378.140  # 赤道半径 (km)
12     rb = 6356.755  # 极半径 (km)
13     flatten = (ra - rb) / ra  # 地球扁率
14     rad_lat_A = radians(Lat_A)
15     rad_lng_A = radians(Lng_A)
16     rad_lat_B = radians(Lat_B)
17     rad_lng_B = radians(Lng_B)
18     pA = atan(rb / ra * tan(rad_lat_A))
19     pB = atan(rb / ra * tan(rad_lat_B))
20     xx = acos(sin(pA) * sin(pB) + cos(pA) * cos(pB) * cos(rad_lng_A - rad_lng_B))
21     c1 = (sin(xx) - xx) * (sin(pA) + sin(pB)) ** 2 / cos(xx / 2) ** 2
22     c2 = (sin(xx) + xx) * (sin(pA) - sin(pB)) ** 2 / sin(xx / 2) ** 2
23     dr = flatten / 8 * (c1 - c2)
24     distance = ra * (xx + dr)
25     return distance
26 
27 Lat_A=32.060255; Lng_A=118.796877 # 南京
28 Lat_B=39.904211; Lng_B=116.407395 # 北京
29 distance=calcDistance(Lat_A,Lng_A,Lat_B,Lng_B)
30 print('(Lat_A, Lng_A)=({0:10.3f},{1:10.3f})'.format(Lat_A,Lng_A))
31 print('(Lat_B, Lng_B)=({0:10.3f},{1:10.3f})'.format(Lat_B,Lng_B))
32 print('Distance={0:10.3f} km'.format(distance))

 执行结果:

(Lat_A, Lng_A)=(    32.060,   118.797)
(Lat_B, Lng_B)=(    39.904,   116.407)
Distance=   896.533 km

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM