2

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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Sep 3, 2020 at 9:47
0

1 Answer 1

5

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')
zabop
3,04617 silver badges44 bronze badges
answered Sep 3, 2020 at 11:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.