Skip to content

Commit

Permalink
Fix all r algorithms ext scripts!
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX committed Dec 27, 2017
1 parent 8b691e8 commit 90bf630
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 289 deletions.
11 changes: 7 additions & 4 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -56,6 +56,7 @@
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterFileDestination,
QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputFolder,
QgsProcessingOutputVectorLayer,
Expand Down Expand Up @@ -543,6 +544,8 @@ def processCommand(self, parameters, context, delOutputs=False):
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)):
Expand Down Expand Up @@ -694,8 +697,8 @@ def exportRasterLayer(self, grassName, fileName,
# 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 '',
Expand All @@ -719,8 +722,8 @@ def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=

# 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,
Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/grass7/TODO.md
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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
14 changes: 0 additions & 14 deletions python/plugins/processing/algs/grass7/description/r.support.txt

This file was deleted.

111 changes: 37 additions & 74 deletions python/plugins/processing/algs/grass7/ext/r_category.py
Expand Up @@ -25,15 +25,14 @@

__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!")
Expand All @@ -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)


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')
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, 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)

# Replace rules with temporary file
rules.value = tempRulesName
alg.parameters.remove(txtRulesParam)

alg.processCommand()

# 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)
with open(tempRulesName, "w") as tempRules:
tempRules.write(txtRules)
alg.removeParameter('txtrules')
parameters['rules'] = tempRulesName

alg.processCommand(parameters, context, True)


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 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)
107 changes: 41 additions & 66 deletions python/plugins/processing/algs/grass7/ext/r_colors.py
Expand Up @@ -26,87 +26,62 @@
__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

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)
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)

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)
# Optional raster layer to copy from
raster = alg.parameterAsString(parameters, 'raster', context)
if raster:
alg.loadRasterLayerFromParameter('raster', parameters, context, False, None)

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)

Expand Up @@ -29,7 +29,7 @@

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
Expand Down
17 changes: 14 additions & 3 deletions python/plugins/processing/algs/grass7/ext/r_horizon.py
Expand Up @@ -26,16 +26,27 @@
__revision__ = '$Format:%H$'
import os

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)

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 outputs as the difference between start and end divided by steps
# 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 + step
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

0 comments on commit 90bf630

Please sign in to comment.