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?

Vince
  • 20,017
  • 15
  • 45
  • 64
flgn
  • 173
  • 1
  • 4
  • Have you considered a union operation?: https://geopandas.org/docs/user_guide/set_operations.html – Aaron Apr 09 '21 at 03: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 Apr 09 '21 at 03: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 Apr 11 '21 at 06:13

1 Answers1

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.

martinfleis
  • 2,407
  • 11
  • 17