[GIS] SAGA GIS raster calculator – QGIS scripting gt expression

gdalpyqgissaga

I am trying to perform a simple raster calculator that takes input rasters and outputs a new raster with a constant value where values exceed a specified number in the input.

This is part of of a QGIS script and currently works using GDAL raster calculator. However I am having issues where python stops working when running the script using GDAL RC. Therefore i'd like to adjust my script to use the SAGA GIS equivalent to perform the same process:

processing.runalg('gdalogr:rastercalculator', raster,'1',None,'1',None,'1',None,'1',None,'1',None,'1','A>0',None,5,None,OutRaster)

My question is how can I change the line above in my script to perform the same calculation using the SAGA raster calculator? That is how can I write the expression which will output a new raster with a constant value where the input raster exceeds 0.

Best Answer

processing.runalg('saga:rastercalculator', raster,[],'A>0',False,7,OutRaster)

Explanation

  • 'saga:rastercalculator' Calls the algorithm.
  • raster Your input raster
  • [] Additional input rasters as array (empty in your case)
  • 'A>0' The query to run
  • False Use NoData (True or False)
  • 7 The Output Data Type
    • 0: Bit
    • 1: unsigned 1 byte
    • 2: signed 1 byte
    • 3: unsigned 2 byte
    • 4: signed 2 byte
    • 5: unsigned 4 byte
    • 6: signed 4 byte
    • 7: 4 byte floating point number
    • 8: 8 byte floating point number
  • OutRaster Your output raster

Edit:

Also take a look at Joseph's previous answer to this related question.

Related Question