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.

  • 1
    Try using something like this: processing.runalg("saga:rastercalculator", raster, '', 'a > 0', False, 7, OutRaster). You can check this post to see what the parameters are for the SAGA raster calculator: Number inputs as raster calculator variables in QGIS Modeler.
    – Joseph
    Nov 17, 2016 at 11:56
  • Is the same possible with a grass r.mapcalc expression?
    – AJ Walsh
    Dec 8, 2016 at 14:22
  • Should be, type in processing.alghelp("grass7:r.mapcalc") to see the parameters required. Note that the expression used would be something like "raster_name" > 0.
    – Joseph
    Dec 8, 2016 at 14:26

1 Answer 1


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


  • '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


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

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.