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?




For the SAGA raster calculator, you can set the Formula option to read the value from your Number Input parameter:

SAGA raster calculator

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:


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


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

GDAL raster calculator

  • 1
    Thanks a lot! It worked like a charm. I missed the option with the "Pre-calculated value. – Deepmir Jul 17 '19 at 9:51
  • @Deepmir - Most welcome! Glad it helped :) – Joseph Jul 18 '19 at 9:51

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.