From c5dd8714a8c5ce4e5f4cfe967ebd91cacb97ff01 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 26 Feb 2021 12:02:59 +1000 Subject: [PATCH] [processing] Fix escaping of srcnodata argument in Build Virtual Raster alg --- python/plugins/processing/algs/gdal/GdalUtils.py | 3 ++- python/plugins/processing/algs/gdal/buildvrt.py | 3 ++- python/plugins/processing/tests/GdalAlgorithmsGeneralTest.py | 1 + python/plugins/processing/tests/GdalAlgorithmsRasterTest.py | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/python/plugins/processing/algs/gdal/GdalUtils.py b/python/plugins/processing/algs/gdal/GdalUtils.py index 1fbf41c60a6..c1ba0af0170 100644 --- a/python/plugins/processing/algs/gdal/GdalUtils.py +++ b/python/plugins/processing/algs/gdal/GdalUtils.py @@ -255,7 +255,8 @@ class GdalUtils: for s in strList: if not isinstance(s, str): s = str(s) - if s and s[0] != '-' and any(c in s for c in escChars): + # don't escape if command starts with - and isn't a negative number, e.g. -9999 + if s and re.match(r'^([^-]|-\d)', s) and any(c in s for c in escChars): escaped = '"' + s.replace('\\', '\\\\').replace('"', '"""') \ + '"' else: diff --git a/python/plugins/processing/algs/gdal/buildvrt.py b/python/plugins/processing/algs/gdal/buildvrt.py index 8b07df108b6..5501a438bf8 100644 --- a/python/plugins/processing/algs/gdal/buildvrt.py +++ b/python/plugins/processing/algs/gdal/buildvrt.py @@ -196,7 +196,8 @@ class buildvrt(GdalAlgorithm): if self.SRC_NODATA in parameters and parameters[self.SRC_NODATA] not in (None, ''): nodata = self.parameterAsString(parameters, self.SRC_NODATA, context) - arguments.append('-srcnodata "{}"'.format(nodata)) + arguments.append('-srcnodata') + arguments.append(nodata) 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/tests/GdalAlgorithmsGeneralTest.py b/python/plugins/processing/tests/GdalAlgorithmsGeneralTest.py index d9c7a668fd5..135907be6c7 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsGeneralTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsGeneralTest.py @@ -339,6 +339,7 @@ class TestGdalAlgorithms(unittest.TestCase): def testEscapeAndJoin(self): self.assertEqual(GdalUtils.escapeAndJoin([1, "a", "a b", "a&b", "a(b)", ";"]), '1 a "a b" "a&b" "a(b)" ";"') + self.assertEqual(GdalUtils.escapeAndJoin([1, "-srcnodata", "--srcnodata", "-9999 9999"]), '1 -srcnodata --srcnodata "-9999 9999"') if __name__ == '__main__': diff --git a/python/plugins/processing/tests/GdalAlgorithmsRasterTest.py b/python/plugins/processing/tests/GdalAlgorithmsRasterTest.py index 744a7ee9c42..d657f01baf7 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsRasterTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsRasterTest.py @@ -2606,7 +2606,7 @@ class TestGdalRasterAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsT 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" ' + + '-resolution average -separate -r nearest -srcnodata -9999 ' + '-input_file_list buildvrtInputFiles.txt ' + outdir + '/check.vrt'])