Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 238
camnesia Avatar asked Jan 30 '20 11:01

camnesia


2 Answers

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.

like image 119
stratophile Avatar answered Nov 16 '22 00:11

stratophile


Ran into this same issue, as mentioned by stratophile I had to install Rtree. Which can be accomplished as follows:

I am using python 3.8 on Windows. I installed the Rtree package within a virtual environment created for my project, using pip to install packages.

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

  2. With the project's virtual environment activated run pip install with the appropriate path to the .whl file that was downloaded in step 1:

pip install C:\Users\...\Rtree-0.9.4-cp38-cp38-win_amd64.whl
  1. I had to restart my interpreter after installation (using JupyterLab)
like image 28
Frank Avatar answered Nov 16 '22 00:11

Frank