mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
First work on ext mechanism factorisation
This commit is contained in:
parent
6bf8448bfb
commit
5827513c4b
@ -352,28 +352,35 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
||||
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 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, 'processCommand'):
|
||||
# func = getattr(self.module, 'processCommand')
|
||||
# func(self)
|
||||
# else:
|
||||
# self.processCommand()
|
||||
|
||||
if hasattr(self.module, 'processOutputs'):
|
||||
func = getattr(self.module, 'processOutputs')
|
||||
func(self)
|
||||
# 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 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
||||
# 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 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
||||
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 @@ __copyright__ = '(C) 2016, Médéric Ribreux'
|
||||
__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']:
|
||||
|
Loading…
x
Reference in New Issue
Block a user