5

Is there a way to do a 1:1 difference between two polygon layers based on a similar attribute? The layers have the same number of features with a shared attribute. I've tried the native QGIS difference algorithm but it subtracts from the features of the first layer, the dissolved/merged area of the second layer.

Edit: Added photo showing sample layers.

Two vector layers

2
  • You could try a union and find the bits that don't have both IDs although I'm not 100% clear on what you're trying to do here, can you expand your description a little bit and if possible draw a picture of what you're hoping to achieve. Jan 18, 2019 at 4:35
  • Hi @MichaelStimson, thanks for responding to my new question. What I want to achieve is subtract the blue layer from the red layer, but only do so on a per feature basis. And I have a shared attribute to identify which layer feature to subtract from another layer feature. The native difference algorithm takes the blue layer, dissolves it together, and then differences it from each red feature.
    – strythe
    Jan 18, 2019 at 6:09

1 Answer 1

4

Assuming that you have a polygon layer named 'polygonsA', with a field named 'idA'; and you want to make the difference between any feature of that layer and the feature of the polygon layer 'polygonsB' that have the same (idA) value in the field 'idB'.

It is possible in a simple way as long as there is a single feature in polygonsB for each value of the attribute. If not, Dissolve first the polygonsB layer by the idB field.


  • Open the Geometry by expression tool.

  • Select polygonsA as input layer.

  • Select Polygon as output geometry type.

  • Write the following geometry expression:

difference( $geometry, geometry( get_feature( 'polygonsB', 'idB', attribute( $currentfeature, 'idA'))))
  • Run.

The output is a polygon layer named 'Modified geometry' with the difference by attribute between polygonsA and polygonsB.

5
  • Hi. Thank you so much! I've tried this and this is exactly what I need and it's my first time to hear about "Geometry by expression". Also, would you have any idea on how to implement this in modeler? Specifically the part where you call the second layer and its field name. I forgot to mention that this is a step in a model I'm making..
    – strythe
    Jan 21, 2019 at 2:43
  • Hi, you are welcome! In the modeler, the algorithm works in the same way with the input layer, which you can select when you run it. What I am still investigating is how to pass parameters to the expression, in order to choose the second layer and the fields without being predefined in the model. I am also interested in knowing, so if you want to start a new question about it, it will be welcome. Jan 21, 2019 at 3:41
  • In the modeler, there is the possibility to run background algorithms and use their outputs for a new algorithm. I can think of making two Extract by attribute and use the outputs for a Difference. I think that would work well. Still, I would like to be able to pass parameters to the expressions. Jan 21, 2019 at 4:22
  • That's what I was trying to figure out as well. Since the second layer is just a temporary layer (and the first layer), I don't know how to pass the layer name and field. I was wondering though, if merging the two layers would work with geometry by expression? ie. using a merged layer with 6 features, and perform difference on the first 3 and the second 3 features. I've been trying this idea out but I couldn't figure out the right expressions.
    – strythe
    Jan 21, 2019 at 4:24
  • impressive thank you!
    – belairaway
    Dec 19, 2021 at 12:23

Your Answer

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

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