1

Points

I have a GeoDataFrame made of polygons- some overlapping, some not (see picture above). I'd like to create a shapefile or GeoPackage of only the polygon centroids that are not overlapping. There is a very clear way of how to do this in QGIS here, but I'd like to complete this in Python.

This is what I have tried thus far:

combined = point_gdf.overlay(point_gdf, how="union")
valid_buffers = combined.drop_duplicates()
return valid_buffers

How do you select the non-overlapping polygons from a single GeoDataFrame and create a new GeoPackage from that selection in Python?

1

1 Answer 1

1

Dissolve, create a cluster column, join this to the data frame, count, extract the single polygons:

import geopandas as gpd
df = gpd.read_file(r"C:\TEMP\random_polygons.shp")
diss = df.dissolve() #Dissolve into one big multipolygon
singleparts = gpd.GeoDataFrame(geometry=diss.apply(lambda x: [part for part in x.geometry.geoms], axis=1).explode(), crs=df.crs) #Multipart to singleparts
singleparts["cluster"] = range(singleparts.shape[0]) #Calculate a cluster/row number column

joined = gpd.sjoin(left_df=df, right_df=singleparts) #Join the cluster numbers to the df

#Count how many times the cluster number occurs. If more than once, there are other polygons overlapping current
joined = joined.merge(joined.groupby("cluster")["cluster"].count().rename("polycount"), left_on="cluster", right_index=True)

joined = joined.loc[joined["polycount"]==1] #Extract polygons with only one occurence

joined.to_file(r"C:\TEMP\random_polygons_SINGLES.shp")

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.