diff --git a/python/plugins/processing/algs/grass7/Grass7Algorithm.py b/python/plugins/processing/algs/grass7/Grass7Algorithm.py index 01d44a39b48..11c7700873b 100644 --- a/python/plugins/processing/algs/grass7/Grass7Algorithm.py +++ b/python/plugins/processing/algs/grass7/Grass7Algorithm.py @@ -646,7 +646,7 @@ class Grass7Algorithm(QgsProcessingAlgorithm): layer = self.parameterAsRasterLayer(parameters, name, context) self.loadRasterLayer(name, layer, external, band) - def loadRasterLayer(self, name, layer, external=True, band=1): + def loadRasterLayer(self, name, layer, external=True, band=1, destName=None): """ Creates a dedicated command to load a raster into the temporary GRASS DB. @@ -654,18 +654,20 @@ class Grass7Algorithm(QgsProcessingAlgorithm): :param layer: QgsMapLayer for the raster layer. :param external: True if using r.external. :param band: imports only specified band. None for all bands. + :param destName: force the destination name of the raster. """ self.inputLayers.append(layer) self.setSessionProjectionFromLayer(layer) - destFilename = 'a' + os.path.basename(getTempFilename()) - self.exportedLayers[name] = destFilename + if not destName: + destName = 'rast_{}'.format(os.path.basename(getTempFilename())) + self.exportedLayers[name] = destName command = '{0} input="{1}" {2}output="{3}" --overwrite -o'.format( 'r.external' if external else 'r.in.gdal', os.path.normpath(layer.source()), 'band={} '.format(band) if band else '', - destFilename) + destName) self.commands.append(command) - + def exportRasterLayerFromParameter(self, name, parameters, context, colorTable=True): """ Creates a dedicated command to export a raster from @@ -714,7 +716,7 @@ class Grass7Algorithm(QgsProcessingAlgorithm): ) ) - def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True): + def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True, wholeDB=False): """ Creates a dedicated loop command to export rasters from temporary GRASS DB into a directory via gdal. @@ -722,11 +724,14 @@ class Grass7Algorithm(QgsProcessingAlgorithm): :param parameters: Algorithm parameters dict. :param context: Algorithm context. :param colorTable: preserve color Table. + :param wholeDB: export every raster layer from the GRASSDB """ # Grab directory name and temporary basename outDir = os.path.normpath( self.parameterAsString(parameters, name, context)) - basename = name + self.uniqueSuffix + basename = '' + if not wholeDB: + basename = name + self.uniqueSuffix # Add a loop export from the basename for cmd in [self.commands, self.outputCommands]: diff --git a/python/plugins/processing/algs/grass7/description/i.in.spotvgt.txt b/python/plugins/processing/algs/grass7/description/i.in.spotvgt.txt index 9c1840b8633..4854b4e0bce 100644 --- a/python/plugins/processing/algs/grass7/description/i.in.spotvgt.txt +++ b/python/plugins/processing/algs/grass7/description/i.in.spotvgt.txt @@ -1,6 +1,6 @@ i.in.spotvgt Imports SPOT VGT NDVI data into a raster map. Imagery (i.*) -QgsProcessingParameterRasterLayer|input|Name of input SPOT VGT NDVI HDF file|None|False +QgsProcessingParameterFile|input|Name of input SPOT VGT NDVI HDF file|False|hdf|None|False *QgsProcessingParameterBoolean|-a|Also import quality map (SM status map layer) and filter NDVI map|False QgsProcessingParameterRasterDestination|output|SPOT NDVI Raster diff --git a/python/plugins/processing/algs/grass7/ext/i.py b/python/plugins/processing/algs/grass7/ext/i.py index 73bd13e4822..6c039ca2ce9 100644 --- a/python/plugins/processing/algs/grass7/ext/i.py +++ b/python/plugins/processing/algs/grass7/ext/i.py @@ -25,153 +25,149 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from processing.core.parameters import getParameterFromString -from processing.tools.system import isWindows -from ..Grass7Utils import Grass7Utils -from os import path -from copy import deepcopy +import os +from processing.tools.system import (isWindows, getTempFilename) +from processing.algs.grass7.Grass7Utils import Grass7Utils +from qgis.core import QgsProcessingParameterString +from qgis.core import QgsMessageLog -def multipleOutputDir(alg, field, basename=None): +def orderedInput(alg, parameters, context, src, tgt, numSeq=None): + """Import multiple rasters in order + :param alg: algorithm object. + :param parameters: algorithm parameters dict. + :param context: algorithm context. + :param src: Name of the source parameter. + :param tgt: Name of a new input parameter. + :param numSeq: List of a sequence for naming layers. """ - Handle multiple output of rasters into a - directory. - """ - # We need to know where is the output directory - outputDir = alg.getOutputValue(field) + rootFilename = 'rast_{}.'.format(os.path.basename(getTempFilename())) + #parameters[tgt] = rootFilename + param = QgsProcessingParameterString(tgt, 'virtual input', + rootFilename, False, False) + alg.addParameter(param) - # We need to grab the variable basename - if basename: - commands = ["for r in $(g.list type=rast pattern='{}*'); do".format(basename)] - # Otherwise, export everything - else: - commands = ["for r in $(g.list type=rast); do"] - commands.append(" r.out.gdal -c -t -f input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\"".format(outputDir)) - commands.append("done") - alg.commands.extend(commands) - alg.outputCommands.extend(commands) - - -def orderedInput(alg, inputParameter, targetParameterDef, numSeq=None): - """Inport multiple rasters in the order""" - rasters = alg.getParameterValue(inputParameter).split(';') - # TODO: make targetParameter - inputParameter = getParameterFromString(targetParameterDef) - rootFilename = '{}_'.format(alg.getTempFilename()) - inputParameter.value = rootFilename - alg.addParameter(inputParameter) + rasters = alg.parameterAsLayerList(parameters, src, context) # Handle specific range if numSeq is None: numSeq = list(range(1, len(rasters) + 1)) + + for idx, raster in enumerate(rasters): + rasterName = '{}{}'.format(rootFilename, numSeq[idx]) + alg.loadRasterLayer(rasterName, raster, False, None, rasterName) - for idx in range(len(rasters)): - layer = rasters[idx] - if layer in list(alg.exportedLayers.keys()): - continue - else: - destFilename = '{}{}'.format(rootFilename, numSeq[idx]) - alg.setSessionProjectionFromLayer(layer, alg.commands) - alg.exportedLayers[layer] = destFilename - command = 'r.external input={} band=1 output={} --overwrite -o'.format(layer, destFilename) - alg.commands.append(command) + # Don't forget to remove the old input parameter + alg.removeParameter(src) + - 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) - return rootFilename - - -def regroupRasters(alg, parameters, field, groupField, subgroupField=None, extFile=None): +def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=None): """ Group multiple input rasters into a group * If there is a subgroupField, a subgroup will automatically be created. * When an external file is provided, the file is copied into the respective - directory of the subgroup. - * extFile is a dict of the form 'parameterName':'directory name'. + directory of the subgroup. :param parameters: + :param context: + :param src: name of input parameter with multiple rasters. + :param group: name of group. + :param subgroup: name of subgroup. + :param extFile: dict : parameterName:directory name """ - # List of rasters names + # Create a group parameter + groupName = 'group_{}'.format(os.path.basename(getTempFilename())) + param = QgsProcessingParameterString(group, 'virtual group', + groupName, False, False) + alg.addParameter(param) - new_parameters = deepcopy(parameters) + # Create a subgroup + subgroupName = None + if subgroup: + subgroupName = 'subgroup_{}'.format(os.path.basename(getTempFilename())) + param = QgsProcessingParameterString(subgroup, 'virtual subgroup', + subgroupName, False, False) + alg.addParameter(param) - rasters = alg.getParameterFromName(field) - rastersList = rasters.value.split(';') - del new_parameters[field] + # Compute raster names + rasters = alg.parameterAsLayerList(parameters, src, context) + rasterNames = [] + for idx, raster in enumerate(rasters): + name = '{}_{}'.format(src, idx) + if name in alg.exportedLayers: + rasterNames.append(alg.exportedLayers[name]) # Insert a i.group command - group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField)) - new_parameters[group.name()] = alg.getTempFilename() - - if subgroupField: - subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField)) - new_parameters[subgroup.name()] = alg.getTempFilename() - command = 'i.group group={}{} input={}'.format( - new_parameters[group.name()], - ' subgroup={}'.format(new_parameters[subgroup.name()]) if subgroupField else '', - ','.join([alg.exportedLayers[f] for f in rastersList]) - ) + groupName, + ' subgroup={}'.format(subgroupName) if subgroup else '', + ','.join(rasterNames)) alg.commands.append(command) # Handle external files - if subgroupField and extFile: - for ext in list(extFile.keys()): - extFileName = new_parameters[ext] - if extFileName: - shortFileName = path.basename(extFileName) - destPath = path.join(Grass7Utils.grassMapsetFolder(), - 'PERMANENT', - 'group', new_parameters[group.name()], - 'subgroup', new_parameters[subgroup.name()], - extFile[ext], shortFileName) - copyFile(alg, extFileName, destPath) - new_parameters[ext] = shortFileName + # if subgroupField and extFile: + # for ext in extFile.keys(): + # extFileName = new_parameters[ext] + # if extFileName: + # shortFileName = os.path.basename(extFileName) + # destPath = os.path.join(Grass7Utils.grassMapsetFolder(), + # 'PERMANENT', + # 'group', new_parameters[group.name()], + # 'subgroup', new_parameters[subgroup.name()], + # extFile[ext], shortFileName) + # copyFile(alg, extFileName, destPath) + - # modify parameters values - alg.processCommand(new_parameters) - - return group.value + alg.removeParameter(src) + + return groupName, subgroupName -def exportInputRasters(alg, rasterDic): +def importSigFile(alg, group, subgroup, src, sigDir='sig'): + """ + Import a signature file into an + internal GRASSDB folder + """ + shortSigFile = os.path.basename(src) + interSig = os.path.join(Grass7Utils.grassMapsetFolder(), + 'PERMANENT', 'group', group, 'subgroup', + subgroup, sigDir, shortSigFile) + copyFile(alg, src, interSig) + return shortSigFile + + +def exportSigFile(alg, group, subgroup, dest, sigDir='sig'): + """ + Export a signature file from internal GRASSDB + to final destination + """ + shortSigFile = os.path.basename(dest) + interSig = os.path.join(Grass7Utils.grassMapsetFolder(), + 'PERMANENT', 'group', group, 'subgroup', + subgroup, sigDir, shortSigFile) + moveFile(alg, interSig, dest) + return interSig + + +def exportInputRasters(alg, parameters, context, rasterDic): """ Export input rasters Use a dict to make input/output link: { 'inputName1': 'outputName1', 'inputName2': 'outputName2'} """ + createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) + metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) + # Get inputs and outputs - for inputName, outputName in list(rasterDic.items()): - inputRaster = alg.getParameterValue(inputName) - outputRaster = alg.getOutputFromName(outputName) - command = 'r.out.gdal -c -t -f --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format( - alg.exportedLayers[inputRaster], - outputRaster.value - ) - alg.commands.append(command) - alg.outputCommands.append(command) + for inputName, outputName in rasterDic.items(): + fileName = os.path.normpath( + alg.parameterAsOutputLayer(parameters, outputName, context)) + grassName = alg.exportedLayers[inputName] + outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) + alg.exportRasterLayer(grassName, fileName, True, outFormat, createOpt, metaOpt) def verifyRasterNum(alg, parameters, context, rasters, mini, maxi=None): - """Verify if we have at least n rasters in multipleInput""" - num = len(alg.parameterAsStrings(rasters).split(';')) + """Verify that we have at least n rasters in multipleInput""" + num = len(alg.parameterAsLayerList(parameters, rasters, context)) if num < mini: return 'You need to set at least {} input rasters for this algorithm!'.format(mini) if maxi and num > maxi: @@ -179,18 +175,18 @@ def verifyRasterNum(alg, parameters, context, rasters, mini, maxi=None): return None -def file2Output(alg, output): - """Transform an OutputFile to a parameter""" - # Get the outputFile - outputFile = alg.getOutputFromName(output) - alg.removeOutputFromName(output) +# def file2Output(alg, output): +# """Transform an OutputFile to a parameter""" +# # Get the outputFile +# outputFile = alg.getOutputFromName(output) +# alg.removeOutputFromName(output) - # Create output parameter - param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) - param.value = outputFile.value - alg.addParameter(param) +# # Create output parameter +# param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) +# param.value = outputFile.value +# alg.addParameter(param) - return outputFile +# return outputFile def createDestDir(alg, toFile): @@ -198,7 +194,7 @@ def createDestDir(alg, toFile): # Creates the destination directory command = "{} {}".format( "MD" if isWindows() else "mkdir -p", - path.dirname(toFile) + os.path.dirname(toFile) ) alg.commands.append(command) diff --git a/python/plugins/processing/algs/grass7/ext/i_albedo.py b/python/plugins/processing/algs/grass7/ext/i_albedo.py index 3cdca18c3b5..dfc37e0587a 100644 --- a/python/plugins/processing/algs/grass7/ext/i_albedo.py +++ b/python/plugins/processing/algs/grass7/ext/i_albedo.py @@ -28,11 +28,12 @@ __revision__ = '$Format:%H$' from .i import verifyRasterNum -def checkParameterValuesBeforeExecuting(alg): - if alg.getParameterValue('-m'): - return verifyRasterNum(alg, 'input', 7) - elif alg.getParameterValue('-n'): - return verifyRasterNum(alg, 'input', 2) - elif alg.getParameterValue('-l') or alg.getParameterValue('-a'): - return verifyRasterNum(alg, 'input', 6) +def checkParameterValuesBeforeExecuting(alg, parameters, context): + if alg.parameterAsBool(parameters, '-m', context): + return verifyRasterNum(alg, parameters, context, 'input', 7) + elif alg.parameterAsBool(parameters, '-n', context): + return verifyRasterNum(alg, parameters, context, 'input', 2) + elif (alg.parameterAsBool(parameters, '-l', context) + or alg.parameterAsBool(parameters, '-a', context)): + return verifyRasterNum(alg, parameters, context, 'input', 6) return None diff --git a/python/plugins/processing/algs/grass7/ext/i_cca.py b/python/plugins/processing/algs/grass7/ext/i_cca.py index 1e3ade8f9bf..e403f779b76 100644 --- a/python/plugins/processing/algs/grass7/ext/i_cca.py +++ b/python/plugins/processing/algs/grass7/ext/i_cca.py @@ -25,7 +25,7 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import verifyRasterNum, regroupRasters +from .i import verifyRasterNum, regroupRasters, importSigFile def checkParameterValuesBeforeExecuting(alg, parameters, context): @@ -34,7 +34,12 @@ def checkParameterValuesBeforeExecuting(alg, parameters, context): def processCommand(alg, parameters, context): # Regroup rasters - regroupRasters(alg, parameters, 'input', 'group', 'subgroup', {'signature': 'sig'}) + group, subgroup = regroupRasters(alg, parameters, context, + 'input', 'group', 'subgroup') + signatureFile = alg.parameterAsString(parameters, 'signature', context) + shortSigFile = importSigFile(alg, group, subgroup, signatureFile) + parameters['signature'] = shortSigFile + # Handle other parameters - alg.processCommand(alg, parameters, context) + alg.processCommand(parameters, context) diff --git a/python/plugins/processing/algs/grass7/ext/i_cluster.py b/python/plugins/processing/algs/grass7/ext/i_cluster.py index 237cb65ad82..a814f59bc7b 100644 --- a/python/plugins/processing/algs/grass7/ext/i_cluster.py +++ b/python/plugins/processing/algs/grass7/ext/i_cluster.py @@ -25,34 +25,26 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import regroupRasters, file2Output, moveFile, verifyRasterNum -from os import path -from ..Grass7Utils import Grass7Utils +import os +from .i import regroupRasters, verifyRasterNum, exportSigFile + +def checkParameterValuesBeforeExecuting(alg, parameters, context): + return verifyRasterNum(alg, parameters, context, 'input', 2) -def checkParameterValuesBeforeExecuting(alg): - return verifyRasterNum(alg, 'input', 2) - - -def processCommand(alg, parameters): +def processCommand(alg, parameters, context): # We need to extract the basename of the signature file - signatureFile = alg.getOutputFromName('signaturefile') - origSigFile = signatureFile.value - shortSigFile = path.basename(origSigFile) - alg.setOutputValue('signaturefile', shortSigFile) - - # Transform output files in string parameters - signatureFile = file2Output(alg, 'signaturefile') - reportFile = file2Output(alg, 'reportfile') + signatureFile = alg.parameterAsString(parameters, 'signaturefile', context) + shortSigFile = os.path.basename(signatureFile) + parameters['signaturefile'] = shortSigFile # Regroup rasters - group, subgroup = regroupRasters(alg, parameters, 'input', 'group', 'subgroup') - + group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup') + alg.processCommand(parameters, context) + # Re-add signature files - alg.addOutput(signatureFile) - alg.addOutput(reportFile) + parameters['signaturefile'] = signatureFile + + # Export signature file + exportSigFile(alg, group, subgroup, signatureFile) - # Find Grass directory - interSig = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group, 'subgroup', subgroup, 'sig', shortSigFile) - moveFile(alg, interSig, origSigFile) - alg.setOutputValue('signaturefile', origSigFile) diff --git a/python/plugins/processing/algs/grass7/ext/i_colors_enhance.py b/python/plugins/processing/algs/grass7/ext/i_colors_enhance.py index 1914c62b158..a6c18be9fb9 100644 --- a/python/plugins/processing/algs/grass7/ext/i_colors_enhance.py +++ b/python/plugins/processing/algs/grass7/ext/i_colors_enhance.py @@ -28,21 +28,12 @@ __revision__ = '$Format:%H$' from .i import exportInputRasters -def processCommand(alg, parameters): - +def processCommand(alg, parameters, context): # Temporary remove outputs: - outputs = [alg.getOutputFromName('{}output'.format(f)) for f in ['red', 'green', 'blue']] - for out in outputs: - alg.removeOutputFromName(out.name) - - alg.processCommand(parameters) - - # Re-add outputs - for output in outputs: - alg.addOutput(output) + alg.processCommand(parameters, context, True) -def processOutputs(alg): +def processOutputs(alg, parameters, context): # Input rasters are output rasters rasterDic = {'red': 'redoutput', 'green': 'greenoutput', 'blue': 'blueoutput'} - exportInputRasters(alg, rasterDic) + exportInputRasters(alg, parameters, context, rasterDic) diff --git a/python/plugins/processing/algs/grass7/ext/i_evapo_mh.py b/python/plugins/processing/algs/grass7/ext/i_evapo_mh.py index 00b9f012a06..9179b3610b1 100644 --- a/python/plugins/processing/algs/grass7/ext/i_evapo_mh.py +++ b/python/plugins/processing/algs/grass7/ext/i_evapo_mh.py @@ -26,9 +26,11 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -def checkParameterValuesBeforeExecuting(alg): - if alg.getParameterValue('-h') and alg.getParameterValue('precipitation'): +def checkParameterValuesBeforeExecuting(alg, parameters, context): + if (alg.parameterAsBool(parameters, '-h', context) + and alg.parameterAsLayer(parameters, 'precipitation', context)): return alg.tr('You can\'t use original Hargreaves flag and precipitation parameter together!') - if not alg.getParameterValue('-h') and not alg.getParameterValue('precipitation'): + if (not alg.parameterAsBool(parameters, '-h', context) + and not alg.parameterAsLayer(parameters, 'precipitation', context)): return alg.tr('If you don\'t use original Hargreaves flag, you must set the precipitation raster parameter!') return None diff --git a/python/plugins/processing/algs/grass7/ext/i_gensig.py b/python/plugins/processing/algs/grass7/ext/i_gensig.py index 9e1ae84866c..85cf7d33357 100644 --- a/python/plugins/processing/algs/grass7/ext/i_gensig.py +++ b/python/plugins/processing/algs/grass7/ext/i_gensig.py @@ -25,27 +25,22 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import regroupRasters, file2Output, moveFile -from os import path -from ..Grass7Utils import Grass7Utils +import os +from .i import regroupRasters, exportSigFile - -def processCommand(alg, parameters): - # Transform output files in string parameter - signatureFile = alg.getOutputFromName('signaturefile') - origSigFile = signatureFile.value - shortSigFile = path.basename(origSigFile) - alg.setOutputValue('signaturefile', shortSigFile) - - signatureFile = file2Output(alg, 'signaturefile') +def processCommand(alg, parameters, context): + # We need to extract the basename of the signature file + signatureFile = alg.parameterAsString(parameters, 'signaturefile', context) + shortSigFile = os.path.basename(signatureFile) + parameters['signaturefile'] = shortSigFile # Regroup rasters - group, subgroup = regroupRasters(alg, parameters, 'input', 'group', 'subgroup') - + group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup') + alg.processCommand(parameters, context) + # Re-add signature files - alg.addOutput(signatureFile) + parameters['signaturefile'] = signatureFile + + # Export signature file + exportSigFile(alg, group, subgroup, signatureFile) - # Find Grass directory - interSig = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group, 'subgroup', subgroup, 'sig', shortSigFile) - moveFile(alg, interSig, origSigFile) - alg.setOutputValue('signaturefile', origSigFile) diff --git a/python/plugins/processing/algs/grass7/ext/i_gensigset.py b/python/plugins/processing/algs/grass7/ext/i_gensigset.py index 93a33382060..5fa296e8f38 100644 --- a/python/plugins/processing/algs/grass7/ext/i_gensigset.py +++ b/python/plugins/processing/algs/grass7/ext/i_gensigset.py @@ -25,27 +25,22 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import regroupRasters, file2Output, moveFile -from os import path -from ..Grass7Utils import Grass7Utils +import os +from .i import regroupRasters, exportSigFile - -def processCommand(alg, parameters): - # Transform output files in string parameter - signatureFile = alg.getOutputFromName('signaturefile') - origSigFile = signatureFile.value - shortSigFile = path.basename(origSigFile) - alg.setOutputValue('signaturefile', shortSigFile) - - signatureFile = file2Output(alg, 'signaturefile') +def processCommand(alg, parameters, context): + # We need to extract the basename of the signature file + signatureFile = alg.parameterAsString(parameters, 'signaturefile', context) + shortSigFile = os.path.basename(signatureFile) + parameters['signaturefile'] = shortSigFile # Regroup rasters - group, subgroup = regroupRasters(alg, parameters, 'input', 'group', 'subgroup') - + group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup') + alg.processCommand(parameters, context) + # Re-add signature files - alg.addOutput(signatureFile) + parameters['signaturefile'] = signatureFile + + # Export signature file + exportSigFile(alg, group, subgroup, signatureFile, 'sigset') - # Find Grass directory - interSig = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group, 'subgroup', subgroup, 'sigset', shortSigFile) - moveFile(alg, interSig, origSigFile) - alg.setOutputValue('signaturefile', origSigFile) diff --git a/python/plugins/processing/algs/grass7/ext/i_group.py b/python/plugins/processing/algs/grass7/ext/i_group.py index 9ebe69deeba..dd16ef99b0c 100644 --- a/python/plugins/processing/algs/grass7/ext/i_group.py +++ b/python/plugins/processing/algs/grass7/ext/i_group.py @@ -28,5 +28,5 @@ __revision__ = '$Format:%H$' from .i import verifyRasterNum -def checkParameterValuesBeforeExecuting(alg): - return verifyRasterNum(alg, 'input', 2) +def checkParameterValuesBeforeExecuting(alg, parameters, context): + return verifyRasterNum(alg, parameters, context, 'input', 2) diff --git a/python/plugins/processing/algs/grass7/ext/i_in_spotvgt.py b/python/plugins/processing/algs/grass7/ext/i_in_spotvgt.py index e3293d24253..d11a0fe1a26 100644 --- a/python/plugins/processing/algs/grass7/ext/i_in_spotvgt.py +++ b/python/plugins/processing/algs/grass7/ext/i_in_spotvgt.py @@ -26,8 +26,8 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -def processInputs(alg): +def processInputs(alg, parameters, context): # Here, we apply directly the algorithm # So we just need to get the projection of the layer ! - layer = alg.getParameterValue('input') - alg.setSessionProjectionFromLayer(layer, alg.commands) + layer = alg.parameterAsRasterLayer(parameters, 'input', context) + alg.setSessionProjectionFromLayer(layer) diff --git a/python/plugins/processing/algs/grass7/ext/i_landsat_acca.py b/python/plugins/processing/algs/grass7/ext/i_landsat_acca.py index 618d374dec5..c5bb65cf196 100644 --- a/python/plugins/processing/algs/grass7/ext/i_landsat_acca.py +++ b/python/plugins/processing/algs/grass7/ext/i_landsat_acca.py @@ -28,22 +28,14 @@ __revision__ = '$Format:%H$' from .i import verifyRasterNum, orderedInput -def checkParameterValuesBeforeExecuting(alg): - return verifyRasterNum(alg, 'rasters', 5, 5) +def checkParameterValuesBeforeExecuting(alg, parameters, context): + return verifyRasterNum(alg, parameters, context, 'rasters', 5, 5) -def processInputs(alg): - orderedInput(alg, 'rasters', - "ParameterString|input|Base name of input raster bands|None|False|False", +def processInputs(alg, parameters, context): + orderedInput(alg, parameters, context, 'rasters', 'input', [2, 3, 4, 5, 61]) -def processCommand(alg, parameters): - # Remove rasters parameter - rasters = alg.getParameterFromName('rasters') - alg.parameters.remove(rasters) - - alg.processCommand() - - # re-add rasters - alg.addParameter(rasters) +def processCommand(alg, parameters, context): + alg.processCommand(parameters, context) diff --git a/python/plugins/processing/algs/grass7/ext/i_landsat_toar.py b/python/plugins/processing/algs/grass7/ext/i_landsat_toar.py index 00e3a9f80d1..3455cb99919 100644 --- a/python/plugins/processing/algs/grass7/ext/i_landsat_toar.py +++ b/python/plugins/processing/algs/grass7/ext/i_landsat_toar.py @@ -26,7 +26,6 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' from .i import verifyRasterNum, orderedInput -from processing.core.parameters import getParameterFromString def checkParameterValuesBeforeExecuting(alg, parameters, context): @@ -34,12 +33,9 @@ def checkParameterValuesBeforeExecuting(alg, parameters, context): def processInputs(alg, parameters, context): - orderedInput(alg, 'rasters', - "ParameterString|input|Base name of input raster bands|None|False|False", + orderedInput(alg, parameters, context, 'rasters', 'input', [1, 2, 3, 4, 5, 61, 62, 7, 8]) def processCommand(alg, parameters, context): - # Remove rasters parameter - rasters = alg.getParameterFromName('rasters') - alg.parameters.remove(rasters) + alg.processCommand(parameters, context) diff --git a/python/plugins/processing/algs/grass7/ext/i_maxlik.py b/python/plugins/processing/algs/grass7/ext/i_maxlik.py index aaa3f51ec0d..556cb7205e1 100644 --- a/python/plugins/processing/algs/grass7/ext/i_maxlik.py +++ b/python/plugins/processing/algs/grass7/ext/i_maxlik.py @@ -25,9 +25,18 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import regroupRasters +from .i import regroupRasters, importSigFile -def processCommand(alg, parameters): - # Regroup rasters - regroupRasters(alg, parameters, 'input', 'group', 'subgroup', {'signaturefile': 'sig'}) +def processCommand(alg, parameters, context): + group, subgroup = regroupRasters(alg, parameters, context, + 'input', 'group', 'subgroup') + + # import signature + signatureFile = alg.parameterAsString(parameters, 'signaturefile', context) + shortSigFile = importSigFile(alg, group, subgroup, signatureFile) + parameters['signaturefile'] = shortSigFile + + # Handle other parameters + alg.processCommand(parameters, context) + diff --git a/python/plugins/processing/algs/grass7/ext/i_oif.py b/python/plugins/processing/algs/grass7/ext/i_oif.py index 67131e7310b..25b49e619b6 100644 --- a/python/plugins/processing/algs/grass7/ext/i_oif.py +++ b/python/plugins/processing/algs/grass7/ext/i_oif.py @@ -25,18 +25,9 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import file2Output, verifyRasterNum +from .i import verifyRasterNum -def checkParameterValuesBeforeExecuting(alg): - return verifyRasterNum(alg, 'input', 4) +def checkParameterValuesBeforeExecuting(alg, parameters, context): + return verifyRasterNum(alg, parameters, context, 'input', 4) - -def processCommand(alg, parameters): - # Transform output file in string parameter - oifFile = file2Output(alg, 'output') - - alg.processCommand() - - # Re-add output file - alg.addOutput(oifFile) diff --git a/python/plugins/processing/algs/grass7/ext/i_pansharpen.py b/python/plugins/processing/algs/grass7/ext/i_pansharpen.py index 209a94f611b..7cdb7a1e381 100644 --- a/python/plugins/processing/algs/grass7/ext/i_pansharpen.py +++ b/python/plugins/processing/algs/grass7/ext/i_pansharpen.py @@ -25,33 +25,28 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from processing.core.parameters import getParameterFromString +import os +from processing.tools.system import getTempFilename +from processing.algs.grass7.Grass7Utils import Grass7Utils +from qgis.core import QgsProcessingParameterString + +def processCommand(alg, parameters, context): + # Temporary remove outputs and add a virtual output parameter + outputName = 'output_{}'.format(os.path.basename(getTempFilename())) + param = QgsProcessingParameterString('output', 'virtual output', + outputName, False, False) + alg.addParameter(param) + alg.processCommand(parameters, context, True) -def processCommand(alg, parameters): - - # Temporary remove outputs: - outputs = [alg.getOutputFromName('{}output'.format(f)) for f in ['red', 'green', 'blue']] - for out in outputs: - alg.removeOutputFromName(out.name) - - # create a false output - base = getParameterFromString('ParameterString|output|Name for output basename raster map(s)|None|False|False') - base.value = alg.getTempFilename() - alg.addParameter(base) - alg.processCommand() - - # Re-add outputs - for output in outputs: - alg.addOutput(output) - - -def processOutputs(alg): - base = alg.getParameterValue('output') +def processOutputs(alg, parameters, context): + outputName = alg.parameterAsString(parameters, 'output', context) + createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) + metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) for channel in ['red', 'green', 'blue']: - command = 'r.out.gdal -c -t -f --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format( - '{}_{}'.format(base, channel), - alg.getOutputValue('{}output'.format(channel)) - ) - alg.commands.append(command) - alg.outputCommands.append(command) + fileName = alg.parameterAsOutputLayer(parameters, '{}output'.format(channel), context) + grassName = '{}_{}'.format(outputName, channel) + outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) + alg.exportRasterLayer(grassName, fileName, True, outFormat, createOpt, metaOpt) + + diff --git a/python/plugins/processing/algs/grass7/ext/i_segment.py b/python/plugins/processing/algs/grass7/ext/i_segment.py index 79797617c71..97340859f53 100644 --- a/python/plugins/processing/algs/grass7/ext/i_segment.py +++ b/python/plugins/processing/algs/grass7/ext/i_segment.py @@ -28,6 +28,7 @@ __revision__ = '$Format:%H$' from .i import regroupRasters -def processCommand(alg, parameters): +def processCommand(alg, parameters, context): # Regroup rasters - regroupRasters(alg, parameters, 'input', 'group') + regroupRasters(alg, parameters, context, 'input', 'group') + alg.processCommand(parameters, context) diff --git a/python/plugins/processing/algs/grass7/ext/i_smap.py b/python/plugins/processing/algs/grass7/ext/i_smap.py index 61d47f51435..c66551c0ba0 100644 --- a/python/plugins/processing/algs/grass7/ext/i_smap.py +++ b/python/plugins/processing/algs/grass7/ext/i_smap.py @@ -25,9 +25,18 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from .i import regroupRasters +from .i import regroupRasters, importSigFile -def processCommand(alg, parameters): +def processCommand(alg, parameters, context): # Regroup rasters - regroupRasters(alg, parameters, 'input', 'group', 'subgroup', {'signaturefile': 'sigset'}) + group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup') + + # import signature + signatureFile = alg.parameterAsString(parameters, 'signaturefile', context) + shortSigFile = importSigFile(alg, group, subgroup, signatureFile, 'sigset') + parameters['signaturefile'] = shortSigFile + + # Handle other parameters + alg.processCommand(parameters, context) + diff --git a/python/plugins/processing/algs/grass7/ext/r_mapcalc.py b/python/plugins/processing/algs/grass7/ext/r_mapcalc.py index 587c1023ef8..3a6f7543474 100644 --- a/python/plugins/processing/algs/grass7/ext/r_mapcalc.py +++ b/python/plugins/processing/algs/grass7/ext/r_mapcalc.py @@ -58,11 +58,7 @@ def processCommand(alg, parameters, context): def processOutputs(alg, parameters, context): # We need to export every raster from the GRASSDB - outputDir = alg.parameterAsString(parameters, 'output_dir', context) - # TODO Windows support - # TODO Format/options support - commands = ["for r in $(g.list type=rast); do"] - commands.append(" r.out.gdal --overwrite -c -t -f input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\"".format(outputDir)) - commands.append("done") - alg.commands.extend(commands) + alg.exportRasterLayersIntoDirectory('output_dir', + parameters, context, + wholeDB=True)