2

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

4

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.

5

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.

  • 1
    Thank you for this comment! Saved me a some debugging time. – n3utrino Aug 7 '18 at 20:59
  • That also saved me some debugging time. – MKJ Jan 5 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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