12

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?

example

1 Answer 1

10

SAGA

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:

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

GDAL raster calculator

5
  • 1
    Thanks a lot! It worked like a charm. I missed the option with the "Pre-calculated value.
    – Deepmir
    Commented Jul 17, 2019 at 9:51
  • @Deepmir - Most welcome! Glad it helped :)
    – Joseph
    Commented 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? Commented Aug 20, 2020 at 20:25
  • This doesn't seem to be working in 3.16
    – cefect
    Commented 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
    Commented Mar 14, 2022 at 2:47

Your Answer

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

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