2

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

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Dec 4, 2024 at 12:32
4
  • 1
    What have you tried so far? Commented Dec 4, 2024 at 12:32
  • 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 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 Commented Dec 4, 2024 at 13:21
  • 1
    Please add those relevant details to your question. I am unsure, whether virtual fields may be part of a layer style - have you tried adding the virtual field and then saving the .qml? Commented Dec 4, 2024 at 13:31
  • You are totally right : I didn't realize the virtual fields could be part of a .qml file! I did not tick the Fields option when saving the .qml file and the virtual field was dropped due to this. Thank you a lot! :) Commented Dec 4, 2024 at 13:51

1 Answer 1

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 :

  1. 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).
  2. 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 :)

answered Dec 5, 2024 at 8:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.