I am trying to create a flow in the graphical modeler using a variable in a raster calculation. Is there a way to use a variable directly in the raster calculator (SAGA or GDAL)? I know there was a similar question Number inputs as raster calculator variables in QGIS Modeler some time ago, however, I cannot get the solution from there to work in the latest QGIS version. Is there an alternative solution?
1 Answer
SAGA
For the SAGA raster calculator, you can set the Formula
option to read the value from your Number Input
parameter:
In the Formula
option, set the drop-down menu to Pre-calculated Value
. The first layer is defined by the string 'a'
. So if you want to take the original values from the first raster layer and add the value from the Number Input
parameter, you could use the following expression:
'a + ' || @numberinput
When you run the model, the log should show you the formula used by the calculator which could look something like:
Parameters
Grid system: 0.000000; 3675x 2288y; -5.694896x 40.248699y
Grids: 1 object (vir4326)
Grids from different Systems: No objects
Result: Result
Formula: a + 1
Name: Calculation
Take Formula: no
Use NoData: no
Data Type: 4 byte floating point number
GDAL
For the GDAL raster calculator, you can repeat the process as you did for SAGA but you must ensure you use the correct band for each input raster layer. The formula expression is also slightly different in that you use the capital letter 'A'
to denote the first layer:
'A + ' || @numberinput
-
1Thanks a lot! It worked like a charm. I missed the option with the "Pre-calculated value.– DeepmirCommented Jul 17, 2019 at 9:51
-
-
How do you set logical thresholds there? Your example says 'A +' || @numberinput, but what about '(A+B+C) >=' || ??? Is it possible? Commented Aug 20, 2020 at 20:25
-
-
How would this work with a like expression? I tried
"RoadName" like '%+'||@RoadName||'+%'
usually it would be `"RoadName" like '%TheRoadName%'– GeorgeCCommented Mar 14, 2022 at 2:47