Skip to content

Commit

Permalink
Add safeguard to all gdal algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 19, 2023
1 parent 9df62b9 commit 385fedd
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 25 deletions.
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/ClipRasterByExtent.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

if options:
arguments.extend(GdalUtils.parseCreationOptions(options))
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/ClipRasterByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

if self.parameterAsBoolean(parameters, self.KEEP_RESOLUTION, context):
arguments.append('-tr')
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/ColorRelief.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/GridAverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
8 changes: 7 additions & 1 deletion python/plugins/processing/algs/gdal/GridDataMetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,14 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)

if options:
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/GridInverseDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/GridLinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/GridNearestNeighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/aspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append(out)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/fillnodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-mask')
arguments.append(mask.source())

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
extra = self.parameterAsString(parameters, self.EXTRA, context)
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/hillshade.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
8 changes: 6 additions & 2 deletions python/plugins/processing/algs/gdal/nearblack.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
inLayer.source(),

'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
output_format,
'-o',
out,
'-near',
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/pansharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
panchromatic.source(),
spectral.source(),
out,
'-r',
self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1],
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
output_format
]

options = self.parameterAsString(parameters, self.OPTIONS, context)
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/pct2rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
output_format,
'-b',
str(self.parameterAsInt(parameters, self.BAND, context)),
]
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/proximity.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

if options:
arguments.extend(GdalUtils.parseCreationOptions(options))
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/rearrange_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/rgb2pct.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if raster is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
'-n',
str(self.parameterAsInt(parameters, self.NCOLORS, context)),
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
output_format,
raster.source(),
out
]
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/roughness.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
'roughness',
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
output_format,
'-b',
str(self.parameterAsInt(parameters, self.BAND, context))
]
Expand Down
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/gdal/sieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
extra = self.parameterAsString(parameters, self.EXTRA, context)
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/slope.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments = [
'slope',
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
output_format,
'-b',
str(self.parameterAsInt(parameters, self.BAND, context)),
'-s',
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/algs/gdal/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append('-ot ' + self.TYPES[data_type])

output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
if not output_format:
raise QgsProcessingException(self.tr('Output format is invalid'))

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
arguments.append(output_format)

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
Expand Down

0 comments on commit 385fedd

Please sign in to comment.