Skip to content
Permalink
Browse files
Fix many issues in gdal algs
Manual backport of #41893 (#41847) to 3.16 branch
  • Loading branch information
agiudiceandrea authored and nyalldawson committed Apr 17, 2021
1 parent 973ccac commit 9d81c6001dd99a442e058c5000597441660aa76e
@@ -123,13 +123,16 @@ def parametersHaveChanged(self):
parameters = self.dialog.createProcessingParameters()
for output in self.algorithm().destinationParameterDefinitions():
if not output.name() in parameters or parameters[output.name()] is None:
parameters[output.name()] = self.tr("[temporary file]")
if not output.flags() & QgsProcessingParameterDefinition.FlagOptional:
parameters[output.name()] = self.tr("[temporary file]")
for p in self.algorithm().parameterDefinitions():
if p.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

if (not p.name() in parameters and not p.flags() & QgsProcessingParameterDefinition.FlagOptional) \
or (not p.checkValueIsAcceptable(parameters[p.name()])):
if p.flags() & QgsProcessingParameterDefinition.FlagOptional and p.name() not in parameters:
continue

if p.name() not in parameters or not p.checkValueIsAcceptable(parameters[p.name()]):
# not ready yet
self.text.setPlainText('')
return
@@ -217,12 +217,13 @@ def getFormatShortNameFromFilename(filename):

@staticmethod
def escapeAndJoin(strList):
escChars = [' ', '&', '(', ')', '"']
escChars = [' ', '&', '(', ')', '"', ';']
joined = ''
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:
@@ -194,7 +194,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)
@@ -251,14 +251,14 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if shapeEncoding:
arguments.append('--config')
arguments.append('SHAPE_ENCODING')
arguments.append('"' + shapeEncoding + '"')
arguments.append(shapeEncoding)
arguments.append('-f')
arguments.append('PostgreSQL')
arguments.append('PG:"')
for token in QgsDataSourceUri(uri).connectionInfo(executing).split(' '):
arguments.append(token)
arguments.append('active_schema={}'.format(schema or 'public'))
arguments.append('"')

connection_parts = QgsDataSourceUri(uri).connectionInfo(executing).split(' ')
connection_parts.append('active_schema={}'.format(schema or 'public'))
arguments.append('PG:{}'.format(' '.join(connection_parts)))

arguments.append(dimstring)
arguments.append(ogrLayer)
arguments.append(layername)
@@ -206,7 +206,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
delimiter = self.parameterAsString(parameters, self.DELIMITER, context)
if delimiter:
arguments.append('-csvDelim')
arguments.append('"{}"'.format(delimiter))
arguments.append(delimiter)

arguments.append('-targetDir')
arguments.append(self.parameterAsString(parameters, self.OUTPUT, context))
@@ -349,7 +349,8 @@ def testCrsConversion(self):
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')

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, "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__':
@@ -1707,6 +1707,24 @@ def testRetile(self):
'-ps 256 256 -overlap 0 -levels 1 -s_srs EPSG:3111 -r near -ot Float32 -targetDir {} {}'.format(outdir, source)
])

self.assertEqual(
alg.getConsoleCommands({'INPUT': [source],
'OUTPUT_CSV': 'out.csv',
'DELIMITER': '',
'OUTPUT': outdir}, context, feedback),
['gdal_retile.py',
'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -csv out.csv -targetDir {} '.format(outdir) +
source])

self.assertEqual(
alg.getConsoleCommands({'INPUT': [source],
'OUTPUT_CSV': 'out.csv',
'DELIMITER': ';',
'OUTPUT': outdir}, context, feedback),
['gdal_retile.py',
'-ps 256 256 -overlap 0 -levels 1 -r near -ot Float32 -csv out.csv -csvDelim ";" -targetDir {} '.format(outdir) +
source])

# additional parameters
self.assertEqual(
alg.getConsoleCommands({'INPUT': [source],
@@ -2580,7 +2598,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'])

0 comments on commit 9d81c60

Please sign in to comment.