mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-16 00:03:12 -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)
|
self.grabDefaultGrassParameters(parameters, context)
|
||||||
|
|
||||||
# Handle ext functions for inputs/command/outputs
|
# Handle ext functions for inputs/command/outputs
|
||||||
if self.module:
|
# if self.module:
|
||||||
if hasattr(self.module, 'processInputs'):
|
# if hasattr(self.module, 'processInputs'):
|
||||||
func = getattr(self.module, 'processInputs')
|
# func = getattr(self.module, 'processInputs')
|
||||||
func(self)
|
# func(self)
|
||||||
else:
|
# else:
|
||||||
self.processInputs(parameters, context)
|
# self.processInputs(parameters, context)
|
||||||
|
|
||||||
if hasattr(self.module, 'processCommand'):
|
# if hasattr(self.module, 'processCommand'):
|
||||||
func = getattr(self.module, 'processCommand')
|
# func = getattr(self.module, 'processCommand')
|
||||||
func(self)
|
# func(self)
|
||||||
else:
|
# else:
|
||||||
self.processCommand()
|
# self.processCommand()
|
||||||
|
|
||||||
if hasattr(self.module, 'processOutputs'):
|
# if hasattr(self.module, 'processOutputs'):
|
||||||
func = getattr(self.module, 'processOutputs')
|
# func = getattr(self.module, 'processOutputs')
|
||||||
func(self)
|
# 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:
|
else:
|
||||||
self.processOutputs()
|
getattr(self, fullName)(parameters, context)
|
||||||
else:
|
|
||||||
self.processInputs(parameters, context)
|
|
||||||
self.processCommand(parameters, context)
|
|
||||||
self.processOutputs(parameters, context)
|
|
||||||
|
|
||||||
# Run GRASS
|
# Run GRASS
|
||||||
loglines = []
|
loglines = []
|
||||||
@ -468,7 +475,12 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
# Add a vector layer
|
# Add a vector layer
|
||||||
elif layer.type() == QgsMapLayer.VectorLayer:
|
elif layer.type() == QgsMapLayer.VectorLayer:
|
||||||
self.commands.append(self.exportVectorLayer(layerName, layerSrc))
|
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
|
# If projection has not already be set, use the project
|
||||||
self.setSessionProjectionFromProject()
|
self.setSessionProjectionFromProject()
|
||||||
|
|
||||||
@ -647,17 +659,22 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
self.outputCommands.append(command)
|
self.outputCommands.append(command)
|
||||||
QgsMessageLog.logMessage('processOutputs. Commands: {}'.format(self.commands), 'Grass7', QgsMessageLog.INFO)
|
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
|
Creates a dedicated command to load a raster into
|
||||||
temporary GRASS DB.
|
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())
|
destFilename = 'a' + os.path.basename(getTempFilename())
|
||||||
self.exportedLayers[layerKey] = destFilename
|
self.exportedLayers[layerKey] = destFilename
|
||||||
command = 'r.external input="{}" band=1 output={} --overwrite -o'.format(
|
command = '{0] input="{1}"{2}output={3} --overwrite -o'.format(
|
||||||
layerSrc, destFilename)
|
'r.external' if external else 'r.in.gdal',
|
||||||
|
layerSrc,
|
||||||
|
' band={} 'format(band) if band else '',
|
||||||
|
destFilename)
|
||||||
return command
|
return command
|
||||||
|
|
||||||
def exportVectorLayer(self, layerKey, layerSrc):
|
def exportVectorLayer(self, layerKey, layerSrc):
|
||||||
|
@ -26,40 +26,15 @@ __copyright__ = '(C) 2016, Médéric Ribreux'
|
|||||||
__revision__ = '$Format:%H$'
|
__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
|
# We need to import all the bands and color tables of the input raster
|
||||||
raster = alg.getParameterValue('input')
|
if 'input' in alg.exportedLayers:
|
||||||
if raster in list(alg.exportedLayers.keys()):
|
|
||||||
return
|
return
|
||||||
|
raster = alg.parameterAsRasterLayer(parameters, 'input', context)
|
||||||
alg.setSessionProjectionFromLayer(raster, alg.commands)
|
alg.setSessionProjectionFromLayer(raster)
|
||||||
destFilename = alg.getTempFilename()
|
alg.prepareInputs()
|
||||||
alg.exportedLayers[raster] = destFilename
|
|
||||||
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
|
def processCommand(alg, parameters, context):
|
||||||
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):
|
|
||||||
# We need to introduce something clever:
|
# We need to introduce something clever:
|
||||||
# if the input raster is multiband: export each component directly
|
# if the input raster is multiband: export each component directly
|
||||||
raster = alg.exportedLayers[alg.getParameterValue('input')]
|
raster = alg.exportedLayers[alg.getParameterValue('input')]
|
||||||
@ -77,7 +52,7 @@ def processCommand(alg, parameters):
|
|||||||
alg.commands.extend(commands)
|
alg.commands.extend(commands)
|
||||||
|
|
||||||
|
|
||||||
def processOutputs(alg):
|
def processOutputs(alg, parameters, context):
|
||||||
raster = alg.exportedLayers[alg.getParameterValue('input')]
|
raster = alg.exportedLayers[alg.getParameterValue('input')]
|
||||||
commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)]
|
commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)]
|
||||||
for color in ['red', 'green', 'blue']:
|
for color in ['red', 'green', 'blue']:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user