3

In QGIS Graphical modeller there is the option to create user defined variables that one can use in the model. I dont always need to use the modeller to do stuff. Is there a way to have user defined variables in regular QGIS (very good for styling)?

I have found that in model properties I can use Title, Abstract, Author etc. for this, but that is really not very elegant. I just want regular variables that I can set and then use. Really good for visualization works.

Roman Dostál
  • 535
  • 2
  • 9

1 Answers1

3

Yes, you can define your own custom variables in QGIS in different levels like as in global Level, as project or layer variables and even as ad hoc variables in QGIS expressions using the function with_variable().

See functions help for with_variable() expression as well as an expample where three different variable are declared: vertex (=20), azimuth1 and azimuth2 (the last two have an expression assigned as values).

Go to the Settings / properties of the desired Level (e.g. right Click layer > properties > tab variables) and select variables to add your own variable by typing a name and the value of the variable. You can then call the variable by its Name preceded with @ - e.g. @my_variable.

See here for more information about custom variables: http://nyalldawson.net/2015/12/exploring-variables-in-qgis-pt-3-layer-level-variables/


The content (value) of the varible can be set to an attribute or a complex expression e.g. to generate all kind of sophisticated data defined styles. So this example: define a project variable called set_color with this value (string):

case 
when value = 1 then 'red' 
when value = 2 then 'blue' 
when value = 3 then 'green' 
else 'yellow' 
end

enter image description here

Now use the variable @set_color to create a data defined color of a layer, based on its attribute field value using the function eval(): eval(@set_color):

enter image description here

Variables you defined at global, project or layer level will appear in expression string builder dialog under variables:

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • Very cool, thank you so much. I always thought it was something else, now it makes complete sense. Two more questions then. 1. is there a way to have some of the variable on my interface as a panel or toolbar for quick access? 2. is there a way to assign an attribute to a variable? I often make complicated styling settings that include attributes of the layer and it is a pain to change all of them individually, if I could change them globally through a variable it would be a life saver. But I am afraid it only works with one value, right? Thanks. – Roman Dostál Sep 17 '22 at 13:19
  • I'm not sure what you mean by "have some of the variable on my interface as a panel or toolbar for quick access" - if you have another question, best post it as a separate post. 2. You can't only assign an attribute to a variable, but a whole expression, see updated answer.
  • – Babel Sep 17 '22 at 13:38
  • Added another edit at the bottom of my answer, maybe this is what you looked for...? – Babel Sep 17 '22 at 13:49
  • Lets forget n. 1 for now. Thank you for your edit. However, I am afraid it doesn't achieve much for me. I could just as well add this expression directly and not through the variable, even though its quite cool. What I am looking for is to quickly change part of this expression using variables. Lets use your example - what if I wanted to not use "value" but instead wanted to use a different attribute ("value2"), how do I set it up, so that I can change it in multiple styling options? Can this be achieved through variables? Example of expression of mine: https://imgur.com/a/8LobxXZ – Roman Dostál Sep 17 '22 at 13:59
  • Simply go where you defined the variable and change from value to value2 and wherever you used this custom variable, settings will use field value2 and not value any more. This works even for many different layers at once as long as all of them have a field value2 (or value). – Babel Sep 17 '22 at 14:12
  • I am probably doing something wrong. I have set variable s-nula to "B" and then s-srov to "D" and altered the expression like this CASE WHEN @s-srovn - @s-nula <0 THEN 'green' else CASE WHEN @s-srovn - @s-nula >0 THEN 'red' ELSE 'black' end end but it tells me the expression does not work. Original formula was: CASE WHEN "D" - "B" <0 THEN 'green' else CASE WHEN "D" - "B" >0 THEN 'red' ELSE 'black' end end I just want to use variables to change part of the formula, but dont understand how. – Roman Dostál Sep 18 '22 at 13:23
  • You deine a letter as value for the variable and try to Make calcalations with that? That cant work. What should D minus B result in? – Babel Sep 18 '22 at 14:54
  • It does not work for me. I dont know what I am doing wrong. Should I put it in quotes or single-quotes? Should it be just plain text? When I use any such variable that has either of these, the formula does not work. The initial calculation D-B is supposed to return a value that is the difference of these two. I wan to quickly change the outputs through the variables because I have set this in labels, thickness, colour, ... Can you do a fool-proof guide to set up variables that can change part of formulas by holding different attributes? Thank you kindly. – Roman Dostál Sep 18 '22 at 21:09
  • Sorry, did you read my comment? You can't use mathematical operations for characters. No way to work. To be able to help, share you data + project. Otherwise, no way to guess what you have... No idea what "B" and"D" are, where they're coming from etc. – Babel Sep 18 '22 at 21:21
  • By the way - avoid using hyphens - and other special characters in variable names. only use ASCII characters (A-Z, a-z), numbers (0-9) + underscores. – Babel Sep 18 '22 at 21:23
  • Your syntax for case when... is wrong. Don't add a second case when... statement in the else condition! This should work (avoing hyphens in variable names): `CASE WHEN @s_srovn - @s_nula < 0 THEN 'green'

    WHEN @s_srovn - @s_nula > 0 THEN 'red' ELSE 'black' end`

    – Babel Sep 18 '22 at 21:28
  • Well, when I dont put another CASE WHEN in the formula, I get an an error message of "Unexpected WHEN" etc. I have been using this way of writing it forever, and it always worked. Tried it your way and the same error I would expect happened. Thank you about the ASCII information. Anyway, I have tried to fill the value of the variable with B, with 'B' and with "B" and I always get "Cannot convert B to double". I think it does not get the attribute but rather the string. Is there some setting that I need to change? B and D are integer number of car intensities on network, two scenarios. – Roman Dostál Sep 20 '22 at 11:53
  • What I really dont understand is, how to write the attribute in the expression, so that QGIS knows its an attribute. None of my attempts worked. – Roman Dostál Sep 20 '22 at 11:55
  • I guess there is some completely different problem involved. Can you share project+data, at least a sample? To me, it's completely unclear what you have and what you try. The expression in itself should work, but there seems to be a problem with input etc. To refer to a field, you normally do that with either the bare name of the attribute attribute_name or enclosed in double quotes "attribute_name". – Babel Sep 20 '22 at 13:21
  • Sorry for taking this long to reply. I have prepared a testing project for this purpose and you may find it here: https://drive.google.com/file/d/1sPx1hJbcjZP3racLDB9HkwnBn2Jp3YQv/view?usp=sharing – Roman Dostál Oct 03 '22 at 11:31
  • Right now, it seems to be kind of working, however for the conditionals I have set is does not behave as it should. For example I have stated that I only want it to be red if the values are greater than 800, not all features match this condition, yet all features are coloured red. Filling the variable with an attribute name seems to not consider all values separately. There is just one layer: layer_1. Variable (layer specific) is var_1, the value is value_1, code is case when @var_1 >800 then 'red' else case when @var_1 < 10 then 'blue' else 'green' end end. – Roman Dostál Oct 03 '22 at 11:33
  • When I add another variable var_2 with value of value_2 and change the code to: case when @var_1* @var_2 >800 then 'red' else case when @var_1* @var_2 < 10 then 'blue' else 'green' end end then I get the same error message as before "Cannot convert 'value_1' to double". I think it somehow cannot get the correct format form the attributes. Even tried putting the value of the variable as a code to_real(var_1) etc, but the same error - it seems to read it as a string. I am not sure about how the quotation works in the variable value. – Roman Dostál Oct 03 '22 at 11:40