Skip to content

Commit 08b4c1a

Browse files
Rashad Kanavathnyalldawson
Rashad Kanavath
authored andcommitted
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.
1 parent 70be3aa commit 08b4c1a

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

python/plugins/processing/algs/otb/OtbAlgorithm.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
QgsMessageLog,
3636
QgsMapLayer,
3737
QgsApplication,
38-
QgsProcessing,
38+
QgsProcessingException,
3939
QgsProcessingAlgorithm,
4040
QgsProcessingParameterMultipleLayers,
4141
QgsProcessingParameterDefinition,
@@ -165,6 +165,7 @@ def defineCharacteristicsFromFile(self):
165165
# reset list of options to 'self.pixelTypes'.
166166
if name == 'outputpixeltype':
167167
param.setOptions(self.pixelTypes)
168+
param.setDefaultValue(self.pixelTypes.index('float'))
168169

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

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

217215
param = self.parameterDefinition(k)
@@ -223,23 +221,30 @@ def processAlgorithm(self, parameters, context, feedback):
223221
value = self.parameterAsBool(parameters, param.name(), context)
224222
elif isinstance(param, QgsProcessingParameterCrs):
225223
crsValue = self.parameterAsCrs(parameters, param.name(), context)
226-
value = crsValue.authid().split('EPSG:')[1]
224+
authid = crsValue.authid()
225+
if authid.startswith('EPSG:'):
226+
value = authid.split('EPSG:')[1]
227+
else:
228+
raise QgsProcessingException(
229+
self.tr("Incorrect value for parameter '{}'. No EPSG code found in '{}'".format(
230+
param.name(),
231+
authid)))
227232
elif isinstance(param, QgsProcessingParameterFile):
228233
value = self.parameterAsFile(parameters, param.name(), context)
229234
elif isinstance(param, QgsProcessingParameterMultipleLayers):
230235
layers = self.parameterAsLayerList(parameters, param.name(), context)
231236
if layers is None or len(layers) == 0:
232237
continue
233-
value = ' '.join(['"{}"'.format(layer.source()) for layer in layers])
238+
value = ' '.join(['"{}"'.format(self.getLayerSource(param.name(), layer)) for layer in layers])
234239
elif isinstance(param, QgsProcessingParameterNumber):
235240
if param.dataType() == QgsProcessingParameterNumber.Integer:
236241
value = self.parameterAsInt(parameters, param.name(), context)
237242
else:
238243
value = self.parameterAsDouble(parameters, param.name(), context)
239244
elif isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterVectorLayer)):
240-
value = '"{}"'.format(self.parameterAsLayer(parameters, param.name(), context).source())
245+
value = '"{}"'.format(self.getLayerSource(param.name(), self.parameterAsLayer(parameters, param.name(), context)))
241246
elif isinstance(param, QgsProcessingParameterString):
242-
value = '"{}"'.format(parameters[param.name()])
247+
value = '"{}"'.format(self.parameterAsString(parameters, param.name(), context))
243248
else:
244249
# Use whatever is given
245250
value = '"{}"'.format(parameters[param.name()])
@@ -270,3 +275,12 @@ def processAlgorithm(self, parameters, context, feedback):
270275
if o.name() in output_files:
271276
result[o.name()] = output_files[o.name()]
272277
return result
278+
279+
def getLayerSource(self, name, layer):
280+
providerName = layer.dataProvider().name()
281+
#TODO: add other provider support in OTB, eg: memory
282+
if providerName in ['ogr', 'gdal']:
283+
return layer.source()
284+
else:
285+
raise QgsProcessingException(
286+
self.tr("OTB currently support only gdal and ogr provider. Parameter '{}' uses '{}' provider".format(name, providerName)))

0 commit comments

Comments
 (0)