Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
check for layer providers and authid
OTB only supports gdal and ogr providers for now. Maybe memory
provider can be easily supported using some conversion on the fly.
For the moment, we can go with this method. IO Formats in OTB not
using GDAL/OGR (LUM, ONERA) are not supported by QGis. Those can be
treated as simple files.

nyalldawson,  pointed that AUTHORITY id can have types not starting
with 'EPSG:'. Current otb takes just EPSG number and run with it. The
algorithm doesn't know what to with a number which is not EPSG because
it uses Gdal's 'ImportFromEpsg' method AFAIR.

QgsProecessing Exception is raised in both the above invalid cases.
  • Loading branch information
Rashad Kanavath authored and nyalldawson committed Feb 22, 2019
1 parent 70be3aa commit 08b4c1a
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions python/plugins/processing/algs/otb/OtbAlgorithm.py
Expand Up @@ -35,7 +35,7 @@
QgsMessageLog,
QgsMapLayer,
QgsApplication,
QgsProcessing,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterDefinition,
Expand Down Expand Up @@ -165,6 +165,7 @@ def defineCharacteristicsFromFile(self):
# reset list of options to 'self.pixelTypes'.
if name == 'outputpixeltype':
param.setOptions(self.pixelTypes)
param.setDefaultValue(self.pixelTypes.index('float'))

self.addParameter(param)
#parameter is added now and we must move to next line
Expand All @@ -185,10 +186,6 @@ def preprocessParameters(self, parameters):
if param is None:
continue

#if name of parameter is 'pixtype',
#it is considered valid if it has value other than float
if k == 'outputpixeltype' and self.pixelTypes[int(v)] == 'float':
continue
# Any other valid parameters have:
#- empty or no metadata
#- metadata without a 'group_key'
Expand All @@ -211,7 +208,8 @@ def processAlgorithm(self, parameters, context, feedback):
continue
# for 'outputpixeltype' parameter we find the pixeltype string from self.pixelTypes
if k == 'outputpixeltype':
outputPixelType = self.pixelTypes[int(parameters['outputpixeltype'])]
pixel_type = self.pixelTypes[int(parameters['outputpixeltype'])]
outputPixelType = None if pixel_type == 'float' else pixel_type
continue

param = self.parameterDefinition(k)
Expand All @@ -223,23 +221,30 @@ def processAlgorithm(self, parameters, context, feedback):
value = self.parameterAsBool(parameters, param.name(), context)
elif isinstance(param, QgsProcessingParameterCrs):
crsValue = self.parameterAsCrs(parameters, param.name(), context)
value = crsValue.authid().split('EPSG:')[1]
authid = crsValue.authid()
if authid.startswith('EPSG:'):
value = authid.split('EPSG:')[1]
else:
raise QgsProcessingException(
self.tr("Incorrect value for parameter '{}'. No EPSG code found in '{}'".format(
param.name(),
authid)))
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])
value = ' '.join(['"{}"'.format(self.getLayerSource(param.name(), layer)) 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())
value = '"{}"'.format(self.getLayerSource(param.name(), self.parameterAsLayer(parameters, param.name(), context)))
elif isinstance(param, QgsProcessingParameterString):
value = '"{}"'.format(parameters[param.name()])
value = '"{}"'.format(self.parameterAsString(parameters, param.name(), context))
else:
# Use whatever is given
value = '"{}"'.format(parameters[param.name()])
Expand Down Expand Up @@ -270,3 +275,12 @@ def processAlgorithm(self, parameters, context, feedback):
if o.name() in output_files:
result[o.name()] = output_files[o.name()]
return result

def getLayerSource(self, name, layer):
providerName = layer.dataProvider().name()
#TODO: add other provider support in OTB, eg: memory
if providerName in ['ogr', 'gdal']:
return layer.source()
else:
raise QgsProcessingException(
self.tr("OTB currently support only gdal and ogr provider. Parameter '{}' uses '{}' provider".format(name, providerName)))

0 comments on commit 08b4c1a

Please sign in to comment.