mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-12 00:06:54 -05:00
[processing] add optional EXTRA parameter to gdalwarp algorithm (fix #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.
This commit is contained in:
parent
1253707337
commit
bb2c3663b0
@ -38,6 +38,7 @@ from qgis.core import (QgsRasterFileWriter,
|
|||||||
QgsProcessingParameterEnum,
|
QgsProcessingParameterEnum,
|
||||||
QgsProcessingParameterBoolean,
|
QgsProcessingParameterBoolean,
|
||||||
QgsProcessingParameterExtent,
|
QgsProcessingParameterExtent,
|
||||||
|
QgsProcessingParameterString,
|
||||||
QgsProcessingParameterRasterDestination,
|
QgsProcessingParameterRasterDestination,
|
||||||
QgsProcessingUtils)
|
QgsProcessingUtils)
|
||||||
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
||||||
@ -59,6 +60,7 @@ class warp(GdalAlgorithm):
|
|||||||
TARGET_EXTENT = 'TARGET_EXTENT'
|
TARGET_EXTENT = 'TARGET_EXTENT'
|
||||||
TARGET_EXTENT_CRS = 'TARGET_EXTENT_CRS'
|
TARGET_EXTENT_CRS = 'TARGET_EXTENT_CRS'
|
||||||
MULTITHREADING = 'MULTITHREADING'
|
MULTITHREADING = 'MULTITHREADING'
|
||||||
|
EXTRA = 'EXTRA'
|
||||||
OUTPUT = 'OUTPUT'
|
OUTPUT = 'OUTPUT'
|
||||||
|
|
||||||
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
|
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
|
||||||
@ -139,6 +141,13 @@ class warp(GdalAlgorithm):
|
|||||||
multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||||
self.addParameter(multithreading_param)
|
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.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
|
||||||
self.tr('Reprojected')))
|
self.tr('Reprojected')))
|
||||||
|
|
||||||
@ -228,6 +237,10 @@ class warp(GdalAlgorithm):
|
|||||||
if options:
|
if options:
|
||||||
arguments.extend(GdalUtils.parseCreationOptions(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(inLayer.source())
|
||||||
arguments.append(out)
|
arguments.append(out)
|
||||||
|
|
||||||
|
|||||||
@ -2213,6 +2213,43 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
|
|||||||
source + ' ' +
|
source + ' ' +
|
||||||
outdir + '/check.jpg'])
|
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):
|
def testRearrangeBands(self):
|
||||||
context = QgsProcessingContext()
|
context = QgsProcessingContext()
|
||||||
feedback = QgsProcessingFeedback()
|
feedback = QgsProcessingFeedback()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user