问题描述
我正在尝试使用两个开源数据集进行空间连接.我遇到了AttributeError: 'NoneType' object has no attribute 'intersection'
错误.通过确保删除空的几何图形,似乎已经解决了类似的错误,但这似乎无济于事.我还安装了spacespaceindex-1.9.3和rtree-0.9.3.我正在使用Python 3.8
I am trying to do a spatial join with two open source datasets. I am running into an AttributeError: 'NoneType' object has no attribute 'intersection'
error. Similar errors have seem to have been solved by ensuring empty geometry is removed but this does not seem to help. I also have spatialindex-1.9.3 and rtree-0.9.3 installed. I am using 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'
推荐答案
我有相同的错误消息.跟踪了sjoin的代码后,我发现我没有安装rtree ... :-(安装后,错误消失了.
I had the identical error message. After tracing through sjoin's code I spotted that I didn't have rtree installed... :-( After installing it the error went away.
但是,我自己无法测试您的代码-多边形数据的URL返回404.
I wasn't able to test your code myself, though – the URL for the polygon data returns 404.
这篇关于geopandas:sjoin'NoneType'对象没有属性'intersection'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!