4

I want to use 'Geometry by expression' in a model I'm working on but I couldn't figure out how to pass the layer name parameter. Basically I have two polygon layers (A and B) and I want to do a feature-by-feature difference using a shared attribute. The expression I have below works outside the modeller.

Inside modeller, since the two layers are intermediate outputs, and not an input, I do not know what to use for the layer name parameter ('layer B').

difference( $geometry, geometry( get_feature( 'layerB', 'attributeB', attribute($currentfeature, 'attributeA'))))
2

I find the processing modeller extremely challenging to work with, and not as well documented as I'd like it to be. Since my first suggestion didn't work, I have a couple of other suggestions.

This was my first suggestion, which didn't work:

Intermediate output layers are usually called something like 'Toolname of layername'. Eg, the output of running the clip tool on a layer called 'vectorlayer', is probably called something like 'Clip of vectorlayer'. You can find the exact name of an intermediate output by adding any algorithm that would let you choose the intermediate output as input from a dropdown menu. Make a note of what the intermediate outputs are called, and use that name in your difference() expression.

Suggestion number two:

I wonder if the issue could be as simple as that the it's expecting a string for 'LayerB', but the string 'Difference' from algorithm 'Building_frontbuffer' because has single quotes inside it. So if you used ''Difference' from algorithm 'Building_frontbuffer'' for 'LayerB', I believe it would be interpreted as three separate strings, with gibberish in between.

  • first string: ''
  • gibberish: Difference
  • second string: `' from algorithm '
  • gibberish: Building_frontbuffer
  • third string: ''

If this is the issue, you need to escape the single quote characters, so the expression can understand the entire layer name ('Difference' from algorithm 'Building_frontbuffer') as a single string.

According to this Q&A, Is there an escape option for single quotes & apostrophes in QGIS?, you can just put single quotes around a string with single quotes in it. I wouldn't have thought that would work, but it's worth trying:

''Difference' from algorithm 'Building_frontbuffer''

Note that the string starts and ends with two single quote characters, not one double quote character. ('' is ' twice, not ")

If that doesn't work, try escaping the single quote with \, like this:

'\'Difference\' from algorithm \'Building_frontbuffer\''

(Based on suggestions from this Q&A on stackoverflow: Replacing an apostrophe in a string.)

Suggestion number three:

As a workaround, make the 'difference' output a permanent layer. Define the layer name and filepath with a string input. This way you know exactly what the layer is called, so you can use the correct layer name in the expression.

Final suggestion:

If none of the above suggestions works, and you can't find a solution, you just have to end the model at this point. Make the final output of your model a permanent layer from 'Difference' from algorithm 'Building_frontbuffer'. Put the rest of your steps from the original model into a new model.

  • Hi @csk, I've tried your suggestion but I still can't get it to work unfortunately. The name that is shown for the intermediate output is 'Difference' from algorithm 'Building_frontbuffer', single quotes included. I've tried different variations of the name including without quotes but still no luck. – strythe Feb 22 at 4:42
  • Hi @strythe, I'm sorry my first suggestion didn't work. I updated my answer with a few more suggestions. If any of them works, please let me know which it was, so I can update this to a final answer that might be helpful to others with a similar issue. – csk Feb 22 at 17:00

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.