fix some code weakness and review comments

This commit is contained in:
Luigi Pirelli 2018-07-21 17:28:40 +01:00
parent 3a67c5d287
commit faf9716a52

View File

@ -42,6 +42,7 @@ from qgis.core import (QgsProcessing,
QgsProcessingParameterString, QgsProcessingParameterString,
QgsCoordinateTransform, QgsCoordinateTransform,
QgsMapLayer) QgsMapLayer)
from qgis.PyQt.QtCore import QObject
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
@ -134,7 +135,7 @@ class RasterCalculator(QgisAlgorithm):
crs = list(layersDict.values())[0].crs() crs = list(layersDict.values())[0].crs()
bbox = self.parameterAsExtent(parameters, self.EXTENT, context) bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
if not bbox or bbox.isNull(): if bbox.isNull():
if not layers: if not layers:
raise QgsProcessingException(self.tr("No reference layer selected nor extent box provided")) raise QgsProcessingException(self.tr("No reference layer selected nor extent box provided"))
@ -231,63 +232,68 @@ class RasterCalculator(QgisAlgorithm):
nameToMap = lyr.name() nameToMap = lyr.name()
# get last scope of the expressionContext because should be that related
# with mapped variables
# The scope name should be "algorithm_inputs"
expContextLastScope = context.expressionContext().lastScope()
# check for layers directly added in the expression # check for layers directly added in the expression
if (nameToMap + "@") in expression: if (nameToMap + "@") in expression:
layersDict[nameToMap] = lyr layersDict[nameToMap] = lyr
# check for the layers that are mapped as input in a model # get "algorithm_inputs" scope of the expressionContext related
# to do this check in the latest scope all passed variables # with mapped variables
# to look for a variable that is a layer or a string filename ç indexOfScope = context.expressionContext().indexOfScope("algorithm_inputs")
# to a layer if indexOfScope >= 0:
varId = None expContextAlgInputsScope = context.expressionContext().scope(indexOfScope)
varDescription = None
for varName in expContextLastScope.variableNames(): # check for the layers that are mapped as input in a model
# to do this check in the latest scope all passed variables
# to look for a variable that is a layer or a string filename ç
# to a layer
varDescription = None
for varName in expContextAlgInputsScope.variableNames():
layer = expContextLastScope.variable(varName) layer = expContextAlgInputsScope.variable(varName)
if not isinstance(layer, str) and not isinstance(layer, QgsMapLayer): if not isinstance(layer, str) and not isinstance(layer, QgsMapLayer):
continue continue
if isinstance(layer, QgsMapLayer) and nameToMap not in layer.source(): if isinstance(layer, QgsMapLayer) and nameToMap not in layer.source():
continue continue
varId = varName varDescription = expContextAlgInputsScope.description(varName)
varDescription = expContextLastScope.description(varName)
# because there can be variable with None or "" description # because there can be variable with None or "" description
# then skip them # then skip them
if not varDescription: if not varDescription:
continue continue
# check if it's description starts with Output as in: # check if it's description starts with Output as in:
# Output 'Output' from algorithm 'calc1' # Output 'Output' from algorithm 'calc1'
# as set in https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516 # as set in https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516
# but var in expression is called simply # but var in expression is called simply
# 'Output' from algorithm 'calc1' # 'Output' from algorithm 'calc1'
elements = varDescription.split(" ")
if len(elements) > 1 and elements[0] == "Output":
# remove heading "Output " string
varDescription = varDescription[7:]
# check if cleaned varDescription is present in the expression # get the translatin string to use to parse the description
# if not skip it # HAVE to use the same translated string as in
if (varDescription + "@") not in expression: # https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516
continue translatedDesc = self.tr("Output '%1' from algorithm '%2'")
elementZero = translatedDesc.split(" ")[0] # For english the string result should be "Output"
# !!!found!!! => substitute in expression elements = varDescription.split(" ")
# and add in the list of layers that will be passed to raster calculator if len(elements) > 1 and elements[0] == elementZero:
nameToMap = varName # remove heading QObject.tr"Output ") string. Note adding a space at the end of elementZero!
new = "{}@".format(nameToMap) varDescription = varDescription[len(elementZero) + 1:]
old = "{}@".format(varDescription)
expression = expression.replace(old, new)
layersDict[nameToMap] = lyr # check if cleaned varDescription is present in the expression
# if not skip it
if (varDescription + "@") not in expression:
continue
# !!!found!!! => substitute in expression
# and add in the list of layers that will be passed to raster calculator
nameToMap = varName
new = "{}@".format(nameToMap)
old = "{}@".format(varDescription)
expression = expression.replace(old, new)
layersDict[nameToMap] = lyr
# need return the modified expression because it's not a reference # need return the modified expression because it's not a reference
return expression return expression