I have 3 dataframes: df1
(large polygons); df2
(small polygons, which overlays with large polygons) and df3
(points).
How do I exclude large polygons in which points do not overlay with the small polygons?
Below is an example of two polygons (purple) that overlays with the small polygons, together with some points in and outside them.
Polygon No. 1: points overlay with each small polygon within a large one, meaning this large polygon is not excluded.
Polygon No.2: none of the points overlay with a small polygon within a large one, meaning that this large polygon should be excluded.
In short, I need to identify large polygons in which points DO NOT overlay with small polygons and remove them. The result would be df1 with the removed polygons.
So, there is a solution with GeoPandas SpatialJoin using two dataframes. In this case, if small polygons do not overlay with large polygons, the particular polygon from df1
is excluded. But how can this be done using another data frame?
import geopandas as gpd
df1 = gpd.read_file(r'/home/../large_poly.shp')
df2 = gpd.read_file(r'/home/../small_poly.shp')
df3 = gpd.read_file(r'/home/../points.shp')
df1['savedindex']= df1.index #Save the index values as a new column
intersecting = df2.sjoin(df1, how='inner')['savedindex']
df1 = df1[df1.savedindex.isin(intersecting)]