4

I am trying to process some raster files using python scripts in Qgis 2.20, and I have been having trouble finding any documentation on using raster calculator in scripts being run in the processing toolbox.

I am trying to use scripts to process weather data. I will need to recalculate values of cells in order to represent projected wind speeds at different heights and probable gusting speed. I am trying to use the raster calculator for this but have been having trouble. I was not able to easily find a function for the script, and after some digging through the python console I was able to find the saga:rastercalculator function. Documentation in the python console (obtained through alhelp()), say that the parameters are as follows. algorithm: raster calculator Grids formula Raster

I am trying to run a basic formula through the raster calculator to get it working, but am having no luck.

Is this even the right approach to recalculating grid values in a python script?

Here is the code:

#load and symbolize tiff

##[User scripts]=group
##input_raster=raster

#Imports
#=======
from qgis.core import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.analysis import  *
import processing
from processing.core.Processing import Processing

#import Tiff

fileName = input_raster
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
raster = QgsRasterLayer(fileName, baseName)
raster.loadNamedStyle(r"C:\Users\Maximilian\Documents\School\AGRG\test.qml")
# add map to display
QgsMapLayerRegistry.instance().addMapLayer(raster)

#recalculate values
processing.Saga:RasterCalculator(raster, "(raster)*2/2",outraster)

Here is the error:

Uncaught error while executing algorithm
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:/PROGRA~2/QGISVA~1/apps/qgis/./python/plugins\processing  \core\GeoAlgorithm.py", line 202, in execute
    self.processAlgorithm(progress)
  File "C:/PROGRA~2/QGISVA~1/apps/qgis/./python/plugins\processing\script\ScriptAlgorithm.py", line 252, in processAlgorithm
    exec script in ns
  File "<string>", line 65
    processing.Saga:RasterCalculator(raster, "(raster)*2/2",outraster)
               ^
SyntaxError: invalid syntax
4

First of all, to actually run a saga processing scrip use

processing.runalg("saga:algorithmname", parameter1, parameter2) 

Basically Qgis can do two things:

(1) Perform mathematical operations with rasters -> use saga raster calculator from processing toolbox

A thing that can help you is that you can refer to the grids as a,b,c etc. a+b will add two grids.

(2) perform logical tests e.g. raster "value" > 10 -> use QgsRasterCalculator from Qgis.analysis

The script below will execute a simple test. I believe you can use SQL Query within the Python code.

from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry

# Add raster 
rasterpath  = "/Desktop/werkmap/CHIRPS/chirps-v1.8.2012.01.tif"

bohLayer  = QgsRasterLayer(rasterpath, "rasterlayername")
if not bohLayer.isValid():
    print "Layer failed to load!"

# Load raster layer into canvas (not required for script but hey it looks awesome) 
QgsMapLayerRegistry.instance().addMapLayer(bohLayer) 


entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )

# repeat step above if multiple bands


# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1 > 10', '/Desktop/werkmap/output/outputfile.tif', 'GTiff', bohLayer.extent(), bohLayer.width(), bohLayer.height(), entries )
calc.processCalculation()

print "finish"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.