GeoPandas中的过度功能不起作用 [英] Overly Function from GeoPandas Not Working

查看:0
本文介绍了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'

我正在按照说明here操作。

推荐答案

尽管我不知道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,而不按照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,所以正如我在答案的第一部分所解释的,直接解释,只需运行下面的代码安装libspatialindexscript.py应该可以工作。

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

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

这篇关于GeoPandas中的过度功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆