13

I posted this question on stackoverflow here, but it seems that gis.stackexchange is the more appropriate platform.

I am trying to merge two geodataframes in geopandas (want to see which polygon each point is in).

The following code gets me an error ("RTreeError: Coordinates must not have minimums more than maximums").

I cannot find any explanation as to what this means and how to solve it. Any ideas?

import geopandas as gpd
from shapely.geometry import Point, mapping,shape
from geopandas import GeoDataFrame, read_file
#from geopandas.tools import overlay
from geopandas.tools import sjoin

print('Reading points...')
points=pd.read_csv(points_csv)
points['geometry'] = points.apply(lambda z: Point(z.Latitude, z.Longitude), axis=1)
PointsGeodataframe = gpd.GeoDataFrame(points)
print PointsGeodataframe.head()
print('Reading polygons...')
PolygonsGeodataframe = gpd.GeoDataFrame.from_file(china_shapefile+".shp")
print PolygonsGeodataframe.head()
print('Merging GeoDataframes...')
merged=sjoin(PointsGeodataframe, PolygonsGeodataframe, how='left', op='intersects')

#merged = PointsGeodataframe.merge(PolygonsGeodataframe, left_on='iso_alpha2', right_on='ISO2', how='left')
print(merged.head(5))

Link to data for reproduction: Shapefile, GPS points

3 Answers 3

19

For the benefit of anyone else searching for this error message, one thing that can cause it is attempting to do a spatial join where the point coordinates contain nulls.

5
  • 2
    Thank you for this comment! Saved me a some debugging time.
    – n3utrino
    Commented Aug 7, 2018 at 20:59
  • 1
    That also saved me some debugging time.
    – MKJ
    Commented Jan 5, 2019 at 19:05
  • I came back here twice in as many weeks because of this. Shapely will allow Point(nan,nan) without complaints; make sure you check for / drop any such values and avoid this issue. Commented Jul 14, 2020 at 13:47
  • Hero saved me some debugging time.
    – JG_RS_GIS
    Commented Nov 6, 2020 at 14:03
  • Just adding to the list of comments saying that you saved me some debugging time
    – Ric S
    Commented Jan 18, 2021 at 16:08
7

There are many problems in your data (sample_map_data.csv) and in your script.

1) the sample_map_data.csv file contains 6 valid lines and 730 lines with ''

import pandas as pd  
points = pd.read_csv("sample_map_data.csv")
points.shape
(735, 3)

Resulting in the error "RTreeError: Coordinates must not have minimums more than maximums"

The right result should be .

points = pd.read_csv("sample_map_data.csv",nrows= 5)
points.shape
(5, 3)
print(points)
Latitude   Longitude  Heat
0  23.124700  113.282776   100
1  22.618574  113.999634    80
2  23.694332  113.049316    70
3  23.809973  114.735718    90
4  21.815098  110.961914    80)

2) In Shapely, a Point is defined by Point(x,y) and not Point(y,x) so

from shapely.geometry import Point
points['geometry'] = points.apply(lambda z: Point(z.Longitude, z.Latitude), axis=1)
import geopandas as gpd
PointsGeodataframe = gpd.GeoDataFrame(points)
print(PointsGeodataframe)
Latitude      Longitude    Heat         geometry
0  23.124700  113.282776   100  POINT (113.282776 23.1247)
1  22.618574  113.999634    80  POINT (113.999634 22.618574)
2  23.694332  113.049316    70  POINT (113.049316 23.694332)
3  23.809973  114.735718    90  POINT (114.735718 23.809973)
4  21.815098  110.961914    80  POINT (110.961914 21.815098)

3) For Points in Polygons, look at More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc): no need of op='intersects'

PolygonsGeodataframe = gpd.GeoDataFrame.from_file("CHN_adm1.shp")
PointsGeodataframe.crs = PolygonsGeodataframe.crs #same crs for the two layers
from geopandas.tools import sjoin
pointInPolys = sjoin(PointsGeodataframe, PolygonsGeodataframe, how='left') 
print(pointInPolys.head(5))

and it works.

4

To check if there are Point(nan, nan):

gdf.is_valid

To clean them up:

gdf = gdf[gdf.is_valid]

Your Answer

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

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