mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-07 00:02:15 -05:00
initAlgorithm() method This allows 2 benefits: - algorithms can be subclassed and have subclasses add additional parameters/outputs to the algorithm. With the previous approach of declaring parameters/outputs in the constructor, it's not possible to call virtual methods to add additional parameters/ outputs (since you can't call virtual methods from a constructor). - initAlgorithm takes a variant map argument, allowing the algorithm to dynamically adjust its declared parameters and outputs according to this configuration map. This potentially allows model algorithms which can be configured to have variable numbers of parameters and outputs at run time. E.g. a "router" algorithm which directs features to one of any number of output sinks depending on some user configured criteria.
179 lines
7.9 KiB
Python
179 lines
7.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
RasterLayerCalculator.py
|
|
---------------------
|
|
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.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
|
from processing.core.parameters import ParameterMultipleInput, ParameterExtent, ParameterString, ParameterRaster, ParameterNumber
|
|
from processing.core.outputs import OutputRaster
|
|
from processing.tools import dataobjects
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
|
from qgis.core import (QgsApplication,
|
|
QgsRectangle,
|
|
QgsProcessingUtils,
|
|
QgsProject)
|
|
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
from processing.algs.qgis.ui.RasterCalculatorWidgets import LayersListWidgetWrapper, ExpressionWidgetWrapper
|
|
|
|
|
|
class RasterCalculator(QgisAlgorithm):
|
|
|
|
LAYERS = 'LAYERS'
|
|
EXTENT = 'EXTENT'
|
|
CELLSIZE = 'CELLSIZE'
|
|
EXPRESSION = 'EXPRESSION'
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
def group(self):
|
|
return self.tr('Raster')
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def initAlgorithm(self, config=None):
|
|
self.addParameter(ParameterMultipleInput(self.LAYERS,
|
|
self.tr('Input layers'),
|
|
datatype=dataobjects.TYPE_RASTER,
|
|
optional=True,
|
|
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(param.name())
|
|
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(alg.modeler_name, out.name)
|
|
value = value.replace(old, new)
|
|
return value
|
|
|
|
self.addParameter(ParameterRasterCalculatorExpression(self.EXPRESSION, self.tr('Expression'),
|
|
multiline=True,
|
|
metadata={'widget_wrapper': ExpressionWidgetWrapper}))
|
|
self.addParameter(ParameterNumber(self.CELLSIZE,
|
|
self.tr('Cell size (use 0 or empty to set it automatically)'),
|
|
minValue=0.0, default=0.0, optional=True))
|
|
self.addParameter(ParameterExtent(self.EXTENT,
|
|
self.tr('Output extent'),
|
|
optional=True))
|
|
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Output')))
|
|
|
|
def name(self):
|
|
return 'rastercalculator'
|
|
|
|
def displayName(self):
|
|
return self.tr('Raster calculator')
|
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
|
expression = self.getParameterValue(self.EXPRESSION)
|
|
layersValue = self.getParameterValue(self.LAYERS)
|
|
layersDict = {}
|
|
if layersValue:
|
|
layers = [QgsProcessingUtils.mapLayerFromString(f, context) for f in layersValue.split(";")]
|
|
layersDict = {os.path.basename(lyr.source().split(".")[0]): lyr for lyr in layers}
|
|
|
|
for lyr in QgsProcessingUtils.compatibleRasterLayers(QgsProject.instance()):
|
|
name = lyr.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
|
|
entries.append(entry)
|
|
|
|
output = self.getOutputValue(self.OUTPUT)
|
|
extentValue = self.getParameterValue(self.EXTENT)
|
|
if not extentValue:
|
|
extentValue = QgsProcessingUtils.combineLayerExtents(layersValue)
|
|
|
|
if extentValue:
|
|
extent = extentValue.split(',')
|
|
bbox = QgsRectangle(float(extent[0]), float(extent[2]),
|
|
float(extent[1]), float(extent[3]))
|
|
else:
|
|
if layersDict:
|
|
bbox = list(layersDict.values())[0].extent()
|
|
for lyr in layersDict.values():
|
|
bbox.combineExtentWith(lyr.extent())
|
|
else:
|
|
raise GeoAlgorithmExecutionException(self.tr("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,
|
|
output,
|
|
driverName,
|
|
bbox,
|
|
width,
|
|
height,
|
|
entries)
|
|
|
|
res = calc.processCalculation()
|
|
if res == QgsRasterCalculator.ParserError:
|
|
raise GeoAlgorithmExecutionException(self.tr("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(param.name) in expression:
|
|
values.append(ValueFromInput(param.name()))
|
|
|
|
if algorithm.name:
|
|
dependent = model.getDependentAlgorithms(algorithm.name)
|
|
else:
|
|
dependent = []
|
|
for alg in list(model.algs.values()):
|
|
if alg.modeler_name not in dependent:
|
|
for out in alg.algorithm.outputs:
|
|
if (isinstance(out, OutputRaster) and
|
|
"{}:{}@".format(alg.modeler_name, out.name) in expression):
|
|
values.append(ValueFromOutput(alg.modeler_name, out.name))
|
|
|
|
algorithm.params[self.LAYERS] = values
|