地理数据处理库(geopandas):sjoin 报错“NoneType”对象没有“intersection”属性。

10
我试图用两个开源数据集进行空间连接。但是我遇到了一个“AttributeError: 'NoneType' object has no attribute 'intersection'”错误。类似的错误似乎已经通过确保空几何体被移除而得以解决,但这并没有帮助。我还安装了spatialindex-1.9.3和rtree-0.9.3。我正在使用Python 3.8。

我正在尝试对两个开源数据集进行空间连接。但是出现了“AttributeError: 'NoneType' object has no attribute 'intersection'”错误。一些相似的错误已经被修复,方法是确保清除空几何物体,但这并没有解决我的问题。我已经安装了spatialindex-1.9.3和rtree-0.9.3。我使用的是Python 3.8。

import geopandas as gpd
import pandas as pd
from shapely.geometry import Point

# Import LSOA polygon data
LSOA_polygons = gpd.read_file('https://raw.githubusercontent.com/gausie/LSOA- 2011-GeoJSON/master/lsoa.geojson')

# Remove any empty geometry
LSOA_polygons = LSOA_polygons[LSOA_polygons.geometry.type == 'Polygon']

# UK charge point data
url_charge_points = 'http://chargepoints.dft.gov.uk/api/retrieve/registry/format/csv'
charge_points = pd.read_csv(url_charge_points, lineterminator='\n', low_memory=False, usecols=[0,3,4])

# Create a geometry column 
geometry = [Point(xy) for xy in zip(charge_points['longitude'], charge_points['latitude'])]

# Coordinate reference system : WGS84
crs = {'init': 'epsg:4326'}

# Create a Geographic data frame 
charge_points = gpd.GeoDataFrame(charge_points, crs=crs, geometry=geometry)

# Remove any empty geometry
charge_points = charge_points[charge_points.geometry.type == 'Point']

# Execute spatial join
charge_points_LSOA = gpd.sjoin(charge_points, LSOA_polygons, how="inner", op='intersects')
我收到的错误信息是:


AttributeError                            Traceback (most recent call last)

<ipython-input-13-d724e30179d7> in <module>
     12 
     13 # Execute spatial join
---> 14 charge_points_LSOA = gpd.sjoin(charge_points, LSOA_polygons, how="inner", op='intersects')
     15 
     16 charge_points_LSOA = (charge_points_LSOA >>

~/PycharmProjects/Desirability/venv/lib/python3.8/site-packages/geopandas/tools/sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)
    106     # get rtree spatial index
    107     if tree_idx_right:
--> 108         idxmatch = left_df.geometry.apply(lambda x: x.bounds).apply(
    109             lambda x: list(tree_idx.intersection(x)) if not x == () else []
    110         )

~/PycharmProjects/Desirability/venv/lib/python3.8/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4043             else:
   4044                 values = self.astype(object).values
-> 4045                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4046 
   4047         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

~/PycharmProjects/Desirability/venv/lib/python3.8/site-packages/geopandas/tools/sjoin.py in <lambda>(x)
    107     if tree_idx_right:
    108         idxmatch = left_df.geometry.apply(lambda x: x.bounds).apply(
--> 109             lambda x: list(tree_idx.intersection(x)) if not x == () else []
    110         )
    111         idxmatch = idxmatch[idxmatch.apply(len) > 0]

AttributeError: 'NoneType' object has no attribute 'intersection'
2个回答

10

我遇到了完全相同的错误信息。在跟踪sjoin代码后,我发现我没有安装rtree……:( 安装后,错误消失了。

不过很遗憾,我无法亲自测试你的代码,因为多边形数据的URL返回404。


1
谢谢!安装 rtree(它本身就有问题)是解决我的问题的关键! - M.O.
在Mac上使用conda安装:| conda install -c conda-forge libspatialindex=1.9.3 | pip install Rtree - SeF

1
我遇到了同样的问题,正如stratophile所提到的,我需要安装Rtree。 可以按照以下步骤完成:
我在Windows上使用Python 3.8。 我在为我的项目创建的虚拟环境中安装了Rtree软件包,并使用pip安装软件包。
1. 从以下链接下载正确Python版本和系统的.whl文件:https://www.lfd.uci.edu/~gohlke/pythonlibs/#rtree 2. 使用项目的虚拟环境运行pip install并指定适当的路径到第1步中下载的.whl文件。
pip install C:\Users\...\Rtree-0.9.4-cp38-cp38-win_amd64.whl

我在安装后(使用JupyterLab)不得不重新启动我的解释器。

你是从JupyterLab终端安装的吗?我尝试了完全相同的操作,但首先出现了警告:“看起来像文件名,但文件不存在”,然后出现了错误:C:Users...Rtree‑0.9.4‑cp39‑cp39‑win_amd64.whl不是有效的wheel文件名。 - KayEss
您需要使用命令提示符(Windows)或终端(Mac和Linux)安装Python环境的软件包。此外,请注意,在第2步中,您必须更改路径以导向您在第1步中下载的wheel文件所保存的位置。 - Frank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接