从坐标列表中连接附近的坐标

Connecting nearby coordinates from list of coordinates

我有以下经纬度列表。

[(52.5, 12.0), (36.0, -0.5), (50.0, -17.5), (52.0, 13.0), (52.0, 13.0), (50.0, -16.5), (37.5, 1.5), (37.5, 1.5), (46.0, 20.0), (37.5, 1.5), (46.0, 20.0), (50.0, -15.0)]

我只想连接彼此靠近的点。我也想仅在每个点之间的索引差异小于 5 时才连接(例如)。

我最初是在寻找一种仅当绘制的线在一定长度以下时才连接所有点的方法。不确定这在 python 中是否可行?

非常感谢。

这假设索引换行并且 'near' 被定义为 1 个单位。对于列表中的每个元素,对周围的 10 个元素执行距离检查,如果它们彼此在 1 以内,则添加到字典中。

nearby = {}                                                          # This will hold 'connected' elements
for index, element in enumerate(l):                                  # Enumerate through the list l, keeping track of index
    for other in l[index-5: index+5]:                                # For each element 5 before and 5 after
        if sum( (a - b)**2 for a, b in zip(element, other))**.5 < 1 and element != other: 
                                                                     # If other < 5 away from element and other not element
            if element not in nearby.keys():                         # If element isn't already in the dicitonary
                nearby[element] = [other]                            # Add it and reference other
            else:                                                    # Otherwise
                nearby[element].append(other)                        # Add other to the list of nearby elements

如果索引不换行,您可以更改行 for other in l[index-5: index+5]: 以包括对列表开头和结尾的检查。以下是我的做法:

for other in l[index-5 if index-5 > 0 else 0 : index+5 if index+5 < len(l) else len(l) -1]:

这很长,所以您可能想将其分成几行,但它按原样完成了。