I have a Polygon of transects that contain overlapping features where they converge. I need to erase where they overlap, but still keep one of the polygons there (preferably the preceding one).
Essentially, I want to iterate through each feature, have the current feature (f1) see where it overlaps with all other features (for loop, f2) erase that overlap from f2 and assign its new geometry as that.
I've used this code within the QGIS Python Console which works, but I'm developing a notebook to automate the generation of transcripts, and this is the final task. Here is the code I use within Q:
for f1 in layer.getFeatures():
for f2 in layer2.getFeatures():
if f1.id() < f2.id():
geom1 = f1.geometry()
geom2 = f2.geometry()
new_geom = geom2.difference(geom1)
layer2.dataProvider().changeGeometryValues({f2.id(): new_geom})
I found a chunk of code which gets me a shapefile of every overlapping area, now I need to erase that geometry, but only from the succeeding feature (f2) not f1. Here is the code.
g1 = gpd.GeoDataFrame.from_file(linesBufferFolder + "\\Lines_Buffer")
g1.shape
import itertools
geoms = g1['geometry'].tolist()
intersection_iter = gpd.GeoDataFrame(gpd.GeoSeries([poly[0].intersection(poly[1]) for poly in itertools.combinations(geoms, 2) if poly[0].intersects(poly[1])]), columns=['geometry'])
intersection_iter.to_file(linesBufferFolder + "\\Lines_int_iter.shp")
I'm assuming I need something in that for loop to tell it to erase while it is there?
Above is the end result. The highlighted polygon comes after the one overlapping it on its right, therefore the polygon to the right remains the same, and the highlighted on is now the shape you see. If I highlight the one to the left of the currently highlighted, it would also show no overlap. All of these polygons are square 1m buffers.