Skip to content

Commit 90bf630

Browse files
author
Médéric RIBREUX
committedDec 27, 2017
Fix all r algorithms ext scripts!
1 parent 8b691e8 commit 90bf630

File tree

12 files changed

+145
-289
lines changed

12 files changed

+145
-289
lines changed
 

‎python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
QgsProcessingParameterVectorDestination,
5757
QgsProcessingParameterRasterDestination,
5858
QgsProcessingParameterFileDestination,
59+
QgsProcessingParameterFile,
5960
QgsProcessingParameterFolderDestination,
6061
QgsProcessingOutputFolder,
6162
QgsProcessingOutputVectorLayer,
@@ -543,6 +544,8 @@ def processCommand(self, parameters, context, delOutputs=False):
543544
value = ','.join(
544545
self.parameterAsFields(parameters, paramName, context)
545546
)
547+
elif isinstance(param, QgsProcessingParameterFile):
548+
value = self.parameterAsString(parameters, paramName, context)
546549
# For numbers and points, we translate as a string
547550
elif isinstance(param, (QgsProcessingParameterNumber,
548551
QgsProcessingParameterPoint)):
@@ -694,8 +697,8 @@ def exportRasterLayer(self, grassName, fileName,
694697
# Adjust region to layer before exporting
695698
cmd.append('g.region raster={}'.format(grassName))
696699
cmd.append(
697-
'r.out.gdal -c -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format(
698-
' -t' if colorTable else '',
700+
'r.out.gdal -t -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format(
701+
'' if colorTable else ' -c',
699702
grassName, fileName,
700703
outFormat,
701704
' createopt="{}"'.format(createOpt) if createOpt else '',
@@ -719,8 +722,8 @@ def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=
719722

720723
# Add a loop export from the basename
721724
for cmd in [self.commands, self.outputCommands]:
722-
# Adjust region to layer before exporting
723-
# TODO: Does-it works under MS-Windows or MacOSX?
725+
# TODO Windows support
726+
# TODO Format/options support
724727
cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename))
725728
cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format(
726729
' -t' if colorTable else '', outDir,

‎python/plugins/processing/algs/grass7/TODO.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ QGIS3 Processing Port
77
* Review i.py.
88
* Use a message band rather than a messageBox for errors on exclusives parameters.
99
* r_rgb.py
10-
* r_blend_combine.py
11-
* r_blend_rgb.py
1210
* r_horizon.py
1311
* r_mask.py
1412
* r_mask_vect.py

‎python/plugins/processing/algs/grass7/description/r.colors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ r.colors
22
Creates/modifies the color table associated with a raster map.
33
Raster (r.*)
44
QgsProcessingParameterMultipleLayers|map|Name of raster maps(s)|3|None|False
5-
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
5+
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
66
QgsProcessingParameterString|rules_txt|Color rules|None|True|True
77
QgsProcessingParameterFile|rules|Color rules file|False|txt|None|True
88
QgsProcessingParameterRasterLayer|raster|Raster map from which to copy color table|None|True
@@ -12,4 +12,4 @@ QgsProcessingParameterBoolean|-n|Invert colors|False
1212
QgsProcessingParameterBoolean|-g|Logarithmic scaling|False
1313
QgsProcessingParameterBoolean|-a|Logarithmic-absolute scaling|False
1414
QgsProcessingParameterBoolean|-e|Histogram equalization|False
15-
QgsProcessingParameterFolderDestination|output_dir|Output Directory
15+
QgsProcessingParameterFolderDestination|output_dir|Output Directory|None|False

‎python/plugins/processing/algs/grass7/description/r.series.interp.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ QgsProcessingParameterString|datapos|Data point position for each input map|None
66
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
77
QgsProcessingParameterString|output|Name for output raster map (comma separated list if multiple)|None|False|True
88
QgsProcessingParameterString|samplingpos|Sampling point position for each output map (comma separated list)|None|True|True
9-
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
9+
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
1010
QgsProcessingParameterEnum|method|Interpolation method, currently only linear interpolation is supported|linear|False|0|True
11-
QgsProcessingParameterFolderDestination|output_dir|Interpolated rasters
11+
QgsProcessingParameterFolderDestination|output_dir|Interpolated rasters|None|False

‎python/plugins/processing/algs/grass7/description/r.support.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎python/plugins/processing/algs/grass7/ext/r_category.py

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
import codecs
2928
from processing.tools.system import getTempFilename
29+
from processing.algs.grass7.Grass7Utils import Grass7Utils
3030

31-
32-
def checkParameterValuesBeforeExecuting(alg):
31+
def checkParameterValuesBeforeExecuting(alg, parameters, context):
3332
""" Verify if we have the right parameters """
34-
rules = alg.getParameterValue(u'rules')
35-
txtrules = alg.getParameterValue(u'txtrules')
36-
raster = alg.getParameterValue(u'raster')
33+
rules = alg.parameterAsString(parameters, 'rules', context)
34+
txtrules = alg.parameterAsString(parameters, 'txtrules', context)
35+
raster = alg.parameterAsString(parameters, 'raster', context)
3736

3837
if rules and txtrules:
3938
return alg.tr("You need to set either a rules file or write directly the rules!")
@@ -43,78 +42,42 @@ def checkParameterValuesBeforeExecuting(alg):
4342
return None
4443

4544

46-
def processInputs(alg):
45+
def processInputs(alg, parameters, context):
4746
# If there is another raster to copy categories from
4847
# we need to import it with r.in.gdal rather than r.external
49-
inputRaster = alg.getParameterValue(u'map')
50-
copyRaster = alg.getParameterValue(u'raster')
51-
if copyRaster:
52-
if copyRaster in list(alg.exportedLayers.keys()):
53-
return
54-
55-
for raster, method in (inputRaster, 'r.external'), (copyRaster, 'r.in.gdal'):
56-
alg.setSessionProjectionFromLayer(raster, alg.commands)
57-
58-
destFilename = alg.getTempFilename()
59-
alg.exportedLayers[raster] = destFilename
60-
command = '{} input={} output={} band=1 --overwrite -o'.format(method, raster, destFilename)
61-
alg.commands.append(command)
62-
63-
alg.setSessionProjectionFromProject(alg.commands)
64-
65-
region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
66-
regionCoords = region.split(',')
67-
command = 'g.region'
68-
command += ' -a'
69-
command += ' n=' + str(regionCoords[3])
70-
command += ' s=' + str(regionCoords[2])
71-
command += ' e=' + str(regionCoords[1])
72-
command += ' w=' + str(regionCoords[0])
73-
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
74-
if cellsize:
75-
command += ' res=' + str(cellsize)
76-
else:
77-
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
78-
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
79-
if alignToResolution:
80-
command += ' -a'
81-
alg.commands.append(command)
82-
else:
83-
alg.processInputs(context, parameters)
84-
85-
86-
def processCommand(alg, parameters):
87-
# We temporary remove the output
88-
out = alg.getOutputFromName('output')
89-
mapParam = alg.getParameterValue('map')
90-
alg.exportedLayers[out.value] = alg.exportedLayers[mapParam]
91-
alg.removeOutputFromName('output')
92-
txtRulesParam = alg.getParameterFromName(u'txtrules')
93-
rules = alg.getParameterFromName(u'rules')
48+
raster = alg.parameterAsString(parameters, 'raster', context)
49+
if raster:
50+
alg.loadRasterLayerFromParameter('raster',
51+
parameters, context,
52+
False, None)
53+
alg.loadRasterLayerFromParameter('map', parameters, context)
54+
alg.postInputs()
55+
9456

57+
def processCommand(alg, parameters, context):
9558
# Handle inline rules
96-
if txtRulesParam.value:
59+
txtRules = alg.parameterAsString(parameters, 'txtrules', context)
60+
if txtRules:
9761
# Creates a temporary txt file
98-
tempRulesName = getTempFilename('txt')
62+
tempRulesName = getTempFilename()
9963

10064
# Inject rules into temporary txt file
101-
with codecs.open(tempRulesName, 'w', 'utf-8') as tempRules:
102-
tempRules.write(txtRulesParam.value)
103-
104-
# Replace rules with temporary file
105-
rules.value = tempRulesName
106-
alg.parameters.remove(txtRulesParam)
107-
108-
alg.processCommand()
109-
110-
# We re-add the new output
111-
alg.addOutput(out)
112-
113-
114-
def processOutputs(alg):
115-
# Output results ('from' table and output table)
116-
out = alg.getOutputValue('output')
117-
command = u"r.out.gdal --overwrite -t -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\"".format(
118-
alg.exportedLayers[out], out)
119-
alg.commands.append(command)
120-
alg.outputCommands.append(command)
65+
with open(tempRulesName, "w") as tempRules:
66+
tempRules.write(txtRules)
67+
alg.removeParameter('txtrules')
68+
parameters['rules'] = tempRulesName
69+
70+
alg.processCommand(parameters, context, True)
71+
72+
73+
def processOutputs(alg, parameters, context):
74+
# Output results ('map' layer)
75+
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
76+
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
77+
78+
# We need to export the raster with all its bands and its color table
79+
fileName = alg.parameterAsOutputLayer(parameters, 'output', context)
80+
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
81+
grassName = alg.exportedLayers['map']
82+
alg.exportRasterLayer(grassName, fileName, True,
83+
outFormat, createOpt, metaOpt)

‎python/plugins/processing/algs/grass7/ext/r_colors.py

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,87 +26,62 @@
2626
__revision__ = '$Format:%H$'
2727

2828
import os
29-
from copy import deepcopy
29+
from processing.algs.grass7.Grass7Utils import Grass7Utils
3030

31-
32-
def checkParameterValuesBeforeExecuting(alg):
31+
def checkParameterValuesBeforeExecuting(alg, parameters, context):
3332
""" Verify if we have the right parameters """
34-
if alg.getParameterValue('rules_txt') and alg.getParameterValue('rules'):
33+
txtRules = alg.parameterAsString(parameters, 'rules_txt', context)
34+
rules = alg.parameterAsString(parameters, 'rules', context)
35+
if txtRules and rules:
3536
return alg.tr("You need to set either inline rules or a rules file!")
3637

3738
return None
3839

3940

40-
def processInputs(alg):
41+
def processInputs(alg, parameters, context):
4142
# import all rasters with their color tables (and their bands)
4243
# We need to import all the bands and color tables of the input rasters
43-
rasters = alg.getParameterValue('map').split(',')
44-
for raster in rasters:
45-
if raster in list(alg.exportedLayers.keys()):
46-
continue
47-
48-
alg.setSessionProjectionFromLayer(raster, alg.commands)
49-
destFilename = alg.getTempFilename()
50-
alg.exportedLayers[raster] = destFilename
51-
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
52-
alg.commands.append(command)
53-
54-
alg.setSessionProjectionFromProject(alg.commands)
44+
rasters = alg.parameterAsLayerList(parameters, 'map', context)
45+
for idx, layer in enumerate(rasters):
46+
layerName = 'map_{}'.format(idx)
47+
# Add a raster layer
48+
alg.loadRasterLayer(layerName, layer, False, None)
5549

56-
region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
57-
regionCoords = region.split(',')
58-
command = 'g.region'
59-
command += ' -a'
60-
command += ' n=' + str(regionCoords[3])
61-
command += ' s=' + str(regionCoords[2])
62-
command += ' e=' + str(regionCoords[1])
63-
command += ' w=' + str(regionCoords[0])
64-
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
65-
if cellsize:
66-
command += ' res=' + str(cellsize)
67-
else:
68-
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
69-
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
70-
if alignToResolution:
71-
command += ' -a'
72-
alg.commands.append(command)
50+
# Optional raster layer to copy from
51+
raster = alg.parameterAsString(parameters, 'raster', context)
52+
if raster:
53+
alg.loadRasterLayerFromParameter('raster', parameters, context, False, None)
7354

55+
alg.postInputs()
7456

75-
def processCommand(alg, parameters):
76-
# remove output before processCommand
77-
new_parameters = deepcopy(parameters)
7857

79-
output = alg.getOutputFromName('output_dir')
80-
alg.removeOutputFromName('output_dir')
81-
color = alg.getParameterFromName('color')
82-
if new_parameters[color.name()] == 0:
83-
del new_parameters[color.name()]
84-
85-
# Handle rules
86-
txtRules = alg.getParameterFromName('rules_txt')
87-
if new_parameters[txtRules.name()]:
58+
def processCommand(alg, parameters, context):
59+
# Handle inline rules
60+
txtRules = alg.parameterAsString(parameters, 'txtrules', context)
61+
if txtRules:
8862
# Creates a temporary txt file
89-
tempRulesName = alg.getTempFilename()
90-
63+
tempRulesName = getTempFilename()
64+
9165
# Inject rules into temporary txt file
9266
with open(tempRulesName, "w") as tempRules:
93-
tempRules.write(new_parameters[txtRules.name()])
67+
tempRules.write(txtRules)
68+
alg.removeParameter('txtrules')
69+
parameters['rules'] = tempRulesName
70+
71+
alg.processCommand(parameters, context, True)
72+
73+
74+
def processOutputs(alg, parameters, context):
75+
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
76+
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
9477

95-
# Use temporary file as rules file
96-
new_parameters['rules'] = tempRulesName
97-
del new_parameters[textRules.name()]
98-
99-
alg.processCommand(new_parameters)
100-
101-
102-
def processOutputs(alg):
10378
# Export all rasters with their color tables (and their bands)
104-
rasters = [alg.exportedLayers[f] for f in alg.getParameterValue('map').split(',')]
105-
output_dir = alg.getOutputValue('output_dir')
106-
for raster in rasters:
107-
command = u"r.out.gdal -t createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
108-
raster,
109-
os.path.join(output_dir, raster + '.tif')
110-
)
111-
alg.commands.append(command)
112-
alg.outputCommands.append(command)
79+
rasters = alg.parameterAsLayerList(parameters, 'map', context)
80+
outputDir = alg.parameterAsString(parameters, 'output_dir', context)
81+
for idx, raster in enumerate(rasters):
82+
rasterName = 'map_{}'.format(idx)
83+
fileName = os.path.join(outputDir, rasterName)
84+
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
85+
alg.exportRasterLayer(alg.exportedLayers[rasterName], fileName, True,
86+
outFormat, createOpt, metaOpt)
87+

‎python/plugins/processing/algs/grass7/ext/r_colors_stddev.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
def processInputs(alg, parameters, context):
3131
# We need to import all the bands and to preserve color table
32-
if 'input' in alg.exportedLayers:
32+
if 'map' in alg.exportedLayers:
3333
return
3434

3535
# We need to import all the bands and color tables of the input raster

‎python/plugins/processing/algs/grass7/ext/r_horizon.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@
2626
__revision__ = '$Format:%H$'
2727
import os
2828

29+
def checkParameterValuesBeforeExecuting(alg, parameters, context):
30+
""" Verify if we have the right parameters """
31+
start = alg.parameterAsDouble(parameters, 'start', context)
32+
end = alg.parameterAsDouble(parameters, 'end', context)
33+
step = alg.parameterAsDouble(parameters, 'step', context)
34+
35+
if start >= end:
36+
return alg.tr("The start position muste be inferior to the end position!")
37+
if step == 0.0:
38+
return alg.tr("The step must be greater than zero!")
39+
return None
2940

3041
def processOutputs(alg, parameters, context):
31-
# There will be as outputs as the difference between start and end divided by steps
42+
# There will be as many outputs as the difference between start and end divided by steps
3243
start = alg.parameterAsDouble(parameters, 'start', context)
3344
end = alg.parameterAsDouble(parameters, 'end', context)
3445
step = alg.parameterAsDouble(parameters, 'step', context)
35-
num = start + step
46+
num = start
3647
directory = alg.parameterAsString(parameters, 'output', context)
3748
while num < end:
38-
grassName = '{}_{}'.format(alg.exportedLayers['output'], int(num))
49+
grassName = '{}_{}'.format('output{}'.format(alg.uniqueSuffix), int(num))
3950
fileName = '{}.tif'.format(os.path.join(directory, '{0:0>3}'.format(int(num))))
4051
alg.exportRasterLayer(grassName, fileName)
4152
num += step

0 commit comments

Comments
 (0)