7

The setting

In a QGIS model, I have an input layer polygon from which a grid is created.

On this grid-layer, a new attribute field should be created based on an expression that refers to the input layer polygon. That works well as long as I set a fixed name for the layer polygon - see the expression and the model linked here (at the bottom).

Basically, I just have to replace 'polygon' in the overlay_intersects-part of the expression in a way to refer to the model's input layer: overlay_intersects('polygon',$geometry) works, as long as the layer indeed is named 'polygon', but the model should be flexible to take the selected input layer here.

The question

How can the layer-name be defined so that it refers to the name of the input layer, regardless of its name?

What I tried

In the field-calculator dialog, I select pre-defined value and try to set my expression in 'single quotes' as described here in the style of: 'expression' || @loadpolygonlayer || 'expression'. For the layer to refer, I select it from the context variables as can be seen on the screenshot.

I also tried to get the name of the layer by using this syntax and different variations with and without quotes, with masking single quotes etc., with no success.

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52
Babel
  • 71,072
  • 14
  • 78
  • 208
  • 1
    This is (currently) not possible. See https://github.com/qgis/QGIS/issues/37347 which is marked as duplicate of https://github.com/qgis/QGIS/issues/30397. Where nyalldawson states The issue here is that expression functions which operate on map layers (including get_feature_by_id ) don't currently have access to any temporary output layers created a step in a model. This applies to a lot of functions, including aggregates. – MrXsquared May 25 '21 at 16:34
  • I stumbled upon this when I looked for a solution. However, I'm not sure that this is the same case: in fact, I want to refer to an already existing layer (not a temporary one, not one created as model-output), the same as the input layer. There is even a variable appearing in the selection as can be seen on the screenshot. So it should be possible to use this somehow? Fact that the model works when refering to the exact layer-name makes me optimistic that it should be possible. – Babel May 25 '21 at 16:38

1 Answers1

10

The trick to be able to refer to the layer-name of an input vector-layer inside an expression, using a model, is to rename the input layer.

Add a Rename layer algorithm, for the layer use Model input and select Load polygon layer (the input layer) and a define a name: the input layer is renamed to let's say polygon_model. This name can then be used in the expression of the field calculator to refer to the input layer.

Don't forget to set Algorithm Dependencies in the Field calculator algorithm: check the box refering to Rename layer algorithm.

The model can be downloaded from here. It does an Anti-aliased rasterization of an input polygon layer.

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • 2
    So you are essentially using the Rename layer alogrithm as a kind of variable assignment? Nice approach :) – Matt Jan 15 '22 at 11:52
  • It is the only way I see right now to achieve this. Would be interesting if there are other ways to do it. – Babel Jan 15 '22 at 11:53
  • Here is another possible solution: https://gis.stackexchange.com/questions/446636/using-input-parameter-as-part-of-sql-query-in-qgis-model/448131#448131 – César Arquero Cabral Dec 21 '22 at 18:07