10

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'

2 Answers 2

10

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
  • 1
    Thank you! Installing rtree (which had problems on its own) was what fixed it for me!
    – M.O.
    Feb 2, 2020 at 1:55
  • To install it on mac with conda: | conda install -c conda-forge libspatialindex=1.9.3 | pip install Rtree
    – SeF
    Jul 13, 2020 at 18:52
1

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)
2
  • Did you install it from JupyterLab terminal? I tried the exact same thing but it first WARNED me that ..."looks like a filename, but the file does not exist" and afterwards an ERROR: C:Users...Rtree‑0.9.4‑cp39‑cp39‑win_amd64.whl is not a valid wheel filename.
    – KayEss
    May 26, 2020 at 7:25
  • You will have to install packages for your python environment using the command prompt (on windows) or terminal (mac & linux). Also note that at step 2 you have to change the path to lead to where you have saved the wheel file that you downloaded in step 1.
    – Frank
    May 27, 2020 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.