GeoPandas – How to Find and Remove Overlapping Polygons

geopandasintersectionoverlaypython

I have 2 data frames, let's say, df1 and df2. I need to find df1 polygons that overlay with df2 (the green line), and remove them from main dataframe df1.

I do not need to extract a overlaying part or create a new dataframe, I need to identify the ones which overlays in any cases and remove them. The result would be df1 with the removed polygons.

I tried to use GeoPandas intersect and overlay, however, could not find the solution. Both dataframes have the same coordinate system.

import pandas as pd
import geopandas as gpd

df1 = gpd.read_file(r'E:\...\dfs1.shp')
df2 = gpd.read_file(r'E:\...\dfs2.shp')

print(df1.crs, df1.crs)

res_intersection = gpd.overlay(df1, df2, how='intersection')

TypeError: keep_geom_type does not support None.

enter image description here

Best Answer

You can use spatial join:

import geopandas as gpd
    
df1 = gpd.read_file(r'/home/bera/Desktop/gistemp/greens.shp')
df2 = gpd.read_file(r'/home/bera/Desktop/gistemp/buildings.shp')
df2['savedindex']= df2.index #Save the index values as a new column

intersecting = df1.sjoin(df2, how='inner')['savedindex'] #Find the polygons that intersect. Keep savedindex as a series

df2 = df2[~df2.savedindex.isin(intersecting)] #Filter away these, "savedindex is not in intersecting"

df2.to_file(r'/home/bera/Desktop/gistemp/buildings_disjoint.shp')

enter image description here