AttributeError:'GeoSeries'对象没有属性'_geom'

问题描述 投票:3回答:1

使用shapely的unary_union合并两个区域时出现这个奇怪的错误。

匀称版本: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'
python gis shapely geopandas
1个回答
2
投票

你试图使一元联合分裂两种方法之间的差异。您尝试选择两个多边形(dfff["geometry"][1:2]dfff["geometry"][2:3])的方式实际上返回一对GeoSeries(其中包含一些shapely几何序列),因此您将unary_union传递给GeoSeries列表,而unary_union中的shapely函数是期待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

这也更容易扩展到更长的多边形列表。

© www.soinside.com 2019 - 2024. All rights reserved.