3

I am relatively new to QGIS. I use conditional formatting for various problems. In the qgis query editor, I would like to define a string variable for further usage, so I wouldn't have to retype the column name countles times.

Example: A = 'B' -- doesn't work

CASE 
    WHEN A LIKE   '<1,25'  THEN  color_rgb(1,2,3) 
END
-- A should be a column name
3

You can define a layer variable as it's explain in Nyalldawson blog. Then use the variable name with @ decorator in Calculator.

For exemple :

  1. Let's define the column_name variable in layer properties > variables panel to a real column name of your layer.

enter image description here

  1. Then in Calculator I set the conditional code with this new variable (like @Chrisl show in his answer).

enter image description here

For your next layers you have to create and define again the variable in layer properties but only once in this place. enter image description here

As you can see in your calculator when you use the exact same code as the precedent one, the variable change the value :

enter image description here

Why use layer variable than Global or Project variable ?

Because in you case you can define once the column_name variable and keep it as long as you want even if you define in another layer the same column_name variable. If you use a project or global variable, the variable is define for all your layers but you need to redefine the value each time you need to perform your code (not recommended in your case)

| improve this answer | |
1

You can reference the column by using the @ operator. So in your example:

CASE WHEN @A LIKE '<1,25' THEN color_rgb(1,2,3) END
| improve this answer | |
  • Thanks, that is nice, it also works with double quotes. But I want to define and store the column name in the script. So I wouldn't have to retype @A all over. – Krisselack Aug 16 '17 at 13:05
1

The problem was double quotes. A solution could be:

CASE WHEN "A" LIKE '<1,25' THEN color_rgb(1,2,3) END
| 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.