我只是想使用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。
答案 0 :(得分:4)
尽管我不知道OP的操作系统,但我认为我已经找到解决问题的方法,至少对于GNU / Linux系统(我无法在其他系统中进行测试)
直接说明
要使用overlay
函数,您不仅需要安装geopandas
,还需要安装rtree
,但是rtree
是C库的包装{ {3}}。因此,要使用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
)。
详细解释
我遇到了同样的错误,并花了很多时间来找出问题所在。这个问题libspatialindex的答案为我提供了有关如何解决问题的提示。
考虑以下代码(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 package
树.
它正在抱怨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
至少对我来说,问题已经解决了。