Sunday, 21 July 2019

Processing raster files with raster calculator in PyQGIS?


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 "", line 65
processing.Saga:RasterCalculator(raster, "(raster)*2/2",outraster)
^
SyntaxError: invalid syntax


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...