Skip to content


Browse files Browse the repository at this point in the history
Merge pull request #3779 from volaya/rastercalculator
[processing] add native raster calculator
  • Loading branch information
alexbruy committed Dec 5, 2016
2 parents abbe281 + 5c6c18c commit 2fea23f
Show file tree
Hide file tree
Showing 16 changed files with 801 additions and 103 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/
Expand Up @@ -78,7 +78,8 @@ def __init__(self, parent, alg):

def connectParameterSignals(self):
for w in list(self.widgets.values()):
for wrapper in list(self.wrappers.values()):
w = wrapper.widget
if isinstance(w, QLineEdit):
elif isinstance(w, QComboBox):
Expand Down
15 changes: 15 additions & 0 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -554,3 +554,18 @@ qgis:voronoipolygons: >


qgis:rastercalculator: >
This algorithm allows to perform algebraic operations using raster layers.

The resulting layer will have its values computed according to an expression. The expression can contain numerical values, operators and references to any of the layers in the current project. The following functions are also supported:

- sin(), cos(), tan(), atan2(), ln(), log10()

The extent and cellsize can be defined by the user. If the extent is not specified, the minimum extent that covers the input layers will be used. If the cellsize is not specified, the minimum cellsize of all input layers will be used.

The cellsize is assumed to be the same in both X and Y axes.

Layers are refered by their name as displayed in the layer list and the number of the band to use (based on 1), using the pattern 'layer_name@band number'. For instance, the first band from a layer named DEM will be referred as DEM@1.

When using the calculator in the batch interface or from the console, the files to use have to be specified. The corresponding layers are refered using the base name of the file (without the full path). For instance, is using a layer at path/to/my/rasterfile.tif, the first band of that layer will be refered as rasterfile.tif@1.

1 change: 0 additions & 1 deletion python/plugins/processing/algs/qgis/
Expand Up @@ -86,7 +86,6 @@ def setValue(self, value):
return False
return False

self.addParameter(ParameterFieldsMapping(self.FIELDS_MAPPING,'Fields mapping'),
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/
Expand Up @@ -178,6 +178,7 @@
from .GeometryByExpression import GeometryByExpression
from .SnapGeometries import SnapGeometriesToLayer
from .PoleOfInaccessibility import PoleOfInaccessibility
from .RasterCalculator import RasterCalculator
from .CreateAttributeIndex import CreateAttributeIndex
from .DropGeometry import DropGeometry
from .BasicStatistics import BasicStatisticsForField
Expand Down Expand Up @@ -245,7 +246,7 @@ def __init__(self):
RemoveNullGeometry(), ExtractByExpression(), ExtendLines(),
ExtractSpecificNodes(), GeometryByExpression(), SnapGeometriesToLayer(),
PoleOfInaccessibility(), CreateAttributeIndex(), DropGeometry(),
BasicStatisticsForField(), RasterCalculator()

if hasMatplotlib:
Expand Down
165 changes: 165 additions & 0 deletions python/plugins/processing/algs/qgis/
@@ -0,0 +1,165 @@
# -*- coding: utf-8 -*-

Date : November 2016
Copyright : (C) 2016 by Victor Olaya
Email : volayaf at gmail dot com
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput
import os

__author__ = 'Victor Olaya'
__date__ = 'November 2016'
__copyright__ = '(C) 2016, Victor Olaya'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import math
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterMultipleInput, ParameterExtent, ParameterString, ParameterRaster, ParameterNumber
from processing.core.outputs import OutputRaster
from import dataobjects
from processing.algs.gdal.GdalUtils import GdalUtils
from qgis.core import QgsRectangle
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.algs.qgis.ui.RasterCalculatorWidgets import LayersListWidgetWrapper, ExpressionWidgetWrapper

class RasterCalculator(GeoAlgorithm):


def defineCharacteristics(self):, self.i18n_name = self.trAlgorithm('Raster calculator'), self.i18n_group = self.trAlgorithm('Raster')

self.addParameter(ParameterMultipleInput(self.LAYERS,'Input layers'),
metadata={'widget_wrapper': LayersListWidgetWrapper}))

class ParameterRasterCalculatorExpression(ParameterString):

def evaluateForModeler(self, value, model):
for i in list(model.inputs.values()):
param = i.param
if isinstance(param, ParameterRaster):
new = "{}@".format(os.path.basename(param.value))
old = "{}@".format(
value = value.replace(old, new)

for alg in list(model.algs.values()):
for out in alg.algorithm.outputs:
if isinstance(out, OutputRaster):
if out.value:
new = "{}@".format(os.path.basename(out.value))
old = "{}:{}@".format(,
value = value.replace(old, new)
return value

metadata={'widget_wrapper': ExpressionWidgetWrapper}))
self.addParameter(ParameterNumber(self.CELLSIZE,'Cellsize (use 0 or empty to set it automatically)'),
minValue=0.0, default=0.0, optional=True))
self.addParameter(ParameterExtent(self.EXTENT,'Output extent'),

def processAlgorithm(self, progress):
expression = self.getParameterValue(self.EXPRESSION)
layersValue = self.getParameterValue(self.LAYERS)
layersDict = {}
if layersValue:
layers = [dataobjects.getObjectFromUri(f) for f in layersValue.split(";")]
layersDict = {os.path.basename(lyr.source()): lyr for lyr in layers}

for lyr in dataobjects.getRasterLayers():
name =
if (name + "@") in expression:
layersDict[name] = lyr

entries = []
for name, lyr in layersDict.items():
for n in range(lyr.bandCount()):
entry = QgsRasterCalculatorEntry()
entry.ref = '{:s}@{:d}'.format(name, n + 1)
entry.raster = lyr
entry.bandNumber = n + 1

output = self.getOutputValue(self.OUTPUT)
extentValue = self.getParameterValue(self.EXTENT)

if extentValue:
extent = extentValue.split(',')
bbox = QgsRectangle(float(extent[0]), float(extent[2]),
float(extent[1]), float(extent[3]))
if layersDict:
bbox = list(layersDict.values())[0].extent()
for lyr in layersDict.values():
raise GeoAlgorithmExecutionException("No layers selected"))

def _cellsize(layer):
return (layer.extent().xMaximum() - layer.extent().xMinimum()) / layer.width()
cellsize = self.getParameterValue(self.CELLSIZE) or min([_cellsize(lyr) for lyr in layersDict.values()])
width = math.floor((bbox.xMaximum() - bbox.xMinimum()) / cellsize)
height = math.floor((bbox.yMaximum() - bbox.yMinimum()) / cellsize)
driverName = GdalUtils.getFormatShortNameFromFilename(output)
calc = QgsRasterCalculator(expression,

res = calc.processCalculation()
if res == QgsRasterCalculator.ParserError:
raise GeoAlgorithmExecutionException("Error parsing formula"))

def processBeforeAddingToModeler(self, algorithm, model):
values = []
expression = algorithm.params[self.EXPRESSION]
for i in list(model.inputs.values()):
param = i.param
if isinstance(param, ParameterRaster) and "{}@".format( in expression:

dependent = model.getDependentAlgorithms(
dependent = []
for alg in list(model.algs.values()):
if not in dependent:
for out in alg.algorithm.outputs:
if (isinstance(out, OutputRaster)
and "{}:{}@".format(, in expression):

algorithm.params[self.LAYERS] = values

0 comments on commit 2fea23f

Please sign in to comment.