【问题标题】:python region growing with polygons performancepython 区域随着多边形性能的增长而增长
【发布时间】:2017-02-18 10:03:25
【问题描述】:

我正在尝试根据它们接触的条件创建多边形区域。在我的示例中,我有一个示例数据集,其中包含需要组合在一起的 382 个多边形(但完整的数据集包含 6355 个多边形)。 (我会展示一张图片,但我没有足够的声誉来做到这一点..)

我虽然想做这种蛮力,但当然这需要很长时间,而且不是很理想。

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

我了解了区域增长并认为这是一个可能的解决方案,但性能仍然很糟糕。我通过以下方式实现了这一点:

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

但我认为这里的问题是我没有任何最近的邻居,所以我仍然必须对每个建筑物进行两次迭代。

所以我的问题是:最近的邻居对区域增长至关重要吗?还是有其他有效的方法?

【问题讨论】:

    标签: python polygon region shapely


    【解决方案1】:

    最好使用shapely.ops.cascaded_union() (docs here) 为您服务。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2016-04-27
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多