如何根据两个坐标之间的距离附加列表?

时间:2019-02-05 13:42:53

标签: python loops append geopy

我试图根据两个纬度/经度坐标之间的距离附加一个数字列表。如果我有纬度和经度列表,以及点和它们各自的纬度和经度的压缩数组,我该如何编写一个循环以添加两个坐标之间的距离小于500的点?我尝试了以下方法,但仍会出错。有什么建议吗?

import geopy.distance

latH = [25.5, 29.0, 27.2, 26.0, 30.1, 25.7, 26.3, 35.5] 
lonH = [-84.3, -85.0, -85.6, -90.3, -89.0, -89.7, -90.5, -86.2]

rain = [([29816.0, 24387, 33155.0, 32062.0, 31251.0],26.1915, -80.4492), ([30954.0, 31251.0, 32366.0, 32062.0] 26.1297, -81.7625)]
rain2 = []
for j in range(len(rain)):
    appendres = []
    for k in range(len(rain[j][0]):
        for i in range(len(latH)):
            if geopy.distance.distance((rain[j][1],rain[j][2]),(latH[i],lonH[i])).km <= 500.0:
                appendres.append(rain[k])
    rain2.append(appendres)

1 个答案:

答案 0 :(得分:1)

那适合你吗?

from geopy.distance import distance

latH = [25.5, 29.0, 27.2, 26.0, 30.1, 25.7, 26.3, 35.5] 
lonH = [-84.3, -85.0, -85.6, -90.3, -89.0, -89.7, -90.5, -86.2]

rain = [([29816.0, 24387, 33155.0, 32062.0, 31251.0], 26.1915, -80.4492), ([30954.0, 31251.0, 32366.0, 32062.0], 26.1297, -81.7625)]
rain2 = []

#   Since all your arrays are the same length...
for lat, lon in zip(latH, lonH):
    arr = []
    for data, lat_2, lon_2 in rain:
        if distance((lat, lon), (lat_2, lon_2)).km < 500:
            arr.append(data)
    rain2.append(arr)

print(rain2)
[[[29816.0, 24387, 33155.0, 32062.0, 31251.0], [30954.0, 31251.0, 32366.0, 32062.0]], [[30954.0, 31251.0, 32366.0, 32062.0]], [[30954.0, 31251.0, 32366.0, 32062.0]], [], [], [], [], []]

我编写了与您的行为类似的代码,没有出现所有语法问题,并且对我的意图更明确。但是由于我们无法启动您的代码,并且您也没有提供所需的输出...我不确定。