diff --git a/python/plugins/processing/algs/grass7/Grass7Algorithm.py b/python/plugins/processing/algs/grass7/Grass7Algorithm.py index d1e67eaaf98..5629ace629d 100644 --- a/python/plugins/processing/algs/grass7/Grass7Algorithm.py +++ b/python/plugins/processing/algs/grass7/Grass7Algorithm.py @@ -56,6 +56,7 @@ from qgis.core import (QgsRasterLayer, QgsProcessingParameterVectorDestination, QgsProcessingParameterRasterDestination, QgsProcessingParameterFileDestination, + QgsProcessingParameterFile, QgsProcessingParameterFolderDestination, QgsProcessingOutputFolder, QgsProcessingOutputVectorLayer, @@ -543,6 +544,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm): value = ','.join( self.parameterAsFields(parameters, paramName, context) ) + elif isinstance(param, QgsProcessingParameterFile): + value = self.parameterAsString(parameters, paramName, context) # For numbers and points, we translate as a string elif isinstance(param, (QgsProcessingParameterNumber, QgsProcessingParameterPoint)): @@ -694,8 +697,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm): # Adjust region to layer before exporting cmd.append('g.region raster={}'.format(grassName)) cmd.append( - 'r.out.gdal -c -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format( - ' -t' if colorTable else '', + 'r.out.gdal -t -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format( + '' if colorTable else ' -c', grassName, fileName, outFormat, ' createopt="{}"'.format(createOpt) if createOpt else '', @@ -719,8 +722,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm): # Add a loop export from the basename for cmd in [self.commands, self.outputCommands]: - # Adjust region to layer before exporting - # TODO: Does-it works under MS-Windows or MacOSX? + # TODO Windows support + # TODO Format/options support cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename)) cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format( ' -t' if colorTable else '', outDir, diff --git a/python/plugins/processing/algs/grass7/TODO.md b/python/plugins/processing/algs/grass7/TODO.md index 076eb14dd0d..b2ed076447b 100644 --- a/python/plugins/processing/algs/grass7/TODO.md +++ b/python/plugins/processing/algs/grass7/TODO.md @@ -7,8 +7,6 @@ QGIS3 Processing Port * Review i.py. * Use a message band rather than a messageBox for errors on exclusives parameters. * r_rgb.py - * r_blend_combine.py - * r_blend_rgb.py * r_horizon.py * r_mask.py * r_mask_vect.py diff --git a/python/plugins/processing/algs/grass7/description/r.colors.txt b/python/plugins/processing/algs/grass7/description/r.colors.txt index f7cc459967c..2d076398388 100644 --- a/python/plugins/processing/algs/grass7/description/r.colors.txt +++ b/python/plugins/processing/algs/grass7/description/r.colors.txt @@ -2,7 +2,7 @@ r.colors Creates/modifies the color table associated with a raster map. Raster (r.*) QgsProcessingParameterMultipleLayers|map|Name of raster maps(s)|3|None|False -QgsProcessingParameterEnum|color|Name of color table|not selected;aspect;aspectcolr;bcyr;bgyr;blues;byg;byr;celsius;corine;curvature;differences;elevation;etopo2;evi;fahrenheit;gdd;greens;grey;grey.eq;grey.log;grey1.0;grey255;gyr;haxby;kelvin;ndvi;ndwi;oranges;population;population_dens;precipitation;precipitation_daily;precipitation_monthly;rainbow;ramp;random;reds;rstcurv;ryb;ryg;sepia;slope;srtm;srtm_plus;terrain;wave|False|0|False +QgsProcessingParameterEnum|color|Name of color table|not selected;aspect;aspectcolr;bcyr;bgyr;blues;byg;byr;celsius;corine;curvature;differences;elevation;etopo2;evi;fahrenheit;gdd;greens;grey;grey.eq;grey.log;grey1.0;grey255;gyr;haxby;kelvin;ndvi;ndwi;oranges;population;population_dens;precipitation;precipitation_daily;precipitation_monthly;rainbow;ramp;random;reds;rstcurv;ryb;ryg;sepia;slope;srtm;srtm_plus;terrain;wave|False|0|True QgsProcessingParameterString|rules_txt|Color rules|None|True|True QgsProcessingParameterFile|rules|Color rules file|False|txt|None|True QgsProcessingParameterRasterLayer|raster|Raster map from which to copy color table|None|True @@ -12,4 +12,4 @@ QgsProcessingParameterBoolean|-n|Invert colors|False QgsProcessingParameterBoolean|-g|Logarithmic scaling|False QgsProcessingParameterBoolean|-a|Logarithmic-absolute scaling|False QgsProcessingParameterBoolean|-e|Histogram equalization|False -QgsProcessingParameterFolderDestination|output_dir|Output Directory +QgsProcessingParameterFolderDestination|output_dir|Output Directory|None|False diff --git a/python/plugins/processing/algs/grass7/description/r.series.interp.txt b/python/plugins/processing/algs/grass7/description/r.series.interp.txt index 39a086c0b80..ef5e8eeacec 100644 --- a/python/plugins/processing/algs/grass7/description/r.series.interp.txt +++ b/python/plugins/processing/algs/grass7/description/r.series.interp.txt @@ -6,6 +6,6 @@ QgsProcessingParameterString|datapos|Data point position for each input map|None QgsProcessingParameterFile|infile|Input file with one input raster map name and data point position per line, field separator between name and sample point is 'pipe'|False|txt|None|True QgsProcessingParameterString|output|Name for output raster map (comma separated list if multiple)|None|False|True QgsProcessingParameterString|samplingpos|Sampling point position for each output map (comma separated list)|None|True|True -QgsProcessingParameterFile|outfile|Input file with one output raster map name and sample point position per line, field separator between name and sample point is 'pipe'|False|txt|NoneTrue +QgsProcessingParameterFile|outfile|Input file with one output raster map name and sample point position per line, field separator between name and sample point is 'pipe'|False|txt|None|True QgsProcessingParameterEnum|method|Interpolation method, currently only linear interpolation is supported|linear|False|0|True -QgsProcessingParameterFolderDestination|output_dir|Interpolated rasters +QgsProcessingParameterFolderDestination|output_dir|Interpolated rasters|None|False diff --git a/python/plugins/processing/algs/grass7/description/r.support.txt b/python/plugins/processing/algs/grass7/description/r.support.txt deleted file mode 100644 index 2b897771811..00000000000 --- a/python/plugins/processing/algs/grass7/description/r.support.txt +++ /dev/null @@ -1,14 +0,0 @@ -r.support -Allows creation and/or modification of raster map layer support files (metadata). -Raster (r.*) -QgsProcessingParameterRasterLayer|map|Name of raster map|None|False -QgsProcessingParameterString|title|Title for resultant raster map|None|False|True -QgsProcessingParameterString|timestamp|r.timestamp date: Datetime, datetime1/datetime2, or 'none' to remove'|None|False|True -QgsProcessingParameterString|history|Text to append to the next line of the map's metadata file|None|True|True -QgsProcessingParameterString|units|Text to use for map data units|None|False|True -QgsProcessingParameterString|vdatum|Text to use for map vertical datum|None|False|True -QgsProcessingParameterString|source1|Text to use for data source, line 1|None|False|True -QgsProcessingParameterString|source2|Text to use for data source, line 2|None|False|True -QgsProcessingParameterString|description|Text to use for data description or keyword(s)|None|False|True -*QgsProcessingParameterFile|loadhistory|Text file from which to load history|0|txt|None|True -QgsProcessingParameterRasterDestination|output|Metadata diff --git a/python/plugins/processing/algs/grass7/ext/r_category.py b/python/plugins/processing/algs/grass7/ext/r_category.py index cb18dca64b6..e348fea4be8 100644 --- a/python/plugins/processing/algs/grass7/ext/r_category.py +++ b/python/plugins/processing/algs/grass7/ext/r_category.py @@ -25,15 +25,14 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -import codecs from processing.tools.system import getTempFilename +from processing.algs.grass7.Grass7Utils import Grass7Utils - -def checkParameterValuesBeforeExecuting(alg): +def checkParameterValuesBeforeExecuting(alg, parameters, context): """ Verify if we have the right parameters """ - rules = alg.getParameterValue(u'rules') - txtrules = alg.getParameterValue(u'txtrules') - raster = alg.getParameterValue(u'raster') + rules = alg.parameterAsString(parameters, 'rules', context) + txtrules = alg.parameterAsString(parameters, 'txtrules', context) + raster = alg.parameterAsString(parameters, 'raster', context) if rules and txtrules: return alg.tr("You need to set either a rules file or write directly the rules!") @@ -43,78 +42,42 @@ def checkParameterValuesBeforeExecuting(alg): return None -def processInputs(alg): +def processInputs(alg, parameters, context): # If there is another raster to copy categories from # we need to import it with r.in.gdal rather than r.external - inputRaster = alg.getParameterValue(u'map') - copyRaster = alg.getParameterValue(u'raster') - if copyRaster: - if copyRaster in list(alg.exportedLayers.keys()): - return - - for raster, method in (inputRaster, 'r.external'), (copyRaster, 'r.in.gdal'): - alg.setSessionProjectionFromLayer(raster, alg.commands) - - destFilename = alg.getTempFilename() - alg.exportedLayers[raster] = destFilename - command = '{} input={} output={} band=1 --overwrite -o'.format(method, 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) - else: - alg.processInputs(context, parameters) + raster = alg.parameterAsString(parameters, 'raster', context) + if raster: + alg.loadRasterLayerFromParameter('raster', + parameters, context, + False, None) + alg.loadRasterLayerFromParameter('map', parameters, context) + alg.postInputs() -def processCommand(alg, parameters): - # We temporary remove the output - out = alg.getOutputFromName('output') - mapParam = alg.getParameterValue('map') - alg.exportedLayers[out.value] = alg.exportedLayers[mapParam] - alg.removeOutputFromName('output') - txtRulesParam = alg.getParameterFromName(u'txtrules') - rules = alg.getParameterFromName(u'rules') - +def processCommand(alg, parameters, context): # Handle inline rules - if txtRulesParam.value: + txtRules = alg.parameterAsString(parameters, 'txtrules', context) + if txtRules: # Creates a temporary txt file - tempRulesName = getTempFilename('txt') + tempRulesName = getTempFilename() # Inject rules into temporary txt file - with codecs.open(tempRulesName, 'w', 'utf-8') as tempRules: - tempRules.write(txtRulesParam.value) + with open(tempRulesName, "w") as tempRules: + tempRules.write(txtRules) + alg.removeParameter('txtrules') + parameters['rules'] = tempRulesName - # Replace rules with temporary file - rules.value = tempRulesName - alg.parameters.remove(txtRulesParam) + alg.processCommand(parameters, context, True) + - alg.processCommand() +def processOutputs(alg, parameters, context): + # Output results ('map' layer) + createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) + metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) - # We re-add the new output - alg.addOutput(out) - - -def processOutputs(alg): - # Output results ('from' table and output table) - out = alg.getOutputValue('output') - command = u"r.out.gdal --overwrite -t -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\"".format( - alg.exportedLayers[out], out) - alg.commands.append(command) - alg.outputCommands.append(command) + # We need to export the raster with all its bands and its color table + fileName = alg.parameterAsOutputLayer(parameters, 'output', context) + outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) + grassName = alg.exportedLayers['map'] + alg.exportRasterLayer(grassName, fileName, True, + outFormat, createOpt, metaOpt) diff --git a/python/plugins/processing/algs/grass7/ext/r_colors.py b/python/plugins/processing/algs/grass7/ext/r_colors.py index c6f9c54a91b..f1153e0b2f1 100644 --- a/python/plugins/processing/algs/grass7/ext/r_colors.py +++ b/python/plugins/processing/algs/grass7/ext/r_colors.py @@ -26,87 +26,62 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' import os -from copy import deepcopy +from processing.algs.grass7.Grass7Utils import Grass7Utils - -def checkParameterValuesBeforeExecuting(alg): +def checkParameterValuesBeforeExecuting(alg, parameters, context): """ Verify if we have the right parameters """ - if alg.getParameterValue('rules_txt') and alg.getParameterValue('rules'): + txtRules = alg.parameterAsString(parameters, 'rules_txt', context) + rules = alg.parameterAsString(parameters, 'rules', context) + if txtRules and rules: return alg.tr("You need to set either inline rules or a rules file!") return None -def processInputs(alg): +def processInputs(alg, parameters, context): # import all rasters with their color tables (and their bands) # We need to import all the bands and color tables of the input rasters - rasters = alg.getParameterValue('map').split(',') - for raster in rasters: - if raster in list(alg.exportedLayers.keys()): - continue + rasters = alg.parameterAsLayerList(parameters, 'map', context) + for idx, layer in enumerate(rasters): + layerName = 'map_{}'.format(idx) + # Add a raster layer + alg.loadRasterLayer(layerName, layer, False, None) - 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) + # Optional raster layer to copy from + raster = alg.parameterAsString(parameters, 'raster', context) + if raster: + alg.loadRasterLayerFromParameter('raster', parameters, context, False, None) - 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) + alg.postInputs() -def processCommand(alg, parameters): - # remove output before processCommand - new_parameters = deepcopy(parameters) - - output = alg.getOutputFromName('output_dir') - alg.removeOutputFromName('output_dir') - color = alg.getParameterFromName('color') - if new_parameters[color.name()] == 0: - del new_parameters[color.name()] - - # Handle rules - txtRules = alg.getParameterFromName('rules_txt') - if new_parameters[txtRules.name()]: +def processCommand(alg, parameters, context): + # Handle inline rules + txtRules = alg.parameterAsString(parameters, 'txtrules', context) + if txtRules: # Creates a temporary txt file - tempRulesName = alg.getTempFilename() - + tempRulesName = getTempFilename() + # Inject rules into temporary txt file with open(tempRulesName, "w") as tempRules: - tempRules.write(new_parameters[txtRules.name()]) + tempRules.write(txtRules) + alg.removeParameter('txtrules') + parameters['rules'] = tempRulesName + + alg.processCommand(parameters, context, True) + + +def processOutputs(alg, parameters, context): + createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) + metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) - # Use temporary file as rules file - new_parameters['rules'] = tempRulesName - del new_parameters[textRules.name()] - - alg.processCommand(new_parameters) - - -def processOutputs(alg): # Export all rasters with their color tables (and their bands) - rasters = [alg.exportedLayers[f] for f in alg.getParameterValue('map').split(',')] - output_dir = alg.getOutputValue('output_dir') - for raster in rasters: - command = u"r.out.gdal -t createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format( - raster, - os.path.join(output_dir, raster + '.tif') - ) - alg.commands.append(command) - alg.outputCommands.append(command) + rasters = alg.parameterAsLayerList(parameters, 'map', context) + outputDir = alg.parameterAsString(parameters, 'output_dir', context) + for idx, raster in enumerate(rasters): + rasterName = 'map_{}'.format(idx) + fileName = os.path.join(outputDir, rasterName) + outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) + alg.exportRasterLayer(alg.exportedLayers[rasterName], fileName, True, + outFormat, createOpt, metaOpt) + diff --git a/python/plugins/processing/algs/grass7/ext/r_colors_stddev.py b/python/plugins/processing/algs/grass7/ext/r_colors_stddev.py index 690cd468b72..5adbeac13de 100644 --- a/python/plugins/processing/algs/grass7/ext/r_colors_stddev.py +++ b/python/plugins/processing/algs/grass7/ext/r_colors_stddev.py @@ -29,7 +29,7 @@ from processing.algs.grass7.Grass7Utils import Grass7Utils def processInputs(alg, parameters, context): # We need to import all the bands and to preserve color table - if 'input' in alg.exportedLayers: + if 'map' in alg.exportedLayers: return # We need to import all the bands and color tables of the input raster diff --git a/python/plugins/processing/algs/grass7/ext/r_horizon.py b/python/plugins/processing/algs/grass7/ext/r_horizon.py index b25ff88042f..b42472c0f8d 100644 --- a/python/plugins/processing/algs/grass7/ext/r_horizon.py +++ b/python/plugins/processing/algs/grass7/ext/r_horizon.py @@ -26,16 +26,27 @@ __copyright__ = '(C) 2017, Médéric Ribreux' __revision__ = '$Format:%H$' import os - -def processOutputs(alg, parameters, context): - # There will be as outputs as the difference between start and end divided by steps +def checkParameterValuesBeforeExecuting(alg, parameters, context): + """ Verify if we have the right parameters """ start = alg.parameterAsDouble(parameters, 'start', context) end = alg.parameterAsDouble(parameters, 'end', context) step = alg.parameterAsDouble(parameters, 'step', context) - num = start + step + + if start >= end: + return alg.tr("The start position muste be inferior to the end position!") + if step == 0.0: + return alg.tr("The step must be greater than zero!") + return None + +def processOutputs(alg, parameters, context): + # There will be as many outputs as the difference between start and end divided by steps + start = alg.parameterAsDouble(parameters, 'start', context) + end = alg.parameterAsDouble(parameters, 'end', context) + step = alg.parameterAsDouble(parameters, 'step', context) + num = start directory = alg.parameterAsString(parameters, 'output', context) while num < end: - grassName = '{}_{}'.format(alg.exportedLayers['output'], int(num)) + grassName = '{}_{}'.format('output{}'.format(alg.uniqueSuffix), int(num)) fileName = '{}.tif'.format(os.path.join(directory, '{0:0>3}'.format(int(num)))) alg.exportRasterLayer(grassName, fileName) num += step diff --git a/python/plugins/processing/algs/grass7/ext/r_mapcalc.py b/python/plugins/processing/algs/grass7/ext/r_mapcalc.py index 8ac59c84476..587c1023ef8 100644 --- a/python/plugins/processing/algs/grass7/ext/r_mapcalc.py +++ b/python/plugins/processing/algs/grass7/ext/r_mapcalc.py @@ -25,7 +25,7 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from os import path +import os def checkParameterValuesBeforeExecuting(alg, parameters, context): @@ -38,46 +38,31 @@ def checkParameterValuesBeforeExecuting(alg, parameters, context): def processInputs(alg, parameters, context): - # We need to use the same raster names than in QGIS - if alg.getParameterValue('maps'): - rasters = alg.getParameterValue('maps').split(',') - for raster in rasters: - if raster in list(alg.exportedLayers.keys()): - continue - - alg.setSessionProjectionFromLayer(raster, alg.commands) - destFilename = path.splitext(path.basename(raster))[0] - 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) + # We will use the same raster names than in QGIS to name the rasters in GRASS + rasters = alg.parameterAsLayerList(parameters, 'maps', context) + for idx, raster in enumerate(rasters): + rasterName = os.path.splitext( + os.path.basename(raster.source()))[0] + alg.inputLayers.append(raster) + alg.setSessionProjectionFromLayer(raster) + command = 'r.in.gdal input="{0}" output="{1}" --overwrite -o'.format( + os.path.normpath(raster.source()), + rasterName) + alg.commands.append(command) + alg.removeParameter('maps') + alg.postInputs() def processCommand(alg, parameters, context): - # Remove output for command - output_dir = alg.getOutputFromName('output_dir') - maps = alg.getParameterFromName('maps') - alg.removeOutputFromName('output_dir') - alg.parameters.remove(maps) - alg.processCommand() - alg.addOutput(output_dir) - alg.addParameter(maps) + alg.processCommand(parameters, context, True) + +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) + diff --git a/python/plugins/processing/algs/grass7/ext/r_series_interp.py b/python/plugins/processing/algs/grass7/ext/r_series_interp.py index 6b07f3ce660..6f8bc033c96 100644 --- a/python/plugins/processing/algs/grass7/ext/r_series_interp.py +++ b/python/plugins/processing/algs/grass7/ext/r_series_interp.py @@ -25,8 +25,8 @@ __copyright__ = '(C) 2016, Médéric Ribreux' __revision__ = '$Format:%H$' -from os import path - +import os +from processing.algs.grass7.Grass7Utils import Grass7Utils def checkParameterValuesBeforeExecuting(alg, parameters, context): """ Verify if we have the right parameters """ @@ -46,20 +46,14 @@ def checkParameterValuesBeforeExecuting(alg, parameters, context): def processCommand(alg, parameters, context): # We temporary remove the output directory - outdir = alg.getOutputFromName('output_dir') - alg.removeOutputFromName('output_dir') - - alg.processCommand(parameters, context) - - # We re-add the new output - alg.addOutput(outdir) + alg.processCommand(parameters, context, True) def processOutputs(alg, parameters, context): # We take all the outputs and we export them to the output directory - outdir = alg.getOutputFromName('output_dir') - output = alg.getParameterValue('output') - outfile = alg.getParameterValue('outfile') + outputDir = alg.parameterAsString(parameters, 'output_dir', context) + output = alg.parameterAsString(parameters, 'output', context) + outfile = alg.parameterAsString(parameters, 'outfile', context) outs = [] if output: outs = output.split(',') @@ -69,9 +63,14 @@ def processOutputs(alg, parameters, context): for line in f: if '|' in line: outs.append(line.split('|')[0]) + + createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) + metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) for out in outs: - command = u"r.out.gdal --overwrite -t -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\"".format( - out, path.join(outdir.value, '{}.tif'.format(out))) - alg.commands.append(command) - alg.outputCommands.append(command) + # We need to export the raster with all its bands and its color table + fileName = os.path.join(outputDir, out) + outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) + alg.exportRasterLayer(out, fileName, True, + outFormat, createOpt, metaOpt) + diff --git a/python/plugins/processing/algs/grass7/ext/r_support.py b/python/plugins/processing/algs/grass7/ext/r_support.py deleted file mode 100644 index fa84873d166..00000000000 --- a/python/plugins/processing/algs/grass7/ext/r_support.py +++ /dev/null @@ -1,64 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -*************************************************************************** - r_support.py - ------------ - Date : February 2016 - Copyright : (C) 2016 by Médéric Ribreux - Email : medspx at medspx dot fr -*************************************************************************** -* * -* 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. * -* * -*************************************************************************** -""" - -__author__ = 'Médéric Ribreux' -__date__ = 'February 2016' -__copyright__ = '(C) 2016, Médéric Ribreux' - -# This will get replaced with a git SHA1 when you do a git archive - -__revision__ = '$Format:%H$' - - -def processCommand(alg, parameters): - # We temporary remove the output - out = alg.getOutputFromName('output') - mapParam = alg.getParameterValue('map') - alg.exportedLayers[out.value] = alg.exportedLayers[mapParam] - alg.removeOutputFromName('output') - timestamp = alg.getParameterValue('timestamp') - if timestamp: - command = "r.timestamp map={} date='{}'".format(alg.exportedLayers[mapParam], - timestamp) - alg.commands.append(command) - alg.parameters.remove(alg.getParameterFromName('timestamp')) - alg.processCommand() - - # We re-add the new output - alg.addOutput(out) - - -def processOutputs(alg): - tags = {'title': 'TIFFTAG_DOCUMENTNAME', 'description': 'TIFFTAG_IMAGEDESCRIPTION', - 'creator': 'TIFFTAG_ARTIST', 'timestamp': 'TIFFTAG_DATETIME', - 'source1': 'TIFFTAG_COPYRIGHT', 'units': 'TIFFTAG_RESOLUTIONUNIT', - 'source2': 'GRASS_SOURCE2', 'comments': 'GRASS_HISTORY', 'vdatum': 'GRASS_VDATUM'} - awk = "awk -F '=' '" - for support, tag in list(tags.items()): - awk = '{0} /{1}=".+"/{{ print \"{2}=\"substr($0,{3},length($0) - {3})"," }}'.format( - awk, support, tag, len(support) + 3) - - # Output results ('from' table and output table) - out = alg.getOutputValue('output') - command = u"SDF=$(r.info -e map={} | {}')".format(alg.exportedLayers[out], awk) - alg.commands.append(command) - command = u"r.out.gdal --overwrite -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" metaopt=\"${{SDF%%,}}\"".format( - alg.exportedLayers[out], out) - alg.commands.append(command) - alg.outputCommands.append(command)