Can a virtual field be added to a vector layer within a model (using a graphical modeller)? The goal would be to get a model output layer with the virtual field already declared and working.
*I've tried to create a new process that has a vector layer and an expression: the process reads the vector layer and adds a new virtual field with the input expression using the line:
vector_layer.addExpressionField(expression, virtual_field)
The process simply modifies the input layer without returning anything. It works when it is the last process used in the modeler but I'd like to set a style to the output layer using the "Edit rendering style for outputs" menu and when I do this the virtual field gets discarded. I was wondering if there was a more official way to do it
1 Answer 1
Answer for anybody that would have the same question :
I found 2 ways to add a virtual field to an output (vector) layer from a model :
- Create a new process that edits the output layer of the model using the
vector_layer.addExpressionField()
method ; this process should normally be run before the model terminates and releases its outputs so the virtual field should be present in the output file. Pros : you get a lot of control over the expression of the field and you can use inputs from the model (to name the field for example). - Add the virtual laye through a .qml style file using the Edit Rendering Style For Outputs menu ; the virtual field names and expression can be part of a .qml file, simply make sure the Fields section is checked when saving the style of an existing layer. Pros : no need to create a new process and you can modify all other sections of the style at the same time
If you do both things then the style applied by the .qml file will prevail, removing the virtual field that could have been added using the process so that the output layer perfectly satisfies the contents of the .qml file.
Big thanks to Erik :)
Explore related questions
See similar questions with these tags.
vector_layer.addExpressionField(expression,virtual_field)
line. The process simply modifies the input layer without returning anything. It works when it is the last process used in the modeler but I'd like to set a style to the output layer using the "Edit rendering style for outputs" menu and when I do this the virtual field gets discarded. I was wondering if there was a more official way to do it