7

I am trying to use sjoin() to spatially join two geo dataframs and then use both geometries for later spatial calculation, however, I noticed that from the official docs, only one geometry column from either side may be kept.

df= gpd.sjoin(gdf1, gdf2, how='left', op='contains')

Is there a way to manage that?

3
  • Have you considered a union operation?: geopandas.org/docs/user_guide/set_operations.html
    – Aaron
    Commented Apr 9, 2021 at 3:35
  • @Aaron well, I am using the 'contains' operation to join all points from gdf2 fall into the polygons from gdf1. Not sure if and how this will work with union(). Do you maybe have any idea?
    – flgn
    Commented Apr 9, 2021 at 3:48
  • 1
    I think @Aaron was referring to the overlay(), which has a "how" parameter. Reading the examples on that doco page, you might use how='intersection'?
    – Mike Honey
    Commented Apr 11, 2021 at 6:13

1 Answer 1

8

GeoPandas can hold multiple columns with the geometry of which the only one is seen as active. The active geometry is discarded during sjoin.

You can make a copy of your geometry as another column and it that case it will be retained in the joined dataframe.

protected_areas['savedgeom'] = protected_areas.geometry
df = gpd.sjoin(regions, protected_areas, how='left', op='contains')

The resulting df will have savedgeom column.

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.