3

My aim is to find the 'non-overlap' between two GeoPandas DataFrame. In other words the result should exclude the intersection between gdf1 and gdf2.

I am aware that I can iterate through the geometries as follows:

data=[]
for index, orig in gdf1.iterrows():
    for index2, ref in gdf2.iterrows():
        if ref['geometry'].symmetric_difference(orig['geometry']):
               data.append({'geometry':ref['geometry'].symmetric_difference(orig['geometry'])})     

df = gpd.GeoDataFrame(data,columns=['geometry'])

However, I would like to know if there is a way of doing the symmetric difference using gdp.sjoin

Here is the code for intersection:

gis = gpd.sjoin(gdf1, gdf2, how='left', predicate='intersects')

I am looking for the code for the symmetric difference.

The following link does not specify how to do it with the symmetric difference.

https://geopandas.org/en/stable/docs/reference/api/geopandas.sjoin.html

2
  • 1
    You want to erase/difference away the intersecting parts or only find the polygons that dont intersect=
    – BERA
    May 23, 2023 at 8:05
  • 1
    I am not interested in polygons that intersect. I am looking to delete them and only focus on polygons where there is no intersection. I have a series of polygons and I would like to keep the polygons that show no intersection
    – bravopapa
    May 23, 2023 at 8:08

1 Answer 1

5

You can inner join the dataframes, then select the rows that are not in the join.

Find the features in df that are not intersecting df2:

import geopandas as gpd

df = gpd.read_file(r"C:/GIS/data/Bakgrundskartor/Topografi_250/data/mark_sverige.gpkg", layer="sankmark")
df["tempid"] = range(0, df.shape[0]) #Create an id column

df2 = gpd.read_file(r"C:/GIS/data/Bakgrundskartor/Topografi_250/data/naturvard_sverige.gpkg", layer="skyddadnatur")

joined = gpd.sjoin(left_df=df, right_df=df2, how="inner", predicate="intersects") #Find the intersecting polygons

nonintersecting = df.loc[~df["tempid"].isin(joined["tempid"])] #Select the ids in df, that are not in the spatial join output

enter image description here

0

Your Answer

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

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