Skip to content

Commit

Permalink
[processing] Fix escaping of srcnodata argument in Build Virtual Raster
Browse files Browse the repository at this point in the history
alg
  • Loading branch information
nyalldawson committed Feb 26, 2021
1 parent 33b0dbe commit c5dd871
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -255,7 +255,8 @@ def escapeAndJoin(strList):
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:
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/buildvrt.py
Expand Up @@ -196,7 +196,8 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

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)
Expand Down
Expand Up @@ -339,6 +339,7 @@ def testCrsConversion(self):

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__':
Expand Down
Expand Up @@ -2606,7 +2606,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" ' +
'-resolution average -separate -r nearest -srcnodata -9999 ' +
'-input_file_list buildvrtInputFiles.txt ' +
outdir + '/check.vrt'])

Expand Down

0 comments on commit c5dd871

Please sign in to comment.