Skip to content

Commit

Permalink
Adapt more python code to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 28f7a8b commit 87fc31d
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 46 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -95,10 +95,11 @@ def shortHelpString(self):
'''.format(self.commandName(), url)

def commandName(self):
parameters = {}
for output in self.outputs:
output.setValue("dummy")
for param in self.parameters:
param.setValue("1")
for param in self.parameterDefinitions():
parameters[param.name()] = "1"
name = self.getConsoleCommands(parameters)[0]
if name.endswith(".py"):
name = name[:-3]
Expand Down
43 changes: 22 additions & 21 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -132,11 +132,11 @@ def getParameterDescriptions(self):
lines = infile.readlines()
for i in range(len(lines)):
if lines[i].startswith('<DT><b>'):
for param in self.parameters:
searchLine = '<b>' + param.name + '</b>'
for param in self.parameterDefinitions():
searchLine = '<b>' + param.name() + '</b>'
if searchLine in lines[i]:
i += 1
descs[param.name] = (lines[i])[4:-6]
descs[param.name()] = (lines[i])[4:-6]
break

except Exception:
Expand Down Expand Up @@ -217,22 +217,22 @@ def defineCharacteristicsFromFile(self):
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(param)

def getDefaultCellsize(self):
context = dataobjects.createContext()
def getDefaultCellsize(self, parameters, context):
cellsize = 0
for param in self.parameters:
if param.value:
for param in self.parameterDefinitions():
if param.name() in parameters:
value = parameters[param.name()]
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
if isinstance(value, QgsRasterLayer):
layer = value
else:
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
cellsize = max(cellsize, (layer.extent().xMaximum() -
layer.extent().xMinimum()) /
layer.width())
elif isinstance(param, ParameterMultipleInput):

layers = param.value.split(';')
layers = value.split(';')
for layername in layers:
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
if isinstance(layer, QgsRasterLayer):
Expand Down Expand Up @@ -274,7 +274,7 @@ def processAlgorithm(self, parameters, context, feedback):
func = getattr(self.module, 'processInputs')
func(self)
else:
self.processInputs()
self.processInputs(parameters,context)

if hasattr(self.module, 'processCommand'):
func = getattr(self.module, 'processCommand')
Expand All @@ -288,7 +288,7 @@ def processAlgorithm(self, parameters, context, feedback):
else:
self.processOutputs()
else:
self.processInputs()
self.processInputs(parameters,context)
self.processCommand()
self.processOutputs()

Expand Down Expand Up @@ -317,13 +317,13 @@ def processAlgorithm(self, parameters, context, feedback):
else:
Grass7Utils.endGrass7Session()

def processInputs(self):
def processInputs(self, parameters, context):
"""Prepare the GRASS import commands"""
for param in self.parameters:
for param in self.parameterDefinitions():
if isinstance(param, ParameterRaster):
if param.value is None:
if not param.name() in parameters():
continue
value = param.value
value = parameters[param.name()]

# Check if the layer hasn't already been exported in, for
# example, previous GRASS calls in this session
Expand All @@ -333,9 +333,9 @@ def processInputs(self):
self.setSessionProjectionFromLayer(value, self.commands)
self.commands.append(self.exportRasterLayer(value))
if isinstance(param, ParameterVector):
if param.value is None:
if not param.name() in parameters():
continue
value = param.value
value = parameters[param.name()]
if value in list(self.exportedLayers.keys()):
continue
else:
Expand All @@ -344,9 +344,10 @@ def processInputs(self):
if isinstance(param, ParameterTable):
pass
if isinstance(param, ParameterMultipleInput):
if param.value is None:
if not param.name() in parameters():
continue
layers = param.value.split(';')
value = parameters[param.name()]
layers = value.split(';')
if layers is None or len(layers) == 0:
continue
if param.datatype == dataobjects.TYPE_RASTER:
Expand Down Expand Up @@ -384,7 +385,7 @@ def processInputs(self):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(self.getDefaultCellsize())
command += ' res=' + str(self.getDefaultCellsize(parameters, context))
alignToResolution = \
self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/i.py
Expand Up @@ -93,7 +93,7 @@ def orderedInput(alg, inputParameter, targetParameterDef, numSeq=None):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = \
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
Expand Down
Expand Up @@ -57,7 +57,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_blend_rgb.py
Expand Up @@ -59,7 +59,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/grass7/ext/r_category.py
Expand Up @@ -75,13 +75,13 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
alg.commands.append(command)
else:
alg.processInputs()
alg.processInputs(context, parameters)


def processCommand(alg, parameters):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_colors.py
Expand Up @@ -65,7 +65,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
Expand Up @@ -53,7 +53,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_mapcalc.py
Expand Up @@ -67,7 +67,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_null.py
Expand Up @@ -62,7 +62,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_rgb.py
Expand Up @@ -55,7 +55,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_shade.py
Expand Up @@ -63,7 +63,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_what_color.py
Expand Up @@ -53,7 +53,7 @@ def processInputs(alg):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize())
command += ' res=' + str(alg.getDefaultCellsize(parameters,context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/v_lrs_segment.py
Expand Up @@ -45,7 +45,7 @@ def processInputs(alg):
alg.exportedLayers[rstable]
)
alg.commands.append(command)
alg.processInputs()
alg.processInputs(context,parameters)


def processCommand(alg, parameters):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/v_lrs_where.py
Expand Up @@ -37,7 +37,7 @@ def processInputs(alg):
alg.exportedLayers[rstable]
)
alg.commands.append(command)
alg.processInputs()
alg.processInputs(parameters,context)


def processCommand(alg, parameters):
Expand Down
18 changes: 9 additions & 9 deletions python/plugins/processing/algs/grass7/nviz7.py
Expand Up @@ -115,7 +115,7 @@ def processAlgorithm(self, parameters, context, feedback):
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(self.getDefaultCellsize())
command += ' res=' + str(self.getDefaultCellsize(parameters,context))
commands.append(command)

command = 'nviz7'
Expand Down Expand Up @@ -170,22 +170,22 @@ def exportRasterLayer(self, layer):
command += ' --overwrite -o'
return (command, destFilename)

def getDefaultCellsize(self):
def getDefaultCellsize(self, parameters, context):
cellsize = 0
context = dataobjects.createContext()
for param in self.parameters:
if param.value:
for param in self.parameterDefinitions():
if param.name() in parameters:
value = parameters[param.name()]
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
if isinstance(value, QgsRasterLayer):
layer = value
else:
layer = QgsProcessingUtils.mapLayerFromString(param.value, context)
layer = QgsProcessingUtils.mapLayerFromString(value, context)
cellsize = max(cellsize, (layer.extent().xMaximum() -
layer.extent().xMinimum()) /
layer.width())
elif isinstance(param, ParameterMultipleInput):

layers = param.value.split(';')
layers = value.split(';')
for layername in layers:
layer = QgsProcessingUtils.mapLayerFromString(layername, context)
if isinstance(layer, QgsRasterLayer):
Expand Down

0 comments on commit 87fc31d

Please sign in to comment.