1

I would like to combine two fields in QGIS using graphical modeller. If I were to do it using field calculator, I would do it as follows:

"field1" + ", " + "field2"

Unfortunately, I cannot do it, as I want those fields to be passed as parameters in the graphical modeller.

I have the following model:

enter image description here

As you can see, I have two parameters: "Calle" and "Portal", which are fields of the input vector layer. I would like to combine both fields using the field calculator algorithm. I need to pass the fields' names as parameters as I am looking for a generic solution that may work for different field names.

I have tried with the following formula in the field calculator algorigthm:

enter image description here

Unfortunately, instead of combining the parameters' values, it combines the parameters' names. Therefore, I tried with the parameter() function, which I assume should pass not the parameter itself but its value, as follows:

enter image description here

Unfortunately, when doing so, I get an empty result.

Is there any way to achieve what I am looking for?

2

You need to use the eval() function in your expression to get the actual value of the vector field variable instead of the variable name, like this:

eval(@field1) + ', ' + eval(@field2)

More details in the docs: https://docs.qgis.org/testing/en/docs/user_manual/working_with_vector/expression.html#general-functions

  • Thank you very much, Germán! I didn't know the eval() function, thanks for the documentation. – ccamara Jan 10 at 8:03
1

You can use the + operator when you want to make a sum of two numeric fields.

When you have at least one field that is not numeric, the addition is not possible and to combine the two, you have to use the concatenation operator ||.

Try the following command: field1 || ',' || field2

  • Thank you very much for your answer, but unfortunately my problem is not combining fields per se, but retreiving parameters' values in order to combine fields. I tried your answer, but does not work. – ccamara Jan 9 at 10:46
  • With my answer, you combine the values of the fields. I must have misunderstood your problem, what do you mean by retreiving parameters ? – Vincent Bré Jan 9 at 10:50
  • Hello again, I have added more information to the question, hopefully is clearer now. – ccamara Jan 9 at 10:57
  • You want to identify the first field and the second field, not by the name because this one will evolve but by recovering the names of the layer fields of Calle and Portal ? – Vincent Bré Jan 9 at 11:04
  • 1
    Thanks for your time and help, Vincent: Germán's answer is what I was looking for! – ccamara Jan 10 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.