Skip to content

Commit

Permalink
[processing] add optional EXTRA parameter to gdalwarp algorithm (fix #…
Browse files Browse the repository at this point in the history
…20721)

This allows users to pass additional command-line arguments which are
not exposed in the algorithm definition. The most frequent use case is
enabling transparency and adding nodata values.

(cherry picked from commit bb2c366)
  • Loading branch information
alexbruy committed Jan 22, 2019
1 parent e1146df commit f74c280
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/plugins/processing/algs/gdal/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
QgsProcessingParameterEnum,
QgsProcessingParameterBoolean,
QgsProcessingParameterExtent,
QgsProcessingParameterString,
QgsProcessingParameterRasterDestination,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
Expand All @@ -59,6 +60,7 @@ class warp(GdalAlgorithm):
TARGET_EXTENT = 'TARGET_EXTENT'
TARGET_EXTENT_CRS = 'TARGET_EXTENT_CRS'
MULTITHREADING = 'MULTITHREADING'
EXTRA = 'EXTRA'
OUTPUT = 'OUTPUT'

TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
Expand Down Expand Up @@ -139,6 +141,13 @@ def initAlgorithm(self, config=None):
multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(multithreading_param)

extra_param = QgsProcessingParameterBoolean(self.EXTRA,
self.tr('Additional command-line parameters'),
defaultValue=None,
optional=True)
extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(extra_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Reprojected')))

Expand Down Expand Up @@ -228,6 +237,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if options:
arguments.extend(GdalUtils.parseCreationOptions(options))

if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
extra = self.parameterAsString(parameters, self.EXTRA, context)
arguments.append(extra)

arguments.append(inLayer.source())
arguments.append(out)

Expand Down
37 changes: 37 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,43 @@ def testWarp(self):
source + ' ' +
outdir + '/check.jpg'])

# with additional command-line parameter
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTRA': '-dstalpha',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-t_srs EPSG:4326 -r near -of JPEG -dstalpha ' +
source + ' ' +
outdir + '/check.jpg'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTRA': '-dstalpha -srcnodata -9999',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-t_srs EPSG:4326 -r near -of JPEG -dstalpha -srcnodata -9999 ' +
source + ' ' +
outdir + '/check.jpg'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTRA': '-dstalpha -srcnodata "-9999 -8888"',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-t_srs EPSG:4326 -r near -of JPEG -dstalpha -srcnodata "-9999 -8888" ' +
source + ' ' +
outdir + '/check.jpg'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTRA': '',
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-t_srs EPSG:4326 -r near -of JPEG ' +
source + ' ' +
outdir + '/check.jpg'])

def testRearrangeBands(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
Expand Down

0 comments on commit f74c280

Please sign in to comment.