在 Shapely 中删除重复的几何图形

Removing duplicate geometries in Shapely

我有一个形状多边形的列表。从该列表中,我只想提取唯一的多边形,删除重复项。

如何以更快的方式做到这一点? (我的列表包含数千个多边形)

from shapely.geometry import Polygon

lists = [[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)], [(1,1),(2,2),(3,3),(4,4)]]
polys = [Polygon(item) for item in lists] ##This is given condition

for poly in polys:

    test = [p.intersects(poly) for p in polys] ##Return true or false
    print test


[True, False, True]
[False, True, False]
[True, False, True]

预期结果是:

[[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)]]

请注意,您不应使用 intersects(), as that will identify any polygons that overlap at all as duplicates. Use equals()

您可以创建一个存储唯一多边形的列表,然后对于列表中的每个多边形,循环存储在外部列表中的多边形,如果其中 none 个等于新多边形,将其添加到列表中,您可以为此使用 any() 函数。

示例:

from shapely.geometry import Polygon

lists = [[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)], [(1,1),(2,2),(3,3),(4,4)]]
polys = [Polygon(item) for item in lists] ##This is given condition
uniqpolies = []
for poly in polys:

    if not any(p.equals(poly) for p in uniqpolies):
        uniqpolies.append(poly)

来自documentation on any()

Return True if any element of the iterable is true. If the iterable is empty, return False.