From 5827513c4be92823418f81b0681a61e329272e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9d=C3=A9ric=20RIBREUX?= Date: Tue, 12 Sep 2017 21:58:12 +0200 Subject: [PATCH] First work on ext mechanism factorisation --- .../processing/algs/grass7/Grass7Algorithm.py | 67 ++++++++++++------- .../processing/algs/grass7/ext/r_rgb.py | 41 +++--------- 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/python/plugins/processing/algs/grass7/Grass7Algorithm.py b/python/plugins/processing/algs/grass7/Grass7Algorithm.py index 6f9ac38301f..fc2c48aa5b1 100644 --- a/python/plugins/processing/algs/grass7/Grass7Algorithm.py +++ b/python/plugins/processing/algs/grass7/Grass7Algorithm.py @@ -352,28 +352,35 @@ class Grass7Algorithm(QgsProcessingAlgorithm): self.grabDefaultGrassParameters(parameters, context) # Handle ext functions for inputs/command/outputs - if self.module: - if hasattr(self.module, 'processInputs'): - func = getattr(self.module, 'processInputs') - func(self) - else: - self.processInputs(parameters, context) + # if self.module: + # if hasattr(self.module, 'processInputs'): + # func = getattr(self.module, 'processInputs') + # func(self) + # else: + # self.processInputs(parameters, context) - if hasattr(self.module, 'processCommand'): - func = getattr(self.module, 'processCommand') - func(self) - else: - self.processCommand() + # if hasattr(self.module, 'processCommand'): + # func = getattr(self.module, 'processCommand') + # func(self) + # else: + # self.processCommand() - if hasattr(self.module, 'processOutputs'): - func = getattr(self.module, 'processOutputs') - func(self) + # if hasattr(self.module, 'processOutputs'): + # func = getattr(self.module, 'processOutputs') + # func(self) + # else: + # self.processOutputs() + # else: + # self.processInputs(parameters, context) + # self.processCommand(parameters, context) + # self.processOutputs(parameters, context) + # Handle ext methods for input/command/outputs + for fName in ['Inputs', 'Command', 'Outputs']: + fullName = 'process{}'.format(fName) + if self.module and hasattr(self.module, fullName): + getattr(self.module, fullName)(self, parameters, context) else: - self.processOutputs() - else: - self.processInputs(parameters, context) - self.processCommand(parameters, context) - self.processOutputs(parameters, context) + getattr(self, fullName)(parameters, context) # Run GRASS loglines = [] @@ -468,7 +475,12 @@ class Grass7Algorithm(QgsProcessingAlgorithm): # Add a vector layer elif layer.type() == QgsMapLayer.VectorLayer: self.commands.append(self.exportVectorLayer(layerName, layerSrc)) - + self.postInputs() + + def postInputs(self): + """ + After layer imports, we need to update some internal parameters + """ # If projection has not already be set, use the project self.setSessionProjectionFromProject() @@ -647,17 +659,22 @@ class Grass7Algorithm(QgsProcessingAlgorithm): self.outputCommands.append(command) QgsMessageLog.logMessage('processOutputs. Commands: {}'.format(self.commands), 'Grass7', QgsMessageLog.INFO) - def exportRasterLayer(self, layerKey, layerSrc): + def exportRasterLayer(self, layerKey, layerSrc, external=True, band=1): """ Creates a dedicated command to load a raster into temporary GRASS DB. + :layerKey: name of the parameter + :layerSrc: file path of raster layer + :external: use r.external (r.in.gdal if False). + :band: import only this band (if None, all bands are imported). """ - # TODO: handle multiple bands - #destFileName = QgsProcessingUtils.generateTempFilename(layerKey) destFilename = 'a' + os.path.basename(getTempFilename()) self.exportedLayers[layerKey] = destFilename - command = 'r.external input="{}" band=1 output={} --overwrite -o'.format( - layerSrc, destFilename) + command = '{0] input="{1}"{2}output={3} --overwrite -o'.format( + 'r.external' if external else 'r.in.gdal', + layerSrc, + ' band={} 'format(band) if band else '', + destFilename) return command def exportVectorLayer(self, layerKey, layerSrc): diff --git a/python/plugins/processing/algs/grass7/ext/r_rgb.py b/python/plugins/processing/algs/grass7/ext/r_rgb.py index d07a3b66465..afe5cd26cea 100644 --- a/python/plugins/processing/algs/grass7/ext/r_rgb.py +++ b/python/plugins/processing/algs/grass7/ext/r_rgb.py @@ -26,40 +26,15 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -def processInputs(alg): +def processInputs(alg, parameters, context): # We need to import all the bands and color tables of the input raster - raster = alg.getParameterValue('input') - if raster in list(alg.exportedLayers.keys()): + if 'input' in alg.exportedLayers: return - - alg.setSessionProjectionFromLayer(raster, alg.commands) - destFilename = alg.getTempFilename() - alg.exportedLayers[raster] = destFilename - command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename) - alg.commands.append(command) - - alg.setSessionProjectionFromProject(alg.commands) - - region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER)) - regionCoords = region.split(',') - command = 'g.region' - command += ' -a' - command += ' n=' + str(regionCoords[3]) - command += ' s=' + str(regionCoords[2]) - command += ' e=' + str(regionCoords[1]) - command += ' w=' + str(regionCoords[0]) - cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER) - if cellsize: - command += ' res=' + str(cellsize) - else: - command += ' res=' + str(alg.getDefaultCellsize(parameters, context)) - alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION) - if alignToResolution: - command += ' -a' - alg.commands.append(command) - - -def processCommand(alg, parameters): + raster = alg.parameterAsRasterLayer(parameters, 'input', context) + alg.setSessionProjectionFromLayer(raster) + alg.prepareInputs() + +def processCommand(alg, parameters, context): # We need to introduce something clever: # if the input raster is multiband: export each component directly raster = alg.exportedLayers[alg.getParameterValue('input')] @@ -77,7 +52,7 @@ def processCommand(alg, parameters): alg.commands.extend(commands) -def processOutputs(alg): +def processOutputs(alg, parameters, context): raster = alg.exportedLayers[alg.getParameterValue('input')] commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)] for color in ['red', 'green', 'blue']: