Accessing current feature attribute, when iterating over vector features input in QGIS Model Builder
I am trying to develop a process with the model builder in QGIS (3.38) where the process loads a CSV file as vector features, for the ability to go over each combination at a row level. The later part of the algorithm uses the row i.e. feature to get the WFS layer and save it to SQL.
The csv file has a name and id, amongst which id is used in the get WFS step. I am after the ability to refer to the current feature in the iteration. I've tried the @id
, and @feature
variables available with the precalculated value, without any result.
I could access the field if I use an absolute number against the id rather than using @id
or use $current_feature
directly:
concat(@var_TypeNameHeader, to_string(attribute(get_feature_by_id(@select_layers_table, 1), @layer_id_field)))
Example like this one - Iterate Graphical Model over a set of numbers in a non geometric layer
The solutions on SE are mostly diverging to the Python side and I am looking for solutions within graphic modeler inputs or algorithms.
-
another similar issue: gis.stackexchange.com/questions/483687/…karunakar– karunakar2024年09月11日 23:14:40 +00:00Commented Sep 11, 2024 at 23:14
1 Answer 1
The following solution will also work in the modeler when needing to iterate over features of a layer using an algorithm that only takes a hard-coded value (and does not accept a field or calculated value when used in stand alone).
This works by using the green "iterate over this layer" button on the input field when running the model
In your model:
- Use field calculator on your input layer:
- add a new field (I call it "id_1")
- integer
- field length of 1
- value of 1
Access the current feature by calling the result of the field calculator step:
get_feature( @Field_calculator_OUTPUT,'id_1', 1 )
Run the model by using the green "iterate over this layer" button for your input field.
You can then use the attribute you need with:
attribute(get_feature( @Field_calculator_OUTPUT,'id_1', 1 ), @field_name )
where @field_name is a string input containing the field name you want to access.
You can also add that value 1 before using the model so you don't need to use field calculator in the model.
You were half-way there by accessing the field with get_feature_by_id, this solution just creates a new 'fake' id of value 1 and accesses the feature with get_feature instead of get_feature_by_id.
Explore related questions
See similar questions with these tags.