GeoPandas 的过度功能不起作用

Overly Function from GeoPandas Not Working

我只是想使用 geopandas 来获得两个多边形区域的并集和交集。我定义:

import geopandas as gpd
from shapely.geometry import Polygon
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
                                  Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
                                  Polygon([(3,3), (5,3), (5,5), (3,5)])])

df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})

我尝试以下方法来获取 union

res_union = gpd.overlay(df1, df2, how='union')

失败并出现以下错误:

AttributeError: 'NoneType' object has no attribute 'intersection'

我正在按照说明进行操作 here

尽管我不知道 OP 的操作系统,但我认为我找到了解决问题的方法,至少对于 GNU/Linux 系统(我无法在其他系统中进行测试)。

直接解释

要使用 overlay 功能,您需要的不仅仅是安装 geopandas,您还需要安装 rtree,但 rtree 是 C 的包装器图书馆 libspatialindex。所以要使用 rtree 库你需要安装 libspatialindex C 库。

要安装 libspatialindex 打开终端类型:

sudo apt-get update && apt-get install -y libspatialindex-dev

注意:实际上你只需要 sudo apt-get install libspatialindex-dev,但最好更新系统,-y 标志只是为了不停止安装过程以要求继续安装或没有。

现在应该可以解决你的问题了。注意:确保你的系统中安装了 rtree,你可以使用 pip3 freeze(我假设你使用 python3)。

详细解释

我遇到了同样的错误,并且花了很多时间来找出问题所在。这个问题的答案给我一个解决问题的提示。

考虑以下代码(OP 的代码示例)并命名为 script.py:

import geopandas as gpd
from shapely.geometry import Polygon


polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
                        Polygon([(2,2), (4,2), (4,4), (2,4)])])

polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
                        Polygon([(3,3), (5,3), (5,5), (3,5)])])

df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})

df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})

res_union = gpd.overlay(df1, df2, how='union')

考虑以下 requirements.txt

Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2

如果你尝试只安装requirements.txt和运行scrip.py中的库,而不安装requirements.txt中的rtree库,它将显示以下错误消息:

/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing package `rtree`.
  warn("Cannot generate spatial index: Missing package `rtree`.")
Traceback (most recent call last):
  File "script.py", line 17, in <module>
    res_union = gpd.overlay(df1, df2, how='union')
  File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 371, in overlay
    result = _overlay_union(df1, df2)
  File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 298, in _overlay_union
    dfinter = _overlay_intersection(df1, df2)
  File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 212, in _overlay_intersection
    sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
  File "/usr/local/lib/python3.6/site-packages/pandas/core/series.py", line 3194, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
  File "/usr/local/lib/python3.6/site-packages/geopandas/tools/overlay.py", line 212, in <lambda>
    sidx = bbox.apply(lambda x: list(spatial_index.intersection(x)))
AttributeError: 'NoneType' object has no attribute 'intersection'

错误信息的最后一行

AttributeError: 'NoneType' object has no attribute 'intersection'

用处不大。但是如果你看第一行:

/usr/local/lib/python3.6/site-packages/geopandas/base.py:76: UserWarning: Cannot generate spatial index: Missing packagertree.

它在抱怨 rtree 库。

所以让我们安装 rtree 看看会发生什么。 requirements.txt 现在更新为:

Shapely==1.6.4.post2
descartes==1.1.0
geopandas==0.4.0
matplotlib==3.0.2
rtree==0.8.3

再次运行 script.py 我收到以下错误:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    import geopandas as gpd
  File "/usr/local/lib/python3.6/site-packages/geopandas/__init__.py", line 1, in <module>
    from geopandas.geoseries import GeoSeries
  File "/usr/local/lib/python3.6/site-packages/geopandas/geoseries.py", line 12, in <module>
    from geopandas.base import GeoPandasBase, _series_unary_op, _CoordinateIndexer
  File "/usr/local/lib/python3.6/site-packages/geopandas/base.py", line 14, in <module>
    from rtree.core import RTreeError
  File "/usr/local/lib/python3.6/site-packages/rtree/__init__.py", line 1, in <module>
    from .index import Rtree
  File "/usr/local/lib/python3.6/site-packages/rtree/index.py", line 5, in <module>
    from . import core
  File "/usr/local/lib/python3.6/site-packages/rtree/core.py", line 125, in <module>
    raise OSError("Could not find libspatialindex_c library file")
OSError: Could not find libspatialindex_c library file

最后一行抱怨 libspatialindex_c,所以正如我在回答的第一部分所解释的那样,"Direct explanation",只需 运行 下面的代码来安装 libspatialindex script.py 应该可以工作。

sudo apt-get update && apt-get install -y libspatialindex-dev

至少对我来说问题解决了。

Geopandas 文档讲述了以下内容:

由于历史原因,overlay 方法也可以作为顶层函数 overlay() 使用。建议使用该方法,因为该功能将来可能会被弃用.

出于这个原因,我会这样做:

df1.union(df2)