【问题标题】:Merging Polygon Shapefiles in Python在 Python 中合并多边形形状文件
【发布时间】:2020-07-16 23:56:15
【问题描述】:

我的 python3 脚本创建了变量 geometries_list,其值为 shapefile 列表,每个多边形代表一个地理区域

[<shapefile.Shape at 0x7f060abfae48>,
 <shapefile.Shape at 0x7f05dcaf1cc0>,
 <shapefile.Shape at 0x7f060a86b278>,
 <shapefile.Shape at 0x7f05da470668>]

我想“合并”多边形。我尝试了以下代码

from functools import reduce
from shapely.geometry import Polygon
union = reduce(lambda x,y: x.union(y), geometries_list) 

但它给出了结果: AttributeError: 'Shape' 对象没有属性 'union'

我看到另一种方法涉及创建 shapefilewriter 对象并连续覆盖列表中的每个多边形https://gis.stackexchange.com/questions/103033/using-pyshp-to-merge-two-shapefiles 这种方法可能有效,但每次覆盖都会保存到磁盘

【问题讨论】:

  • 以下解决了问题 geometries_list = [geometry.Polygon(x.points) for x in geometries_list]

标签: python polygon shapely


【解决方案1】:

也许值得一提的是shapely.ops.unary_union是一种更有效的合并形状的方法。

您可以通过它们的 GeoJSON 表示将 shapefile.Shape 对象转换为 shapely.Polygon 对象,然后将它们合并如下。

from shapely.geometry import shape
from shapely.geometry.ops import unary_union

union = unary_union([shape(s.__geo_interface__) for s in geometries_list])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多