python - 属性错误 : 'GeoSeries' object has no attribute '_geom'

标签 python gis shapely geopandas

在使用 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/

相关文章:

python - 两个日期之间的日期数

python - 我正在使用 pandas 从数据框中提取两列,但一列成为索引,然后在尝试访问该列时出现关键错误

python - 如何使用Python获取Excel工作簿的路径

r - 使用 ggmap/ggplot2 在 map 上按组绘制等高线

python - 使用 Python 的 matplotlib/basemap 的颜色状态

python - 对 scipy.sparse.csr_matrix 中的行求和

python - 基于集中度结合 GeoPandas Dataframe 和 Pandas Dataframe

python - geopandas:如何 "normalize"geoseries

python - 关闭多边形的算法

python - 使用 Geopandas 时,模块 'shapely' 没有属性 'geometry'