简体   繁体   English

从不同列表中找到两个坐标之间的最小距离

[英]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.

相关问题 查找未排序和排序列表之间的最小距离 - Finding minimal distance between unsorted and sorted lists 计算来自不同行的两个坐标之间的距离 - Compute the distance between two coordinates from different rows 找到两个不同列表的元素之间的距离 - Finding distance between elements of two different list 两个二维数组的坐标,查找来自不同数组的任何坐标是否在彼此的设定距离内(python) - Two 2D array's of coordinates, finding if any of the coordinates from the different arrays are within a set distance of one another (python) 更快的方法来查找坐标之间的距离 - Faster approach to finding distance between coordinates 从列表字典中查找键的最小子集 - Finding a minimal subset of keys from a dictionary of lists 计算两个列表中两点之间的距离 - Calculate distance between two points from two lists 两个列表之间的距离相似度 - Distance similarity between two lists 在Shapely中找到从路线起点开始的坐标距离 - Finding the distance of coordinates from the beginning of a route in Shapely 使用两个迭代列表和&#39;in range (0,len a_list)) 查找点之间的最小距离 - Finding minimum distance between points using two iterative lists and 'in range (0,len a_list))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM