|
42 | 42 | QgsProcessingParameterString,
|
43 | 43 | QgsCoordinateTransform,
|
44 | 44 | QgsMapLayer)
|
| 45 | +from qgis.PyQt.QtCore import QObject |
45 | 46 | from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
|
46 | 47 |
|
47 | 48 |
|
@@ -134,7 +135,7 @@ def processAlgorithm(self, parameters, context, feedback):
|
134 | 135 | crs = list(layersDict.values())[0].crs()
|
135 | 136 |
|
136 | 137 | bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
|
137 |
| - if not bbox or bbox.isNull(): |
| 138 | + if bbox.isNull(): |
138 | 139 | if not layers:
|
139 | 140 | raise QgsProcessingException(self.tr("No reference layer selected nor extent box provided"))
|
140 | 141 |
|
@@ -231,63 +232,68 @@ def mappedNameToLayer(self, lyr, expression, layersDict, context):
|
231 | 232 |
|
232 | 233 | nameToMap = lyr.name()
|
233 | 234 |
|
234 |
| - # get last scope of the expressionContext because should be that related |
235 |
| - # with mapped variables |
236 |
| - # The scope name should be "algorithm_inputs" |
237 |
| - expContextLastScope = context.expressionContext().lastScope() |
238 |
| - |
239 | 235 | # check for layers directly added in the expression
|
240 | 236 | if (nameToMap + "@") in expression:
|
241 | 237 | layersDict[nameToMap] = lyr
|
242 | 238 |
|
243 |
| - # check for the layers that are mapped as input in a model |
244 |
| - # to do this check in the latest scope all passed variables |
245 |
| - # to look for a variable that is a layer or a string filename ç |
246 |
| - # to a layer |
247 |
| - varId = None |
248 |
| - varDescription = None |
249 |
| - |
250 |
| - for varName in expContextLastScope.variableNames(): |
251 |
| - |
252 |
| - layer = expContextLastScope.variable(varName) |
253 |
| - |
254 |
| - if not isinstance(layer, str) and not isinstance(layer, QgsMapLayer): |
255 |
| - continue |
256 |
| - |
257 |
| - if isinstance(layer, QgsMapLayer) and nameToMap not in layer.source(): |
258 |
| - continue |
259 |
| - |
260 |
| - varId = varName |
261 |
| - varDescription = expContextLastScope.description(varName) |
262 |
| - |
263 |
| - # because there can be variable with None or "" description |
264 |
| - # then skip them |
265 |
| - if not varDescription: |
266 |
| - continue |
267 |
| - |
268 |
| - # check if it's description starts with Output as in: |
269 |
| - # Output 'Output' from algorithm 'calc1' |
270 |
| - # as set in https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516 |
271 |
| - # but var in expression is called simply |
272 |
| - # 'Output' from algorithm 'calc1' |
273 |
| - elements = varDescription.split(" ") |
274 |
| - if len(elements) > 1 and elements[0] == "Output": |
275 |
| - # remove heading "Output " string |
276 |
| - varDescription = varDescription[7:] |
277 |
| - |
278 |
| - # check if cleaned varDescription is present in the expression |
279 |
| - # if not skip it |
280 |
| - if (varDescription + "@") not in expression: |
281 |
| - continue |
282 |
| - |
283 |
| - # !!!found!!! => substitute in expression |
284 |
| - # and add in the list of layers that will be passed to raster calculator |
285 |
| - nameToMap = varName |
286 |
| - new = "{}@".format(nameToMap) |
287 |
| - old = "{}@".format(varDescription) |
288 |
| - expression = expression.replace(old, new) |
289 |
| - |
290 |
| - layersDict[nameToMap] = lyr |
| 239 | + # get "algorithm_inputs" scope of the expressionContext related |
| 240 | + # with mapped variables |
| 241 | + indexOfScope = context.expressionContext().indexOfScope("algorithm_inputs") |
| 242 | + if indexOfScope >= 0: |
| 243 | + expContextAlgInputsScope = context.expressionContext().scope(indexOfScope) |
| 244 | + |
| 245 | + # check for the layers that are mapped as input in a model |
| 246 | + # to do this check in the latest scope all passed variables |
| 247 | + # to look for a variable that is a layer or a string filename ç |
| 248 | + # to a layer |
| 249 | + varDescription = None |
| 250 | + for varName in expContextAlgInputsScope.variableNames(): |
| 251 | + |
| 252 | + layer = expContextAlgInputsScope.variable(varName) |
| 253 | + |
| 254 | + if not isinstance(layer, str) and not isinstance(layer, QgsMapLayer): |
| 255 | + continue |
| 256 | + |
| 257 | + if isinstance(layer, QgsMapLayer) and nameToMap not in layer.source(): |
| 258 | + continue |
| 259 | + |
| 260 | + varDescription = expContextAlgInputsScope.description(varName) |
| 261 | + |
| 262 | + # because there can be variable with None or "" description |
| 263 | + # then skip them |
| 264 | + if not varDescription: |
| 265 | + continue |
| 266 | + |
| 267 | + # check if it's description starts with Output as in: |
| 268 | + # Output 'Output' from algorithm 'calc1' |
| 269 | + # as set in https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516 |
| 270 | + # but var in expression is called simply |
| 271 | + # 'Output' from algorithm 'calc1' |
| 272 | + |
| 273 | + # get the translatin string to use to parse the description |
| 274 | + # HAVE to use the same translated string as in |
| 275 | + # https://github.com/qgis/QGIS/blob/master/src/core/processing/models/qgsprocessingmodelalgorithm.cpp#L516 |
| 276 | + translatedDesc = self.tr("Output '%1' from algorithm '%2'") |
| 277 | + elementZero = translatedDesc.split(" ")[0] # For english the string result should be "Output" |
| 278 | + |
| 279 | + elements = varDescription.split(" ") |
| 280 | + if len(elements) > 1 and elements[0] == elementZero: |
| 281 | + # remove heading QObject.tr"Output ") string. Note adding a space at the end of elementZero! |
| 282 | + varDescription = varDescription[len(elementZero) + 1:] |
| 283 | + |
| 284 | + # check if cleaned varDescription is present in the expression |
| 285 | + # if not skip it |
| 286 | + if (varDescription + "@") not in expression: |
| 287 | + continue |
| 288 | + |
| 289 | + # !!!found!!! => substitute in expression |
| 290 | + # and add in the list of layers that will be passed to raster calculator |
| 291 | + nameToMap = varName |
| 292 | + new = "{}@".format(nameToMap) |
| 293 | + old = "{}@".format(varDescription) |
| 294 | + expression = expression.replace(old, new) |
| 295 | + |
| 296 | + layersDict[nameToMap] = lyr |
291 | 297 |
|
292 | 298 | # need return the modified expression because it's not a reference
|
293 | 299 | return expression
|
0 commit comments