4

I am trying to create an if/else condition with a boolean parameter in QGIS graphic modeler. The parameter will be used inside Field Calculator2 algorithm.By default the parameter is False.

enter image description here

I am using the boolean parameter to create an if/else condition inside the formula of the Field Calculator2 Algorithm. See below. The model is working, but the condition always give result for False output even when I make Boolean Parameter True.

Is there something that I am doing wrong in the if/else formula here?

if( 'drainedsoilsleaveuncheckedifnotsure' = True,replace("GDCodeTemp", '/D', ''),replace("GDCodeTemp", map('A/', '', 'B/', '', 'C/', '')))

enter image description here

  • 2
    'drainedsoilsleaveuncheckedifnotsure' is a string literal which will always be true. What is this part of your expression supposed to represent? – ndawson Dec 25 '19 at 21:16
  • @ndawson this represents the Boolean Parameter input from user, false by default and True if checked by the user. – ar-siddiqui Dec 25 '19 at 22:45
  • 1
    Can you try with: if( var('drainedsoilsleaveuncheckedifnotsure') = True,replace("GDCodeTemp", '/D', ''),replace("GDCodeTemp", map('A/', '', 'B/', '', 'C/', '')? – Gabriel De Luca Dec 26 '19 at 4:38
  • @GabrielDeLuca your solution worked. Thank You. Can you put it as an answer, so that I can accept it and you get the reputation, also can you explain why do we have to put var(). – ar-siddiqui Dec 26 '19 at 14:49
  • You are welcome. I have just added an answer. – Gabriel De Luca Dec 26 '19 at 19:18
5

In the model environment, some variables are created for each input parameter, which can be used by algorithms.

The name of those variables is automatically assigned from the description of the parameter when it is created. For instance, in this model:

1

New variables are being created with the names of the inputs, converted to lowercase and removed special characters.

The value of those variables can be returned through the var( ) function (it is in General group):

2

I think that an input parameter can be created with a "variable friendly" description, and then the description could be changed to another "user friendly" one to be shown in the run dialog.


In your screenshot can be seen that you have an input parameter named Drained Solis? [leave u..., and you were trying to return its value from the following name string: 'drainedsoilsleaveuncheckedifnotsure', so you are close to what you need.

The string is not equal to a boolean true, so the condition is evaluated to false.

Your expression may be:

if(
  var('drainedsoilsleaveuncheckedifnotsure') = True,
  replace("GDCodeTemp", '/D', ''),
  replace("GDCodeTemp", map('A/', '', 'B/', '', 'C/', '')))

Also, you don't need to evaluate to a boolean True to return a true condition, if the value of the variable is already a boolean.

The value returned by var('drainedsoilsleaveuncheckedifnotsure') may be the condition.

| improve this answer | |

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.