首页 > 解决方案 > GeoPandas 的过度功能不起作用

问题描述

我只是想用来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'

我正在按照这里的说明进行操作。

标签: pythonpandasgisgeopandas

解决方案


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

直接解释

为了能够使用overlay您需要的功能不仅仅是安装geopandas,您需要安装rtree,但rtree它是 C 库libspatialindex的包装器。因此,要使用rtree库,您需要安装libspatialindexC 库。

要安装libspatialindex开放式终端类型:

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

注意:实际上您只需要sudo apt-get install libspatialindex-dev,但最好更新系统,并且 -y 标志只是为了不停止安装过程以询问是否继续安装。

现在它应该可以解决您的问题。注意:确保您已rtree在系统中安装,您可以使用pip3 freeze(我假设您使用python3)来执行此操作。

长解释

我遇到了同样的错误,并花了很多时间来找出问题所在。这个问题的答案libspatialindex and Rtree on python给了我一个关于如何解决问题的提示。

考虑下面的代码(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,而不是rtree根据安装库requirements.txt,它将显示以下错误消息:

/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,所以正如我的答案的第一部分“直接解释”中所解释的那样,只需运行下面的代码来安装libspatialindex并且script.py应该可以工作。

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

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


推荐阅读