Reputation: 497
I am trying to append a list of numbers based on the distance between two lat/lon coordinates. If I have lists of lat and lon, and a zipped array of the points and their respective lat & lon, how do I write a loop to append the points where the distance between both coordinates is less than 500? I tried the way below, but keep getting errors. Any suggestions?
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)
Upvotes: 0
Views: 494
Reputation: 3306
Does that suits you ?
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]], [], [], [], [], []]
I wrote a code that is similar in behaviour to yours, without all the syntaxic problems and with clearer intentions to me. But as we can't launch your code and you don't provide a wanted output... I cannot be sure.
Upvotes: 1