5

How can i use input numbers as variables in any of the raster calculator algorithms in QGIS through the modeler? One very simple example of that would be a model that allows me to divide raster cell values by an input number:

model example

The thing is: none of the raster calculator algorithms lets me pick the number input as a variable. Is that not possible solely through the application's modeler?

1 Answer 1

4

I'm not sure if you can combine input numbers with the formula from the GUI modeler. I tried testing it and didn't seem to like it.

An alternative could be to create a custom script which, when executed, can look like the exact same interface as when you run a model. The difference here is that you have to write out the code (you can find more information here: Writing new Processing algorithms as python scripts).

To create such a script, go to:

Processing Toolbox > Scripts > Tools > Create new script

Then copy/paste the following:

##Example=name
##Input_Raster=raster
##Number=number 1

processing.runandload("saga:rastercalculator", Input_Raster, '', 'a / ' + str(Number), False, 7, None)

Save the script and run it from the Processing Toolbox:

Executing script

Note: I receive errors when trying to save it directly to a directory. To avoid this, the code saves and loads the output as a temporary layer. You can then save this layer manually.



Essentially, you define various parameters at the beginning of the script (i.e. name of script, input layer, input number). The last line calls the Raster Calculator function from the Processing plugin where you can define the required parameters. You can find what parameters are required from the Python Console (from the menubar Plugins > Python Console) and type:

import processing
processing.alghelp("saga:rastercalculator")

You should receive:

Python Console 
Use iface to access QGIS API interface or Type help(iface) for more info
>>> import processing
>>> processing.alghelp("saga:rastercalculator")
ALGORITHM: Raster calculator
    GRIDS <ParameterRaster>
    XGRIDS <ParameterMultipleInput>
    FORMULA <ParameterString>
    USE_NODATA <ParameterBoolean>
    TYPE <ParameterSelection>
    RESULT <OutputRaster>

TYPE(Output Data Type)
    0 - [0] bit
    1 - [1] unsigned 1 byte integer
    2 - [2] signed 1 byte integer
    3 - [3] unsigned 2 byte integer
    4 - [4] signed 2 byte integer
    5 - [5] unsigned 4 byte integer
    6 - [6] signed 4 byte integer
    7 - [7] 4 byte floating point number
    8 - [8] 8 byte floating point number
3
  • 1
    Thank you very much. Besides helping with the question this helped me come up with solutions for some other problems i had.
    – pdebem
    Aug 9, 2016 at 18:42
  • @pdebem - Most welcome, glad it was helpful :)
    – Joseph
    Aug 10, 2016 at 10:37
  • 1
    see gis.stackexchange.com/questions/328261/…
    – cefect
    May 18, 2021 at 23: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.