mirror of
https://github.com/qgis/QGIS.git
synced 2025-06-05 00:03:43 -04:00
Adapt more python code to new API
This commit is contained in:
parent
28f7a8b7e1
commit
87fc31d6de
@ -95,10 +95,11 @@ class GdalAlgorithm(GeoAlgorithm):
|
|||||||
'''.format(self.commandName(), url)
|
'''.format(self.commandName(), url)
|
||||||
|
|
||||||
def commandName(self):
|
def commandName(self):
|
||||||
|
parameters = {}
|
||||||
for output in self.outputs:
|
for output in self.outputs:
|
||||||
output.setValue("dummy")
|
output.setValue("dummy")
|
||||||
for param in self.parameters:
|
for param in self.parameterDefinitions():
|
||||||
param.setValue("1")
|
parameters[param.name()] = "1"
|
||||||
name = self.getConsoleCommands(parameters)[0]
|
name = self.getConsoleCommands(parameters)[0]
|
||||||
if name.endswith(".py"):
|
if name.endswith(".py"):
|
||||||
name = name[:-3]
|
name = name[:-3]
|
||||||
|
@ -132,11 +132,11 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
lines = infile.readlines()
|
lines = infile.readlines()
|
||||||
for i in range(len(lines)):
|
for i in range(len(lines)):
|
||||||
if lines[i].startswith('<DT><b>'):
|
if lines[i].startswith('<DT><b>'):
|
||||||
for param in self.parameters:
|
for param in self.parameterDefinitions():
|
||||||
searchLine = '<b>' + param.name + '</b>'
|
searchLine = '<b>' + param.name() + '</b>'
|
||||||
if searchLine in lines[i]:
|
if searchLine in lines[i]:
|
||||||
i += 1
|
i += 1
|
||||||
descs[param.name] = (lines[i])[4:-6]
|
descs[param.name()] = (lines[i])[4:-6]
|
||||||
break
|
break
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -217,14 +217,14 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||||
self.addParameter(param)
|
self.addParameter(param)
|
||||||
|
|
||||||
def getDefaultCellsize(self):
|
def getDefaultCellsize(self, parameters, context):
|
||||||
context = dataobjects.createContext()
|
|
||||||
cellsize = 0
|
cellsize = 0
|
||||||
for param in self.parameters:
|
for param in self.parameterDefinitions():
|
||||||
if param.value:
|
if param.name() in parameters:
|
||||||
|
value = parameters[param.name()]
|
||||||
if isinstance(param, ParameterRaster):
|
if isinstance(param, ParameterRaster):
|
||||||
if isinstance(param.value, QgsRasterLayer):
|
if isinstance(value, QgsRasterLayer):
|
||||||
layer = param.value
|
layer = value
|
||||||
else:
|
else:
|
||||||
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
|
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
|
||||||
cellsize = max(cellsize, (layer.extent().xMaximum() -
|
cellsize = max(cellsize, (layer.extent().xMaximum() -
|
||||||
@ -232,7 +232,7 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
layer.width())
|
layer.width())
|
||||||
elif isinstance(param, ParameterMultipleInput):
|
elif isinstance(param, ParameterMultipleInput):
|
||||||
|
|
||||||
layers = param.value.split(';')
|
layers = value.split(';')
|
||||||
for layername in layers:
|
for layername in layers:
|
||||||
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
|
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
|
||||||
if isinstance(layer, QgsRasterLayer):
|
if isinstance(layer, QgsRasterLayer):
|
||||||
@ -274,7 +274,7 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
func = getattr(self.module, 'processInputs')
|
func = getattr(self.module, 'processInputs')
|
||||||
func(self)
|
func(self)
|
||||||
else:
|
else:
|
||||||
self.processInputs()
|
self.processInputs(parameters,context)
|
||||||
|
|
||||||
if hasattr(self.module, 'processCommand'):
|
if hasattr(self.module, 'processCommand'):
|
||||||
func = getattr(self.module, 'processCommand')
|
func = getattr(self.module, 'processCommand')
|
||||||
@ -288,7 +288,7 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
else:
|
else:
|
||||||
self.processOutputs()
|
self.processOutputs()
|
||||||
else:
|
else:
|
||||||
self.processInputs()
|
self.processInputs(parameters,context)
|
||||||
self.processCommand()
|
self.processCommand()
|
||||||
self.processOutputs()
|
self.processOutputs()
|
||||||
|
|
||||||
@ -317,13 +317,13 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
else:
|
else:
|
||||||
Grass7Utils.endGrass7Session()
|
Grass7Utils.endGrass7Session()
|
||||||
|
|
||||||
def processInputs(self):
|
def processInputs(self, parameters, context):
|
||||||
"""Prepare the GRASS import commands"""
|
"""Prepare the GRASS import commands"""
|
||||||
for param in self.parameters:
|
for param in self.parameterDefinitions():
|
||||||
if isinstance(param, ParameterRaster):
|
if isinstance(param, ParameterRaster):
|
||||||
if param.value is None:
|
if not param.name() in parameters():
|
||||||
continue
|
continue
|
||||||
value = param.value
|
value = parameters[param.name()]
|
||||||
|
|
||||||
# Check if the layer hasn't already been exported in, for
|
# Check if the layer hasn't already been exported in, for
|
||||||
# example, previous GRASS calls in this session
|
# example, previous GRASS calls in this session
|
||||||
@ -333,9 +333,9 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
self.setSessionProjectionFromLayer(value, self.commands)
|
self.setSessionProjectionFromLayer(value, self.commands)
|
||||||
self.commands.append(self.exportRasterLayer(value))
|
self.commands.append(self.exportRasterLayer(value))
|
||||||
if isinstance(param, ParameterVector):
|
if isinstance(param, ParameterVector):
|
||||||
if param.value is None:
|
if not param.name() in parameters():
|
||||||
continue
|
continue
|
||||||
value = param.value
|
value = parameters[param.name()]
|
||||||
if value in list(self.exportedLayers.keys()):
|
if value in list(self.exportedLayers.keys()):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
@ -344,9 +344,10 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
if isinstance(param, ParameterTable):
|
if isinstance(param, ParameterTable):
|
||||||
pass
|
pass
|
||||||
if isinstance(param, ParameterMultipleInput):
|
if isinstance(param, ParameterMultipleInput):
|
||||||
if param.value is None:
|
if not param.name() in parameters():
|
||||||
continue
|
continue
|
||||||
layers = param.value.split(';')
|
value = parameters[param.name()]
|
||||||
|
layers = value.split(';')
|
||||||
if layers is None or len(layers) == 0:
|
if layers is None or len(layers) == 0:
|
||||||
continue
|
continue
|
||||||
if param.datatype == dataobjects.TYPE_RASTER:
|
if param.datatype == dataobjects.TYPE_RASTER:
|
||||||
@ -384,7 +385,7 @@ class Grass7Algorithm(GeoAlgorithm):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(self.getDefaultCellsize())
|
command += ' res=' + str(self.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = \
|
alignToResolution = \
|
||||||
self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
|
@ -93,7 +93,7 @@ def orderedInput(alg, inputParameter, targetParameterDef, numSeq=None):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = \
|
alignToResolution = \
|
||||||
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
|
@ -57,7 +57,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -59,7 +59,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -75,13 +75,13 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
alg.commands.append(command)
|
alg.commands.append(command)
|
||||||
else:
|
else:
|
||||||
alg.processInputs()
|
alg.processInputs(context, parameters)
|
||||||
|
|
||||||
|
|
||||||
def processCommand(alg, parameters):
|
def processCommand(alg, parameters):
|
||||||
|
@ -65,7 +65,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -53,7 +53,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -67,7 +67,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -62,7 +62,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -55,7 +55,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -63,7 +63,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -53,7 +53,7 @@ def processInputs(alg):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(alg.getDefaultCellsize())
|
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
|
||||||
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
|
||||||
if alignToResolution:
|
if alignToResolution:
|
||||||
command += ' -a'
|
command += ' -a'
|
||||||
|
@ -45,7 +45,7 @@ def processInputs(alg):
|
|||||||
alg.exportedLayers[rstable]
|
alg.exportedLayers[rstable]
|
||||||
)
|
)
|
||||||
alg.commands.append(command)
|
alg.commands.append(command)
|
||||||
alg.processInputs()
|
alg.processInputs(context,parameters)
|
||||||
|
|
||||||
|
|
||||||
def processCommand(alg, parameters):
|
def processCommand(alg, parameters):
|
||||||
|
@ -37,7 +37,7 @@ def processInputs(alg):
|
|||||||
alg.exportedLayers[rstable]
|
alg.exportedLayers[rstable]
|
||||||
)
|
)
|
||||||
alg.commands.append(command)
|
alg.commands.append(command)
|
||||||
alg.processInputs()
|
alg.processInputs(parameters,context)
|
||||||
|
|
||||||
|
|
||||||
def processCommand(alg, parameters):
|
def processCommand(alg, parameters):
|
||||||
|
@ -115,7 +115,7 @@ class nviz7(GeoAlgorithm):
|
|||||||
if cellsize:
|
if cellsize:
|
||||||
command += ' res=' + str(cellsize)
|
command += ' res=' + str(cellsize)
|
||||||
else:
|
else:
|
||||||
command += ' res=' + str(self.getDefaultCellsize())
|
command += ' res=' + str(self.getDefaultCellsize(parameters,context))
|
||||||
commands.append(command)
|
commands.append(command)
|
||||||
|
|
||||||
command = 'nviz7'
|
command = 'nviz7'
|
||||||
@ -170,22 +170,22 @@ class nviz7(GeoAlgorithm):
|
|||||||
command += ' --overwrite -o'
|
command += ' --overwrite -o'
|
||||||
return (command, destFilename)
|
return (command, destFilename)
|
||||||
|
|
||||||
def getDefaultCellsize(self):
|
def getDefaultCellsize(self, parameters, context):
|
||||||
cellsize = 0
|
cellsize = 0
|
||||||
context = dataobjects.createContext()
|
for param in self.parameterDefinitions():
|
||||||
for param in self.parameters:
|
if param.name() in parameters:
|
||||||
if param.value:
|
value = parameters[param.name()]
|
||||||
if isinstance(param, ParameterRaster):
|
if isinstance(param, ParameterRaster):
|
||||||
if isinstance(param.value, QgsRasterLayer):
|
if isinstance(value, QgsRasterLayer):
|
||||||
layer = param.value
|
layer = value
|
||||||
else:
|
else:
|
||||||
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
|
layer = QgsProcessingUtils.mapLayerFromString(value, context)
|
||||||
cellsize = max(cellsize, (layer.extent().xMaximum() -
|
cellsize = max(cellsize, (layer.extent().xMaximum() -
|
||||||
layer.extent().xMinimum()) /
|
layer.extent().xMinimum()) /
|
||||||
layer.width())
|
layer.width())
|
||||||
elif isinstance(param, ParameterMultipleInput):
|
elif isinstance(param, ParameterMultipleInput):
|
||||||
|
|
||||||
layers = param.value.split(';')
|
layers = value.split(';')
|
||||||
for layername in layers:
|
for layername in layers:
|
||||||
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
|
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
|
||||||
if isinstance(layer, QgsRasterLayer):
|
if isinstance(layer, QgsRasterLayer):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user