From 385fedd23246e428e3f4ae519e015788362c5b11 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Thu, 19 Oct 2023 13:31:42 +0200 Subject: [PATCH] Add safeguard to all gdal algorithms --- python/plugins/processing/algs/gdal/ClipRasterByExtent.py | 6 +++++- python/plugins/processing/algs/gdal/ClipRasterByMask.py | 6 +++++- python/plugins/processing/algs/gdal/ColorRelief.py | 6 +++++- python/plugins/processing/algs/gdal/GridAverage.py | 7 ++++++- python/plugins/processing/algs/gdal/GridDataMetrics.py | 8 +++++++- .../plugins/processing/algs/gdal/GridInverseDistance.py | 7 ++++++- .../algs/gdal/GridInverseDistanceNearestNeighbor.py | 7 ++++++- python/plugins/processing/algs/gdal/GridLinear.py | 7 ++++++- .../plugins/processing/algs/gdal/GridNearestNeighbor.py | 7 ++++++- python/plugins/processing/algs/gdal/aspect.py | 6 +++++- python/plugins/processing/algs/gdal/fillnodata.py | 6 +++++- python/plugins/processing/algs/gdal/hillshade.py | 6 +++++- python/plugins/processing/algs/gdal/merge.py | 6 +++++- python/plugins/processing/algs/gdal/nearblack.py | 8 ++++++-- python/plugins/processing/algs/gdal/pansharp.py | 6 +++++- python/plugins/processing/algs/gdal/pct2rgb.py | 6 +++++- python/plugins/processing/algs/gdal/proximity.py | 6 +++++- python/plugins/processing/algs/gdal/rasterize.py | 7 ++++++- python/plugins/processing/algs/gdal/rearrange_bands.py | 6 +++++- python/plugins/processing/algs/gdal/rgb2pct.py | 6 +++++- python/plugins/processing/algs/gdal/roughness.py | 6 +++++- python/plugins/processing/algs/gdal/sieve.py | 7 ++++++- python/plugins/processing/algs/gdal/slope.py | 6 +++++- python/plugins/processing/algs/gdal/translate.py | 6 +++++- 24 files changed, 130 insertions(+), 25 deletions(-) diff --git a/python/plugins/processing/algs/gdal/ClipRasterByExtent.py b/python/plugins/processing/algs/gdal/ClipRasterByExtent.py index aa6af4f8c4f3..8583d2931639 100644 --- a/python/plugins/processing/algs/gdal/ClipRasterByExtent.py +++ b/python/plugins/processing/algs/gdal/ClipRasterByExtent.py @@ -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)) diff --git a/python/plugins/processing/algs/gdal/ClipRasterByMask.py b/python/plugins/processing/algs/gdal/ClipRasterByMask.py index dec0cb4b64ec..ee0a10960a85 100644 --- a/python/plugins/processing/algs/gdal/ClipRasterByMask.py +++ b/python/plugins/processing/algs/gdal/ClipRasterByMask.py @@ -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') diff --git a/python/plugins/processing/algs/gdal/ColorRelief.py b/python/plugins/processing/algs/gdal/ColorRelief.py index 3ec8abc4e244..050c597440f1 100644 --- a/python/plugins/processing/algs/gdal/ColorRelief.py +++ b/python/plugins/processing/algs/gdal/ColorRelief.py @@ -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))) diff --git a/python/plugins/processing/algs/gdal/GridAverage.py b/python/plugins/processing/algs/gdal/GridAverage.py index f3b73b3193be..62bc7ead1a32 100644 --- a/python/plugins/processing/algs/gdal/GridAverage.py +++ b/python/plugins/processing/algs/gdal/GridAverage.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/GridDataMetrics.py b/python/plugins/processing/algs/gdal/GridDataMetrics.py index ca56dce4c2d0..8b6a28100240 100644 --- a/python/plugins/processing/algs/gdal/GridDataMetrics.py +++ b/python/plugins/processing/algs/gdal/GridDataMetrics.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/GridInverseDistance.py b/python/plugins/processing/algs/gdal/GridInverseDistance.py index dad7bc651488..03641ec256b1 100644 --- a/python/plugins/processing/algs/gdal/GridInverseDistance.py +++ b/python/plugins/processing/algs/gdal/GridInverseDistance.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/GridInverseDistanceNearestNeighbor.py b/python/plugins/processing/algs/gdal/GridInverseDistanceNearestNeighbor.py index cc527e461cac..259b31f42fd3 100644 --- a/python/plugins/processing/algs/gdal/GridInverseDistanceNearestNeighbor.py +++ b/python/plugins/processing/algs/gdal/GridInverseDistanceNearestNeighbor.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/GridLinear.py b/python/plugins/processing/algs/gdal/GridLinear.py index 07ae673a9dc3..ebee8f62215a 100644 --- a/python/plugins/processing/algs/gdal/GridLinear.py +++ b/python/plugins/processing/algs/gdal/GridLinear.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/GridNearestNeighbor.py b/python/plugins/processing/algs/gdal/GridNearestNeighbor.py index ba7df682950a..9e91e69f7171 100644 --- a/python/plugins/processing/algs/gdal/GridNearestNeighbor.py +++ b/python/plugins/processing/algs/gdal/GridNearestNeighbor.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/aspect.py b/python/plugins/processing/algs/gdal/aspect.py index 15d9efd1b2a0..4667d99ae531 100644 --- a/python/plugins/processing/algs/gdal/aspect.py +++ b/python/plugins/processing/algs/gdal/aspect.py @@ -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))) diff --git a/python/plugins/processing/algs/gdal/fillnodata.py b/python/plugins/processing/algs/gdal/fillnodata.py index 8b2cd40e237b..d0792e9ba184 100644 --- a/python/plugins/processing/algs/gdal/fillnodata.py +++ b/python/plugins/processing/algs/gdal/fillnodata.py @@ -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) diff --git a/python/plugins/processing/algs/gdal/hillshade.py b/python/plugins/processing/algs/gdal/hillshade.py index 7fa9224954e2..213ad54ef7e0 100644 --- a/python/plugins/processing/algs/gdal/hillshade.py +++ b/python/plugins/processing/algs/gdal/hillshade.py @@ -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))) diff --git a/python/plugins/processing/algs/gdal/merge.py b/python/plugins/processing/algs/gdal/merge.py index 34349dacf266..51c952a7393f 100644 --- a/python/plugins/processing/algs/gdal/merge.py +++ b/python/plugins/processing/algs/gdal/merge.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/nearblack.py b/python/plugins/processing/algs/gdal/nearblack.py index 4938c314b3dc..d7541d491f89 100644 --- a/python/plugins/processing/algs/gdal/nearblack.py +++ b/python/plugins/processing/algs/gdal/nearblack.py @@ -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', diff --git a/python/plugins/processing/algs/gdal/pansharp.py b/python/plugins/processing/algs/gdal/pansharp.py index 80ad22d246ed..ef678d435eb6 100644 --- a/python/plugins/processing/algs/gdal/pansharp.py +++ b/python/plugins/processing/algs/gdal/pansharp.py @@ -116,6 +116,10 @@ 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(), @@ -123,7 +127,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True): '-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) diff --git a/python/plugins/processing/algs/gdal/pct2rgb.py b/python/plugins/processing/algs/gdal/pct2rgb.py index b3b6a7a3275a..a137d7e2d509 100644 --- a/python/plugins/processing/algs/gdal/pct2rgb.py +++ b/python/plugins/processing/algs/gdal/pct2rgb.py @@ -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)), ] diff --git a/python/plugins/processing/algs/gdal/proximity.py b/python/plugins/processing/algs/gdal/proximity.py index 54ea4b7274cc..c5050e7ee061 100644 --- a/python/plugins/processing/algs/gdal/proximity.py +++ b/python/plugins/processing/algs/gdal/proximity.py @@ -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)) diff --git a/python/plugins/processing/algs/gdal/rasterize.py b/python/plugins/processing/algs/gdal/rasterize.py index 9a203c8fca29..0654cefdb2f8 100644 --- a/python/plugins/processing/algs/gdal/rasterize.py +++ b/python/plugins/processing/algs/gdal/rasterize.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/rearrange_bands.py b/python/plugins/processing/algs/gdal/rearrange_bands.py index 432ee1ef89a1..2c309080392b 100644 --- a/python/plugins/processing/algs/gdal/rearrange_bands.py +++ b/python/plugins/processing/algs/gdal/rearrange_bands.py @@ -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: diff --git a/python/plugins/processing/algs/gdal/rgb2pct.py b/python/plugins/processing/algs/gdal/rgb2pct.py index 93c767d7e3e4..9b59dca3a1b5 100644 --- a/python/plugins/processing/algs/gdal/rgb2pct.py +++ b/python/plugins/processing/algs/gdal/rgb2pct.py @@ -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 ] diff --git a/python/plugins/processing/algs/gdal/roughness.py b/python/plugins/processing/algs/gdal/roughness.py index 43d19f9ca257..2487d7ebdd4a 100644 --- a/python/plugins/processing/algs/gdal/roughness.py +++ b/python/plugins/processing/algs/gdal/roughness.py @@ -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)) ] diff --git a/python/plugins/processing/algs/gdal/sieve.py b/python/plugins/processing/algs/gdal/sieve.py index 35755c880888..6a140dae1163 100644 --- a/python/plugins/processing/algs/gdal/sieve.py +++ b/python/plugins/processing/algs/gdal/sieve.py @@ -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) diff --git a/python/plugins/processing/algs/gdal/slope.py b/python/plugins/processing/algs/gdal/slope.py index a16c880dcb1d..e95215f8c736 100644 --- a/python/plugins/processing/algs/gdal/slope.py +++ b/python/plugins/processing/algs/gdal/slope.py @@ -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', diff --git a/python/plugins/processing/algs/gdal/translate.py b/python/plugins/processing/algs/gdal/translate.py index 69c0f8defbb3..4bfcd94adc4d 100644 --- a/python/plugins/processing/algs/gdal/translate.py +++ b/python/plugins/processing/algs/gdal/translate.py @@ -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: