geopandas: sjoin 'NoneType' object has no attribute 'intersection'

拜拜、爱过 提交于 2020-05-15 02:26:08

问题


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')

The error I get:


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'

回答1:


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.

I wasn't able to test your code myself, though – the URL for the polygon data returns 404.




回答2:


Ran into this same issue.

I am using python 3.8 on windows, within a virtual environment and using pip to manage my packages.

This worked for me:

  1. Download the right .whl file for your python interpreter & system from: https://www.lfd.uci.edu/~gohlke/pythonlibs/#rtree

  2. Within the appropriate environment run pip install with the appropriate path to the .whl file:

pip install C:\Users\..\Rtree-0.9.4-cp38-cp38-win_amd64.whl
  1. I had to restart my interpreter after installation (using JupyterLab)

Also I restarted the PC for good measure (not sure if necessary) before trying the code that produced the error initially.



来源:https://stackoverflow.com/questions/59985197/geopandas-sjoin-nonetype-object-has-no-attribute-intersection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!