[GIS] Spatial join with GeoPandas with two geometry columns

centroidsgeopandaspolygonpythonspatial-join

I have one GeoDataFrame which contains two geometry fields – one is polygon and one is the polygon centroids. The table has also other fields.

I have run spatial join between the gdf and another shapefile of the regions (e.g the shapefile has many polygons of different regions). I have used "op='within' in order to speed it up (and it did):

join=gpd.sjoin(gdf, regions, how='left',op='within')

The problem is that the join didn't work when the polygons touched the borders of the regions' polygons.

For example, here you can see that polygons that are inside specific region have a peach color, the ones outside are green but the ones touching the border are purple:
enter image description here

I believe that if I would use the centroids instead of the full polygons, I could get better results.
Is it possible to tell GeoPandas which geometry column to use?

I have tried to use the set geometry, then do the spatial join and then change it as well:

df.set_geometry('centroids')
join=gpd.sjoin(df, regions, how='left',op='within')
join=join.set_geometry('geometry')

but I still got the same results.

Best Answer

In the end what worked was to use the set_geometry() to the centroids, do the spatial join and then to set_geometry to the original geometry :

df.set_geometry('centroids')
join=gpd.sjoin(df, regions, how='left',predicate='within')
join=join.set_geometry('geometry')