简体   繁体   English

python区域随着多边形性能的增长而增长

[英]python region growing with polygons performance

I'm trying to create regions of polygons on the condition that they touch.我试图在它们接触的条件下创建多边形区域。 In my example I have an example dataset with 382 polygons that need to be grouped together (but the full dataset contains 6355 polygons).在我的示例中,我有一个示例数据集,其中包含 382 个需要组合在一起的多边形(但完整数据集包含 6355 个多边形)。 (I would show a picture, but I don't have enough reputation to do that..) (我会展示一张图片,但我没有足够的声誉来做到这一点..)

I though of doing this brute force, but of course that takes very long and is not very optimal.我虽然这样做了蛮力,但当然这需要很长时间并且不是很理想。

def groupBuildings(blds):
    # blds is a list with shapely polygons
    groups = []
    for bld in blds:
        group = []
        group.append(bld)
        for other in blds:
            for any in group:
                if any != other and any.intersects(other):
                    group.append(other)
        groups.append(group)
    return groups

I learned about region growing and thought that that would be a possible solution, but still the performance is terrible.我了解了区域增长并认为这将是一个可能的解决方案,但性能仍然很糟糕。 I've implemented this in the following way:我已经通过以下方式实现了这一点:

def groupBuildings(blds):
    # blds is a list with shapely polygons
    others = blds
    groups = []
    while blds != []:
        done = []
        group = []
        first = blds.pop(0)
        done.append(first)
        group.append(first)
        for other in others:
            if (other in blds) and first.touches(other):
                group.append(other)
                blds.remove(other)

return groups

But I think the problem here is that I don't have any nearest neighbors, so I still have to iterate over every building twice.但我认为这里的问题是我没有任何最近的邻居,所以我仍然必须对每个建筑物迭代两次。

So my question is: are nearest neighbors essential for region growing?所以我的问题是:最近的邻居对区域增长至关重要吗? Or is there another way of doing this efficiently?或者有没有其他方法可以有效地做到这一点?

You will be best served using shapely.ops.cascaded_union() ( docs here ).最好使用shapely.ops.cascaded_union()此处为文档)。

from shapely.geometry import Point, Polygon, MultiPolygon
from shapely.ops import cascaded_union
import numpy as np
polygons = [Point(200*x,200*y).buffer(b) for x,y,b in np.random.random((6000,3))]
multi = MultiPolygon(polygons)
unioned = cascaded_union(multi)

%%timeit
unioned = cascaded_union(multi) 
# 2.8 seconds for me

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM