Thursday 7 February 2019

Number inputs as raster calculator variables in QGIS Modeler


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?



Answer



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
XGRIDS
FORMULA
USE_NODATA
TYPE
RESULT

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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...