Skip to content
Permalink
Browse files

Use parameterAs methods in OtbAlgorithm

This will accept stuff other than string type if needed by a user.
  • Loading branch information
rkanavath authored and nyalldawson committed Jan 21, 2019
1 parent 5666db2 commit e7ce84cb22d8521e6b7bdf9c01a97aee0848d8c7
Showing with 34 additions and 20 deletions.
  1. +34 −20 python/plugins/processing/algs/otb/OtbAlgorithm.py
@@ -35,6 +35,7 @@
QgsMessageLog,
QgsMapLayer,
QgsApplication,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterDefinition,
@@ -43,6 +44,11 @@
QgsProcessingParameterString,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterBoolean,
QgsProcessingParameterFile,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterEnum)

from processing.core.parameters import getParameterFromString
@@ -190,15 +196,13 @@ def preprocessParameters(self, parameters):
return valid_params

def processAlgorithm(self, parameters, context, feedback):

otb_cli_file = OtbUtils.cliPath()
command = '"{}" {} {}'.format(otb_cli_file, self.name(), OtbUtils.appFolder())
outputPixelType = None
for k, v in parameters.items():
# if value is None for a parameter we don't have any businees with this key
if v is None:
continue

# for 'outputpixeltype' parameter we find the pixeltype string from self.pixelTypes
if k == 'outputpixeltype':
outputPixelType = self.pixelTypes[int(parameters['outputpixeltype'])]
@@ -207,31 +211,41 @@ def processAlgorithm(self, parameters, context, feedback):
param = self.parameterDefinition(k)
if param.isDestination():
continue

if isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterVectorLayer)):
if isinstance(v, QgsMapLayer):
value = '"{}"'.format(v.source())
else:
value = '"{}"'.format(v)
elif isinstance(param, QgsProcessingParameterMultipleLayers):
value = ''
for item in v:
value += '"{}" '.format(item)
elif isinstance(param, QgsProcessingParameterCrs):
crs = self.parameterAsCrs(parameters, param.name(), context)
value = crs.authid().split('EPSG:')[1]

elif isinstance(param, QgsProcessingParameterEnum):
if isinstance(param, QgsProcessingParameterEnum):
value = self.parameterAsEnum(parameters, param.name(), context)
elif isinstance(param, QgsProcessingParameterBoolean):
value = self.parameterAsBool(parameters, param.name(), context)
elif isinstance(param, QgsProcessingParameterCrs):
crsValue = self.parameterAsCrs(parameters, param.name(), context)
value = crsValue.authid().split('EPSG:')[1]
elif isinstance(param, QgsProcessingParameterFile):
value = self.parameterAsFile(parameters, param.name(), context)
elif isinstance(param, QgsProcessingParameterMultipleLayers):
layers = self.parameterAsLayerList(parameters, param.name(), context)
if layers is None or len(layers) == 0:
continue
value = ' '.join(['"{}"'.format(layer.source()) for layer in layers])
elif isinstance(param, QgsProcessingParameterNumber):
if param.dataType() == QgsProcessingParameterNumber.Integer:
value = self.parameterAsInt(parameters, param.name(), context)
else:
value = self.parameterAsDouble(parameters, param.name(), context)
elif isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterVectorLayer)):
value = '"{}"'.format(self.parameterAsLayer(parameters, param.name(), context).source())
elif isinstance(param, QgsProcessingParameterString):
value = '"{}"'.format(parameters[param.name()])
else:
value = self.parameterAsString(parameters, param.name(), context)
# Use whatever is given
value = '"{}"'.format(parameters[param.name()])

command += ' -{} {}'.format(k, value)
# Check if value is set in above if elif ladder and update command string
if value and value is not None:
command += ' -{} {}'.format(k, value)

output_files = {}

for out in self.destinationParameterDefinitions():
filePath = self.parameterAsOutputLayer(parameters, out.name(), context)

output_files[out.name()] = filePath
if outputPixelType is not None:
command += ' -{} "{}" {}'.format(out.name(), filePath, outputPixelType)

0 comments on commit e7ce84c

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