Skip to content

Commit

Permalink
Fix GDAL overwrite and some minor fixes (#42447)
Browse files Browse the repository at this point in the history
Automatically overwrite files in some gdal algorithms, in order to match the behavior of other processing algorithms which automatically overwrite existing files

Fixes #42248
  • Loading branch information
nicogodet committed Mar 24, 2021
1 parent 9747e1b commit bf9efcc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/ClipRasterByMask.py
Expand Up @@ -177,7 +177,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

arguments = []
arguments = ['-overwrite']

if sourceCrs.isValid():
arguments.append('-s_srs')
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/algs/gdal/buildvrt.py
Expand Up @@ -177,6 +177,7 @@ def commandName(self):

def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = [
'-overwrite',
'-resolution',
self.RESOLUTION_OPTIONS[self.parameterAsEnum(parameters, self.RESOLUTION, context)][1]
]
Expand Down
10 changes: 6 additions & 4 deletions python/plugins/processing/algs/gdal/gdalcalc.py
Expand Up @@ -87,10 +87,11 @@ def initAlgorithm(self, config=None):
self.tr('Input layer C'),
optional=True))
self.addParameter(
QgsProcessingParameterBand(self.BAND_C,
self.tr('Number of raster band for C'),
parentLayerParameterName=self.INPUT_C,
optional=True))
QgsProcessingParameterBand(
self.BAND_C,
self.tr('Number of raster band for C'),
parentLayerParameterName=self.INPUT_C,
optional=True))
self.addParameter(
QgsProcessingParameterRasterLayer(
self.INPUT_D,
Expand Down Expand Up @@ -191,6 +192,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
noData = None

arguments = [
'--overwrite',
f'--calc "{formula}"',
'--format',
GdalUtils.getFormatShortNameFromFilename(out),
Expand Down
42 changes: 21 additions & 21 deletions python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
Expand Up @@ -402,7 +402,7 @@ def testClipRasterByMask(self):
'MASK': mask,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-of JPEG -cutline ' +
'-overwrite -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline ' + source + ' ' +
outdir + '/check.jpg'])
# with NODATA value
Expand All @@ -412,7 +412,7 @@ def testClipRasterByMask(self):
'NODATA': 9999,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-of JPEG -cutline ' +
'-overwrite -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' +
outdir + '/check.jpg'])
# with "0" NODATA value
Expand All @@ -422,7 +422,7 @@ def testClipRasterByMask(self):
'NODATA': 0,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-of JPEG -cutline ' +
'-overwrite -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
outdir + '/check.jpg'])
# with "0" NODATA value and custom data type
Expand All @@ -433,7 +433,7 @@ def testClipRasterByMask(self):
'DATA_TYPE': 6,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-ot Float32 -of JPEG -cutline ' +
'-overwrite -ot Float32 -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
outdir + '/check.jpg'])
# with creation options
Expand All @@ -443,7 +443,7 @@ def testClipRasterByMask(self):
'OPTIONS': 'COMPRESS=DEFLATE|PREDICTOR=2|ZLEVEL=9',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-of JPEG -cutline ' +
'-overwrite -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 ' +
source + ' ' +
outdir + '/check.jpg'])
Expand All @@ -455,7 +455,7 @@ def testClipRasterByMask(self):
'EXTRA': '-nosrcalpha -wm 2048 -nomd',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-of JPEG -cutline ' +
'-overwrite -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline -multi -nosrcalpha -wm 2048 -nomd ' +
source + ' ' +
outdir + '/check.jpg'])
Expand Down Expand Up @@ -659,7 +659,7 @@ def testGdalCalc(self):
'FORMULA': formula,
'OUTPUT': output}, context, feedback),
['gdal_calc.py',
'--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])
'--overwrite --calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])

# check that formula is not escaped and formula is returned as it is
formula = 'A * 2' # <--- add spaces in the formula
Expand All @@ -669,7 +669,7 @@ def testGdalCalc(self):
'FORMULA': formula,
'OUTPUT': output}, context, feedback),
['gdal_calc.py',
'--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])
'--overwrite --calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])

# additional creation options
formula = 'A*2'
Expand All @@ -680,7 +680,7 @@ def testGdalCalc(self):
'OPTIONS': 'COMPRESS=JPEG|JPEG_QUALITY=75',
'OUTPUT': output}, context, feedback),
['gdal_calc.py',
'--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --co COMPRESS=JPEG --co JPEG_QUALITY=75 --outfile {}'.format(formula, source, output)])
'--overwrite --calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --co COMPRESS=JPEG --co JPEG_QUALITY=75 --outfile {}'.format(formula, source, output)])

# additional parameters
formula = 'A*2'
Expand All @@ -691,7 +691,7 @@ def testGdalCalc(self):
'EXTRA': '--debug --quiet',
'OUTPUT': output}, context, feedback),
['gdal_calc.py',
'--calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --debug --quiet --outfile {}'.format(formula, source, output)])
'--overwrite --calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --debug --quiet --outfile {}'.format(formula, source, output)])

def testGdalInfo(self):
context = QgsProcessingContext()
Expand Down Expand Up @@ -2550,7 +2550,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -r nearest ' +
'-overwrite -resolution average -separate -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# custom resolution
Expand All @@ -2561,7 +2561,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution lowest -separate -r nearest ' +
'-overwrite -resolution lowest -separate -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# single layer
Expand All @@ -2572,7 +2572,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -r nearest ' +
'-overwrite -resolution average -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# projection difference
Expand All @@ -2583,7 +2583,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -allow_projection_difference -r nearest ' +
'-overwrite -resolution average -separate -allow_projection_difference -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# add alpha band
Expand All @@ -2594,7 +2594,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -addalpha -r nearest ' +
'-overwrite -resolution average -separate -addalpha -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# assign CRS
Expand All @@ -2605,7 +2605,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -a_srs EPSG:3111 -r nearest ' +
'-overwrite -resolution average -separate -a_srs EPSG:3111 -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])

Expand All @@ -2617,7 +2617,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -a_srs EPSG:20936 -r nearest ' +
'-overwrite -resolution average -separate -a_srs EPSG:20936 -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# source NODATA
Expand All @@ -2628,7 +2628,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -r nearest -srcnodata -9999 ' +
'-overwrite -resolution average -separate -r nearest -srcnodata -9999 ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])

Expand All @@ -2639,7 +2639,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -r nearest -srcnodata "-9999 9999" ' +
'-overwrite -resolution average -separate -r nearest -srcnodata "-9999 9999" ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])

Expand All @@ -2650,7 +2650,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -r nearest ' +
'-overwrite -resolution average -separate -r nearest ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])
# additional parameters
Expand All @@ -2661,7 +2661,7 @@ def testBuildVrt(self):
cmd[1] = t[:t.find('-input_file_list') + 17] + t[t.find('buildvrtInputFiles.txt'):]
self.assertEqual(cmd,
['gdalbuildvrt',
'-resolution average -separate -r nearest -overwrite -optim RASTER -vrtnodata -9999 ' +
'-overwrite -resolution average -separate -r nearest -overwrite -optim RASTER -vrtnodata -9999 ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])

Expand Down

0 comments on commit bf9efcc

Please sign in to comment.