4

This is the first QGIS Process Model I have ever attempted to build (and I am still on a learning curve with QGIS generally).

THE ASSIGNMENT:

I need to build a model that will take an input vector and (1) rename it (the name must come from an attribute value stored within the layer), (2) clip it against another polygon, (3) and finally add a pre-defined style.

THE CATCH:

The input layers will always have the same name i.e. "2D Zones" there is no way around this as they are outputs from another piece of software and this cannot be changed. The fields for these input layers will always have the same names and will be arranged in the same order, the attribute values will differ.

There is a field within each "2D Zones" table called "retperiod" the value appearing in that column will be the same throughout the whole column but will differ from layer to layer, so this is the attribute I want to use as a name.

BACKGROUND READING:

After reading the solutions to similar problems posted here:

I have managed to come up with a model that performs the task I wish it to do (see link).

https://drive.google.com/drive/folders/1y0RnrLsOQIHaiDn74U8Qsana1iBUyoM8?usp=sharing

I have also read all this lot (although probably haven't understood it all):

THE PROBLEM

However, one shortcoming of my model is that I have to specify the input layers as both a "Vector Features" and a "Vector Layer" input. This seems to be because I can only the Modeller won't allow the "get_feature_by_id" function to work on the "vector layer" input but it will work on the "vector feature" input..... Essentially, this model will only complete the renaming routine, i.e. this bit:

attribute(get_feature_by_id(@vectorfeature,1),'retperiod')

If I get it to look in the "vector feature" input for the attribute that can be used as a name but then it must be applied to the input from "vector layer" in order for the subsequent processes (i.e. clipping and styling) to work.

All very unintuitive.

THE QUESTION:

Is there a more elegant solution to all this whereby I don't have to specify the same inputs twice in two different ways?

This is still problematic as the original files all have exactly the same name and there is every chance I will match the two inputs incorrectly when using the "batch process" option.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338

1 Answers1

0

Use the Rename Layer algorithm, set the New name input to Pre-calculated Value and insert an expression that gets the value of the attribute retperiod from the input layer. The input layer is available as a context variable in the expression string builder.

The expression to use looks like:

attribute (get_feature_by_id (@load_vector_layer, 1),'retperiod')

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • If I were to implement your suggestion I could use the "rename" algorithm as soon as the input vector is added to the model: no point in this as the clipping procedure requires the output to be renamed "clipped" unless you re-specify a name. Logically then, I could do it on the model output.... not so; – ManWithQuestions Jan 23 '23 at 09:43
  • "attribute (get_feature_by_id (@load_vector_layer, 1),'retperiod')" doesn't seem to work on vector layers only on vector objects (please see third hyperlink on my original post). This is very much the source of my confusion as your suggested solution looks logical, but in practice; the "attribute()" statement just returns "NULL" and the model fails on this basis. – ManWithQuestions Jan 23 '23 at 09:43
  • I think the difference between what you did with your previous example (i.e. first hyperlink: which I am trying to copy) and what I am doing is that your model might be referring to a saved layer that you have downloaded with some fixed address, my model is trying to rename a virtual layer that is output from an algorithm and thus not saved. (possibly??). – ManWithQuestions Jan 23 '23 at 09:47
  • OK, I see I was maybe referring to another case. Just renaming the layer this way worked in my case, with the expression provided. – Babel Jan 23 '23 at 10:10
  • Well thanks for your input anyway! :-) I really can't understand why your solution doesn't work in my case to be honest. I can see that QGIS seems to point blank refuse to use "@input vector layer" but is happy enough with "@input vector feature" but I have no idea why this is the case. – ManWithQuestions Jan 23 '23 at 10:25
  • Can you post a screenshot showing 100% the same as my screenshot - maybe there is a difference you miss....? – Babel Jan 23 '23 at 10:43
  • Frustratingly there is no way for me to post a picture. I have posted the model plus some example data at this hyperlink. If you have time to look at it and try making the change yourself you might be able to see what I mean. I don't believe I have made a syntax error or not copied the example properly; QGIS makes it clear when there is a problem of that kind. The model runs just fine when I rename using @vectorfeature as the place to look for an attribute to use as a name, just not when I use @vectorlayer

    https://drive.google.com/drive/folders/1y0RnrLsOQIHaiDn74U8Qsana1iBUyoM8

    – ManWithQuestions Jan 23 '23 at 11:46
  • In your example, you should rename the expression using @inputvectorlayer: attribute(get_feature_by_id( @inputvectorlayer,1),'retperiod') Still, I'm not exactly sure what the model should achieve (what should I set as input, what should the output look like). – Babel Jan 25 '23 at 16:54
  • So this is the root of the confusion: I have tried renaming the expression as you suggest but the expression returns "NULL" and the renaming therefore fails. I think it might be because "@inputvectorlayer" would require referring to the file itself e.g. C:\users\QGIS\vectorlayer.gpkg instead of the model input (I am guessing). The model is to clip some Hydraulic Model results (vectors all named 2dZones) with a boundary polygon, each clipped output is then named after the value found in the "retperiod" column (e.g 10, 50, 200) then a style is applied to show flood depth. Nothing complicated – ManWithQuestions Jan 26 '23 at 17:26