Skip to content

Commit

Permalink
Use parameterAs methods in OtbAlgorithm
Browse files Browse the repository at this point in the history
This will accept stuff other than string type if needed by a user.
  • Loading branch information
Rashad Kanavath authored and nyalldawson committed Feb 22, 2019
1 parent 5666db2 commit e7ce84c
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions python/plugins/processing/algs/otb/OtbAlgorithm.py
Expand Up @@ -35,6 +35,7 @@
QgsMessageLog,
QgsMapLayer,
QgsApplication,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterDefinition,
Expand All @@ -43,6 +44,11 @@
QgsProcessingParameterString,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterBoolean,
QgsProcessingParameterFile,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterEnum)

from processing.core.parameters import getParameterFromString
Expand Down Expand Up @@ -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'])]
Expand All @@ -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)
Expand Down

0 comments on commit e7ce84c

Please sign in to comment.