[processing] Fix escaping of srcnodata argument in Build Virtual Raster

alg
This commit is contained in:
Nyall Dawson 2021-02-26 12:02:59 +10:00
parent 33b0dbe761
commit c5dd8714a8
4 changed files with 6 additions and 3 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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__':

View File

@ -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'])