【问题标题】:Calculate the distance between two coordinates with Python [closed]用Python计算两个坐标之间的距离[关闭]
【发布时间】:2017-11-28 07:28:10
【问题描述】:

我有一张地图,他们在其中找到了几个点(纬度/经度),并想知道它们之间存在的距离。

那么,给定一组纬度/经度坐标,我如何在 python 中计算它们之间的距离?

【问题讨论】:

标签: python distance haversine


【解决方案1】:

我曾经写过this答案的python版本。它详细介绍了使用Haversine formula 计算以公里为单位的距离。

import math

def get_distance(lat_1, lng_1, lat_2, lng_2): 
    d_lat = lat_2 - lat_1
    d_lng = lng_2 - lng_1 

    temp = (  
         math.sin(d_lat / 2) ** 2 
       + math.cos(lat_1) 
       * math.cos(lat_2) 
       * math.sin(d_lng / 2) ** 2
    )

    return 6373.0 * (2 * math.atan2(math.sqrt(temp), math.sqrt(1 - temp)))

确保传递给函数的坐标以弧度为单位。如果它们是度数,您可以先转换它们:

lng_1, lat_1, lng_2, lat_2 = map(math.radians, [lng_1, lat_1, lng_2, lat_2])

【讨论】:

  • 输出以米为单位?这对我不起作用。
  • 输出以公里为单位,如果需要米,则添加 *1000 以返回
  • 必须先转换成弧度:lon_1, lat_1, lon_2, lat_2 = map(math.radians, [lon_1, lat_1, lon_2, lat_2])
  • @pepece 我隐含地假设坐标是弧度,因为这就是我为自己编写函数的方式。在答案中添加了注释,感谢指出!
猜你喜欢
  • 2020-02-12
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多