geopandas self intersection grouping

Viewed 287

I am used to R, and am switching to python for some geo transformations. For one task, I need to group polygons in a geopandas dataframe by intersection.

However, I can only find geopandas arguments that can do this between two dataframes. For example, these two polygons in the dataframe:

enter image description here

should get the same group id.

I hope someone can help!

1 Answers

If you are not interested in strictly pair-wise relation, you can use libpysal to get group labels.

Starting from dummy data:

import geopandas as gpd

geom = gpd.points_from_xy([0, 1, 3, 4, 6, 7], [0, 1, 3, 4, 6, 7]).buffer(1.2)
gdf = gpd.GeoDataFrame(geometry=geom)

input data

You can create fuzzy contiguity weights matrix based on intersection and get labels of connected components:

W = libpysal.weights.fuzzy_contiguity(gdf)
W.component_labels

which returns an array:

array([0, 0, 1, 1, 2, 2], dtype=int32)

Plotted:

gdf.plot(W.component_labels, alpha=.5, categorical=True)

final

Related