Skip to content
Permalink
Browse files

First work on ext mechanism factorisation

  • Loading branch information
Médéric RIBREUX
Médéric RIBREUX committed Sep 12, 2017
1 parent 6bf8448 commit 5827513c4be92823418f81b0681a61e329272e8c
@@ -352,28 +352,35 @@ def processAlgorithm(self, parameters, context, feedback):
self.grabDefaultGrassParameters(parameters, context)

# Handle ext functions for inputs/command/outputs
if self.module:
if hasattr(self.module, 'processInputs'):
func = getattr(self.module, 'processInputs')
func(self)
else:
self.processInputs(parameters, context)

if hasattr(self.module, 'processCommand'):
func = getattr(self.module, 'processCommand')
func(self)
else:
self.processCommand()

if hasattr(self.module, 'processOutputs'):
func = getattr(self.module, 'processOutputs')
func(self)
# if self.module:
# if hasattr(self.module, 'processInputs'):
# func = getattr(self.module, 'processInputs')
# func(self)
# else:
# self.processInputs(parameters, context)

# if hasattr(self.module, 'processCommand'):
# func = getattr(self.module, 'processCommand')
# func(self)
# else:
# self.processCommand()

# if hasattr(self.module, 'processOutputs'):
# func = getattr(self.module, 'processOutputs')
# func(self)
# else:
# self.processOutputs()
# else:
# self.processInputs(parameters, context)
# self.processCommand(parameters, context)
# self.processOutputs(parameters, context)
# Handle ext methods for input/command/outputs
for fName in ['Inputs', 'Command', 'Outputs']:
fullName = 'process{}'.format(fName)
if self.module and hasattr(self.module, fullName):
getattr(self.module, fullName)(self, parameters, context)
else:
self.processOutputs()
else:
self.processInputs(parameters, context)
self.processCommand(parameters, context)
self.processOutputs(parameters, context)
getattr(self, fullName)(parameters, context)

# Run GRASS
loglines = []
@@ -468,7 +475,12 @@ def processInputs(self, parameters, context):
# Add a vector layer
elif layer.type() == QgsMapLayer.VectorLayer:
self.commands.append(self.exportVectorLayer(layerName, layerSrc))

self.postInputs()

def postInputs(self):
"""
After layer imports, we need to update some internal parameters
"""
# If projection has not already be set, use the project
self.setSessionProjectionFromProject()

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

def exportRasterLayer(self, layerKey, layerSrc):
def exportRasterLayer(self, layerKey, layerSrc, external=True, band=1):
"""
Creates a dedicated command to load a raster into
temporary GRASS DB.
:layerKey: name of the parameter
:layerSrc: file path of raster layer
:external: use r.external (r.in.gdal if False).
:band: import only this band (if None, all bands are imported).
"""
# TODO: handle multiple bands
#destFileName = QgsProcessingUtils.generateTempFilename(layerKey)
destFilename = 'a' + os.path.basename(getTempFilename())
self.exportedLayers[layerKey] = destFilename
command = 'r.external input="{}" band=1 output={} --overwrite -o'.format(
layerSrc, destFilename)
command = '{0] input="{1}"{2}output={3} --overwrite -o'.format(
'r.external' if external else 'r.in.gdal',
layerSrc,
' band={} 'format(band) if band else '',
destFilename)
return command

def exportVectorLayer(self, layerKey, layerSrc):
@@ -26,40 +26,15 @@
__revision__ = '$Format:%H$'


def processInputs(alg):
def processInputs(alg, parameters, context):
# We need to import all the bands and color tables of the input raster
raster = alg.getParameterValue('input')
if raster in list(alg.exportedLayers.keys()):
if 'input' in alg.exportedLayers:
return

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)

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)


def processCommand(alg, parameters):
raster = alg.parameterAsRasterLayer(parameters, 'input', context)
alg.setSessionProjectionFromLayer(raster)
alg.prepareInputs()

def processCommand(alg, parameters, context):
# We need to introduce something clever:
# if the input raster is multiband: export each component directly
raster = alg.exportedLayers[alg.getParameterValue('input')]
@@ -77,7 +52,7 @@ def processCommand(alg, parameters):
alg.commands.extend(commands)


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

0 comments on commit 5827513

Please sign in to comment.
You can’t perform that action at this time.