Skip to content

Commit 5827513

Browse files
author
Médéric RIBREUX
committedNov 4, 2017
First work on ext mechanism factorisation
1 parent 6bf8448 commit 5827513

File tree

2 files changed

+52
-60
lines changed

2 files changed

+52
-60
lines changed
 

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

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -352,28 +352,35 @@ def processAlgorithm(self, parameters, context, feedback):
352352
self.grabDefaultGrassParameters(parameters, context)
353353

354354
# Handle ext functions for inputs/command/outputs
355-
if self.module:
356-
if hasattr(self.module, 'processInputs'):
357-
func = getattr(self.module, 'processInputs')
358-
func(self)
359-
else:
360-
self.processInputs(parameters, context)
361-
362-
if hasattr(self.module, 'processCommand'):
363-
func = getattr(self.module, 'processCommand')
364-
func(self)
365-
else:
366-
self.processCommand()
367-
368-
if hasattr(self.module, 'processOutputs'):
369-
func = getattr(self.module, 'processOutputs')
370-
func(self)
355+
# if self.module:
356+
# if hasattr(self.module, 'processInputs'):
357+
# func = getattr(self.module, 'processInputs')
358+
# func(self)
359+
# else:
360+
# self.processInputs(parameters, context)
361+
362+
# if hasattr(self.module, 'processCommand'):
363+
# func = getattr(self.module, 'processCommand')
364+
# func(self)
365+
# else:
366+
# self.processCommand()
367+
368+
# if hasattr(self.module, 'processOutputs'):
369+
# func = getattr(self.module, 'processOutputs')
370+
# func(self)
371+
# else:
372+
# self.processOutputs()
373+
# else:
374+
# self.processInputs(parameters, context)
375+
# self.processCommand(parameters, context)
376+
# self.processOutputs(parameters, context)
377+
# Handle ext methods for input/command/outputs
378+
for fName in ['Inputs', 'Command', 'Outputs']:
379+
fullName = 'process{}'.format(fName)
380+
if self.module and hasattr(self.module, fullName):
381+
getattr(self.module, fullName)(self, parameters, context)
371382
else:
372-
self.processOutputs()
373-
else:
374-
self.processInputs(parameters, context)
375-
self.processCommand(parameters, context)
376-
self.processOutputs(parameters, context)
383+
getattr(self, fullName)(parameters, context)
377384

378385
# Run GRASS
379386
loglines = []
@@ -468,7 +475,12 @@ def processInputs(self, parameters, context):
468475
# Add a vector layer
469476
elif layer.type() == QgsMapLayer.VectorLayer:
470477
self.commands.append(self.exportVectorLayer(layerName, layerSrc))
471-
478+
self.postInputs()
479+
480+
def postInputs(self):
481+
"""
482+
After layer imports, we need to update some internal parameters
483+
"""
472484
# If projection has not already be set, use the project
473485
self.setSessionProjectionFromProject()
474486

@@ -647,17 +659,22 @@ def processOutputs(self, parameters, context):
647659
self.outputCommands.append(command)
648660
QgsMessageLog.logMessage('processOutputs. Commands: {}'.format(self.commands), 'Grass7', QgsMessageLog.INFO)
649661

650-
def exportRasterLayer(self, layerKey, layerSrc):
662+
def exportRasterLayer(self, layerKey, layerSrc, external=True, band=1):
651663
"""
652664
Creates a dedicated command to load a raster into
653665
temporary GRASS DB.
666+
:layerKey: name of the parameter
667+
:layerSrc: file path of raster layer
668+
:external: use r.external (r.in.gdal if False).
669+
:band: import only this band (if None, all bands are imported).
654670
"""
655-
# TODO: handle multiple bands
656-
#destFileName = QgsProcessingUtils.generateTempFilename(layerKey)
657671
destFilename = 'a' + os.path.basename(getTempFilename())
658672
self.exportedLayers[layerKey] = destFilename
659-
command = 'r.external input="{}" band=1 output={} --overwrite -o'.format(
660-
layerSrc, destFilename)
673+
command = '{0] input="{1}"{2}output={3} --overwrite -o'.format(
674+
'r.external' if external else 'r.in.gdal',
675+
layerSrc,
676+
' band={} 'format(band) if band else '',
677+
destFilename)
661678
return command
662679

663680
def exportVectorLayer(self, layerKey, layerSrc):

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,15 @@
2626
__revision__ = '$Format:%H$'
2727

2828

29-
def processInputs(alg):
29+
def processInputs(alg, parameters, context):
3030
# We need to import all the bands and color tables of the input raster
31-
raster = alg.getParameterValue('input')
32-
if raster in list(alg.exportedLayers.keys()):
31+
if 'input' in alg.exportedLayers:
3332
return
34-
35-
alg.setSessionProjectionFromLayer(raster, alg.commands)
36-
destFilename = alg.getTempFilename()
37-
alg.exportedLayers[raster] = destFilename
38-
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
39-
alg.commands.append(command)
40-
41-
alg.setSessionProjectionFromProject(alg.commands)
42-
43-
region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
44-
regionCoords = region.split(',')
45-
command = 'g.region'
46-
command += ' -a'
47-
command += ' n=' + str(regionCoords[3])
48-
command += ' s=' + str(regionCoords[2])
49-
command += ' e=' + str(regionCoords[1])
50-
command += ' w=' + str(regionCoords[0])
51-
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
52-
if cellsize:
53-
command += ' res=' + str(cellsize)
54-
else:
55-
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
56-
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
57-
if alignToResolution:
58-
command += ' -a'
59-
alg.commands.append(command)
60-
61-
62-
def processCommand(alg, parameters):
33+
raster = alg.parameterAsRasterLayer(parameters, 'input', context)
34+
alg.setSessionProjectionFromLayer(raster)
35+
alg.prepareInputs()
36+
37+
def processCommand(alg, parameters, context):
6338
# We need to introduce something clever:
6439
# if the input raster is multiband: export each component directly
6540
raster = alg.exportedLayers[alg.getParameterValue('input')]
@@ -77,7 +52,7 @@ def processCommand(alg, parameters):
7752
alg.commands.extend(commands)
7853

7954

80-
def processOutputs(alg):
55+
def processOutputs(alg, parameters, context):
8156
raster = alg.exportedLayers[alg.getParameterValue('input')]
8257
commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)]
8358
for color in ['red', 'green', 'blue']:

0 commit comments

Comments
 (0)
Please sign in to comment.