在Shapely中删除重复的几何 [英] Removing duplicate geometries in Shapely

查看:161
本文介绍了在Shapely中删除重复的几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of Shapely polygons. From that list I want to extract only unique polygons removing the duplicates.

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

How to do it in a faster way? (My list contains thousands of polygons)

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() ,因为它将识别完全重叠的所有多边形作为重复项.改为使用 equals() .

Note that you should not use intersects(), as that will identify any polygons that overlap at all as duplicates. Use equals() instead.

您可以创建一个存储唯一多边形的列表,然后针对列表中的每个多边形,遍历外部列表中存储的多边形,如果它们都不等于新多边形,则将其添加到列表中,您可以为此使用 any() 函数.

You can create a list that stores the unique polygons, and then for each polygon in your list, loop over the polygons stored in the outer list, and if none of them are equal to the new one, add this to the list, you can use any() function for that.

示例:

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)

来自any()文档:

From documentation on any():

如果iterable的任何元素为true,则返回True.如果iterable为空,则返回False.

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

这篇关于在Shapely中删除重复的几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆