在使用 shapely 的 unary_union 合并两个区域时出现这个奇怪的错误。
Shapely 版本:1.6.4.post2
python 3.5
数据
多边形(并排)
我想添加 Gujranwala 1 和 Gujranwala 2 使其成为一个多边形。
代码
from shapely.ops import unary_union
polygons = [dfff['geometry'][1:2], dfff['geometry'][2:3]]
boundary = unary_union(polygons)
输出
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-ee1f09532724> in <module>()
1 from shapely.ops import unary_union
2 polygons = [dfff['geometry'][1:2], dfff['geometry'][2:3]]
----> 3 boundary = unary_union(polygons)
~/.local/lib/python3.5/site-packages/shapely/ops.py in unary_union(self, geoms)
145 subs = (c_void_p * L)()
146 for i, g in enumerate(geoms):
--> 147 subs[i] = g._geom
148 collection = lgeos.GEOSGeom_createCollection(6, subs, L)
149 return geom_factory(lgeos.methods['unary_union'](collection))
~/.local/lib/python3.5/site-packages/pandas/core/generic.py in __getattr__(self, name)
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):
4375 return self[name]
-> 4376 return object.__getattribute__(self, name)
4377
4378 def __setattr__(self, name, value):
AttributeError: 'GeoSeries' object has no attribute '_geom'
最佳答案
您对一元联合的尝试 split 了两种工作方式之间的差异。您尝试选择两个多边形的方式(dfff["geometry"][1:2]
和 dfff["geometry"][2:3]
)实际上返回一对 GeoSeries
(其中包含一些 shapely
几何序列),因此您向 unary_union
传递一个 GeoSeries 列表
,而 shapely
中的 unary_union
函数需要一个 shapely
几何体列表。你可以这样做:
polygons = [dfff.iloc[1, "geometry"], dfff.iloc[2, "geometry"]]
boundary = unary_union(polygons)
也就是说,GeoSeries
提供了它们自己的 unary_union
方法,该方法仅调用 shapely.ops.unary_union
,但在 GeoSeries 上执行此操作
对象。因此,获得一元联合的更简单方法是:
boundary = dfff["geometry"][1:3].unary_union
这也更容易扩展到更长的多边形列表。
关于python - 属性错误 : 'GeoSeries' object has no attribute '_geom' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52131547/