[英]Finding the minimal distance between two coordinates from different lists
Sorry in advance, currently on mobile!提前抱歉,目前在手机上!
So I basically have one list with around 50,000 lat/long tuples (list 1) and another one with around 1,800 lat/long tuples (list 2).所以我基本上有一个包含大约 50,000 个纬度/经度元组的列表(列表 1)和另一个包含大约 1,800 个纬度/经度元组的列表(列表 2)。
What I want to do is the following: For each of the list elements in list 1, I want to I want to find the closest point out of the list elements in list 2, so that I basically end up with a list of around 50,000 values that represent the minimal distances.我想要做的是:对于列表 1 中的每个列表元素,我想从列表 2 中的列表元素中找到最接近的点,这样我基本上最终得到了一个大约 50,000 个列表表示最小距离的值。
I did not have any issues in calculating the distance for single elements using geopy.distance, however, I am stuck with the for loop implementation and appreciate any help!我在使用 geopy.distance 计算单个元素的距离时没有任何问题,但是,我坚持使用 for 循环实现并感谢任何帮助!
Thanks a lot.非常感谢。
from math import sin, cos, sqrt, atan2
def distanceCheck(lat1, lat2, lon1, lon2):
R = 6373.0
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
return distance
distarr = []
for p1 in list1:
minDist = None
point = None
for p2 in list2:
#DISTANCE CHECK HERE -
check = distanceCheck(p1.lat, p2.lat, p1.lon, p2.lon)
if not minDist:
minDist = check
point = p2
else:
if check < minDist:
minDist = check
point = p2
distarr.append({'min': minDist, 'to': point, 'from': p1})
print("{}".format(distarr))
list1 and list2 are the lists with lat and lon. list1 和 list2 是带有 lat 和 lon 的列表。 Hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.