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 1



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, 2019 at 9:51
  • @Deepmir - Most welcome! Glad it helped :)
    – Joseph
    Jul 18, 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? Aug 20, 2020 at 20:25
  • This doesn't seem to be working in 3.16
    – cefect
    May 18, 2021 at 23:55
  • How would this work with a like expression? I tried "RoadName" like '%+'||@RoadName||'+%' usually it would be `"RoadName" like '%TheRoadName%'
    – GeorgeC
    Mar 14, 2022 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.