【问题标题】:Distance between list of points based on latitude/longitude基于纬度/经度的点列表之间的距离
【发布时间】:2019-11-29 13:55:52
【问题描述】:

所以我有这个坐标列表,我需要它们之间距离的最终总和。

track = [[49.16967, 20.21491, 1343],
        [49.17066, 20.22002, 1373],
        [49.16979, 20.22416, 1408],
        [49.17077, 20.22186, 1422],
        [49.17258, 20.22094, 1467],
        [49.17294, 20.21944, 1460]]

到目前为止,我已经有了计算两组坐标之间距离的基本公式

import math
def distance(lat_start, lon_start, lat_ciel, lon_ciel):
    R = 6371000
    lat_start = math.radians(lat_start)
    lon_start = math.radians(lon_start)
    lat_ciel = math.radians(lat_ciel)
    lon_ciel = math.radians(lon_ciel)
    DiffLat = lat_ciel - lat_start
    DiffLon = lon_ciel - lon_start
    a = math.sin(DiffLat/2) ** 2 + math.cos(lat_start) * math.cos(lat_ciel) * math.sin(DiffLon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return R * c

我被困在下一步,我尝试创建一个不同的函数,它使用现有函数作为距离,只需获取每组坐标并计算距离,然后将结果数字相加。
感谢您的帮助。

【问题讨论】:

  • 显示你到目前为止的代码并解释它的问题。
  • 您的问题是生成所有点对以及如何求和点对之间的距离?

标签: python list gps coordinates


【解决方案1】:
import math
from itertools import combinations

def distance(lat_start, lon_start, lat_ciel, lon_ciel):
    R = 6371000
    lat_start = math.radians(lat_start)
    lon_start = math.radians(lon_start)
    lat_ciel = math.radians(lat_ciel)
    lon_ciel = math.radians(lon_ciel)
    DiffLat = lat_ciel - lat_start
    DiffLon = lon_ciel - lon_start
    a = math.sin(DiffLat/2) ** 2 + math.cos(lat_start) * math.cos(lat_ciel) * math.sin(DiffLon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return R * c

def sum_distance(track):
  return sum((map(lambda p: distance(*p[0][:2], *p[1][:2]), combinations(track, 2))))

my_track = [[49.16967, 20.21491, 1343],
        [49.17066, 20.22002, 1373],
        [49.16979, 20.22416, 1408],
        [49.17077, 20.22186, 1422],
        [49.17258, 20.22094, 1467],
        [49.17294, 20.21944, 1460]]

print(sum_distance(my_track))   # 5252.0327870706005

说明

  1. 组合(...)来自 https://docs.python.org/2/library/itertools.html#itertools.combinations 提供所有对的组合
  2. lambda p: distance(*p[0][:2], *p[1][:2]) 计算距离 对,其中 p[0] 和 p[1] 是一对的第一个和第二个元素
  3. [:2] 是获取前两个元素(即纬度/经度)的切片
  4. *p[x][:2] 为距离函数的参数提供前两个元素的解包
  5. map(...) 为所有对生成距离
  6. sum(...) 对距离求和

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 2012-10-13
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多