I'm newbie to programming in pyqgis.
I need to perform a simple "GeoTiff * A" type of calculation(where A is a constant), but I am struggling to do so.
I tried processing.runalg('saga:rastercalculator') but failed 'coz my constant is NOT a raster.
I suppose I should be using 'qgismodelertools:calculator' or possibly something like to what they use here http://geoexamples.blogspot.it/2012/12/raster-calculations-with-gdal-and-numpy.html
Any clues?
Gene's answer iss succinct and logical but rasterio does not come out of the QGIS 2.8 box, which I'm using from OSGEO4W.
In the meantime, I used EikeMike's useful code by turning it into a working, callable function:
def raster_calc(RGBI_DS,outFile,constant):
import processing, os, time, subprocess, array
from osgeo import gdal
from osgeo.gdalnumeric import *
from osgeo.gdalconst import *
# Read Raster dataset
GDALRgbiDS = gdal.Open(RGBI_DS, GA_ReadOnly )
band1rot = GDALRgbiDS.GetRasterBand(1)
# Convert band to Array
Rdata = BandReadAsArray(band1rot) * 1.0
# calculate raster * constant
dataOut = (Rdata * constant)
# Export result as Geotiff
driver = gdal.GetDriverByName("GTiff")
dsOut = driver.Create(outFile, GDALRgbiDS.RasterXSize, GDALRgbiDS.RasterYSize, 1,
gdal.GDT_Float32)
CopyDatasetInfo(GDALRgbiDS,dsOut)
bandOut=dsOut.GetRasterBand(1)
BandWriteArray(bandOut, dataOut)
#Deletion of variables, this actually triggers the writing of the output file
dsOut =None
bandOut = None
driver =None
Later, I found myself a much easier and shorter solution.
Here it is:
Stick to 'saga:rastercalculator' (or similar algorithm). Simply build the mathematical formula therein as a new variable where your constant is itself a variable. In other words:
import processing
formula = ('a * %s' % (str(constant))) # where 'a' is going to point to yourraster and 'constant' is the numeral you want to multiply your raster by
processing.runalg('saga:rastercalculator',yourraster,None,formula,outFile)
Best Answer
Unfortunately the question is about PyQGIS and not GDAL (osgeo) (use rasterio, easier, see below in 3) and there are many examples in GIS Stack Exchange as How to evaluate raster calculator expressions from the console?
processing.runalg('saga:rastercalculator')
is the Saga raster Calculatorprocessing.runalg('grass:r.mapcalculator')
is the GRASS GIS raster calculatorTo use the QGIS raster calculator in the console, I detail here the process using the example of Cómo usar el Raster Calculator desde la Python Console de José Guerrero (in Spanish)
1) The raster calculator dialog
2) The script in the console
Characteristics of the raster layer
To use the raster calculator with the only band, the expression is therefore test@1 as in the dialog
3) EikeMike gives you a solution with GDAL, compare with a rasterio solution