Can I spatially join on a column other than geometry on Python Geopandas?

Viewed 585

In a GeoDataFrame I have a column with the geometry of the polygons (Polygon objects), and another one with Point objects, representing the centroid of the polygons. Now, what I want to do is to be able to join spatially on the centroid column, for example, instead of doing

test = gpd.sjoin(M, Gsq, op="within")

I could do something like

test = gpd.sjoin(M["centroid"], Gsq, op="within")

So far, sjoin works if I overwrite the complete geometry field with the centroid, but that's too ugly. So, any way I can join on a given column?

1 Answers

According to the official documentation of the geopandas.sjoin command, there is no option to select a different column as the geometry for the operation.

However, you can have two columns with geometry data in your geodataframe (for example geometry_pg for the polygon geometry and geometry_pt for the centroid point geometry. Then you can set the "active" geometry of the geodataframe to one or the other column. That way you can do the spatial join without discarding any data. It's easy to identify what geometry is active at any moment too. For how to do it, just check the official documentation of the geodataframe, it's clear and it has a simple example.

Hope it helps.

Related