• 【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
  • 相关阅读:
    [洛谷P1155] 双栈排序
    [洛谷P4315] 月下”毛景“树
    [洛谷P2486] [SDOI2011]染色
    [HNOI2010] 弾飞绵羊
    mysql注入总结
    cisco交换机实现端口聚合
    python为运维人员打造一个监控脚本
    复习ACCESS注入
    利用sfc文件构建网络渗透
    FTP站点设置
  • 原文地址:https://www.cnblogs.com/yejingcn/p/4537863.html
Copyright © 2020-2023  润新知