5

My layer contains overlapping polygons, as seen with some transparency.

enter image description here

What could I do to remove the overlapping parts (in dark on the screenshot) without merging any polygon in order to obtain the result opposite?

enter image description here

2
  • 4
    it's an indeterminate problem, how do you choose which part of the overlay to keep?
    – pigreco
    Commented Feb 19, 2022 at 17:45
  • Hello, Thank you for your propositions @Kadir Şahbaz and . My goal was to remove the overlaps while keeping the same number of objects. I found a solution inspired by the solution of MrXsquared (gis.stackexchange.com/questions/381780/…). This solution works well when two objects overlap 2 by 2. To generalize the result, I added a 'gath' field in which I placed the identifier of the 1st 'id' with the expression: regexp_substr( "ID",'[^.]'), then just do a grouping on the 'gath' field.
    – fcka
    Commented Feb 20, 2022 at 20:47

2 Answers 2

5

If I understood you correctly, all you need to is an intersect of the poylgon layer with itself, and in the resulting attribute table you will have the intersections of polygons with all other overlapping polygons (if any):

BEFORE:
enter image description here

AFTER:

enter image description here

Attribute table (AFTER):

enter image description here

Then in the attribute table, all you need to do is a Select features using an expression and enter the following expression (assuming your input has an id field):

"id" = "id_2"

That will select only the non-overlapping parts of each polygon which you can then export to its own layer.

5

You can use the following script if it is not important which polygon part to delete. (after selecting the layer)

layer = iface.activeLayer()
dpr = layer.dataProvider()

# iterate over the layer
for feature1 in layer.getFeatures():

    # iterate over the same layer
    for feature2 in layer.getFeatures():
        
 
        if feature1.id() < feature2.id():
            geometry1 = feature1.geometry()
            geometry2 = feature2.geometry()
            diff_geometry = geometry2.difference(geometry1)
            dpr.changeGeometryValues({feature2.id(): diff_geometry})

Before:

enter image description here

After:

enter image description here

5
  • 1
    I would also write your script for another case, for area comparison of features : if feature1.geometry().area() < feature2.geometry().area()
    – Taras
    Commented Feb 20, 2022 at 8:58
  • 1
    @Taras Thank you. Comparison of areas is more reasonable. I also thought checking drawing order of both features. Commented Feb 20, 2022 at 10:55
  • 1
    May I extend your answer?
    – Taras
    Commented Feb 20, 2022 at 12:25
  • 1
    @Taras of course. you can add your answer if you desire. Commented Feb 20, 2022 at 12:59
  • Hello, Thank you for your propositions @Kadir Şahbaz and . My goal was to remove the overlaps while keeping the same number of objects. I found a solution inspired by the solution of MrXsquared (gis.stackexchange.com/questions/381780/…). This solution works well when two objects overlap 2 by 2. To generalize the result, I added a 'gath' field in which I placed the identifier of the 1st 'id' with the expression: regexp_substr( "ID",'[^.]'), then just do a grouping on the 'gath' field.
    – fcka
    Commented Feb 20, 2022 at 20:48

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