3

I have a Shapefile consists of multiple polygons, where I want to erase the overlap based on the id. An example to visualize the problem:

import geopandas as gpd
from shapely.geometry import Polygon

polygon1 = Polygon([[7, 0], [8, 1], [8, 4], [6, 4], [6, 1]])
polygon2 = Polygon([[7, 3], [8, 4], [8, 7], [6, 7], [6, 4]])
polygon3 = Polygon([[7, 6], [8, 7], [8, 9], [6, 9], [6, 7]])

polygondata = gpd.GeoDataFrame()
polygondata['geometry'] = None

polygondata.loc[0, 'geometry'] = polygon1
polygondata.loc[1, 'geometry'] = polygon2
polygondata.loc[2, 'geometry'] = polygon3

This codes gives following output visible on the left side (Exported to QGIS)

enter image description here enter image description here

Because the overlay is created systematically in one direction i want to erase every polygon from each other based on the id. The code would be:

erase i from i+1 --> erase 0 from 1, erase 1 from 2 ...

and should give result look like in picture 2 (right). In Python I have the data loaded into geopandas geodataframe, but I only know how to perform geometrical operations between geodataframes and not single features in it. I would split the data in single geometries put it in a list and perform the calculation based on a loop, but I'm not sure how it works. Is there maybe a solution in geopandas how to perform this process without splitting the data?

I'm using Windows 10 and Python 3.7

2

You don't need a loop. Use shift in difference operation. (I used df instead of polygondata for legibility)

df['geometry'] = df['geometry'].difference(df['geometry'].shift(1))
df.loc[0, 'geometry'] = polygon1

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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