Skip to content

Commit

Permalink
[processing] Make gdaladdo optional parameters truly optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jun 10, 2020
1 parent 2829f52 commit 5385ae6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
22 changes: 15 additions & 7 deletions python/plugins/processing/algs/gdal/gdaladdo.py
Expand Up @@ -51,7 +51,7 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.methods = ((self.tr('Nearest Neighbour'), 'nearest'),
self.methods = ((self.tr('Nearest Neighbour (default)'), 'nearest'),
(self.tr('Average'), 'average'),
(self.tr('Gaussian'), 'gauss'),
(self.tr('Cubic Convolution'), 'cubic'),
Expand All @@ -67,19 +67,26 @@ def initAlgorithm(self, config=None):

self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterString(self.LEVELS,
self.tr('Overview levels'),
defaultValue='2 4 8 16'))
self.addParameter(QgsProcessingParameterBoolean(self.CLEAN,
self.tr('Remove all existing overviews'),
defaultValue=False))

if GdalUtils.version() < 230000:
self.addParameter(QgsProcessingParameterString(self.LEVELS,
self.tr('Overview levels'),
defaultValue='2 4 8 16'))

params = []
if GdalUtils.version() >= 230000:
params.append(QgsProcessingParameterString(self.LEVELS,
self.tr('Overview levels (e.g. 2 4 8 16)'),
defaultValue=None,
optional=True))
params.append(QgsProcessingParameterEnum(self.RESAMPLING,
self.tr('Resampling method'),
options=[i[0] for i in self.methods],
allowMultiple=False,
defaultValue=0,
defaultValue=None,
optional=True))
params.append(QgsProcessingParameterEnum(self.FORMAT,
self.tr('Overviews format'),
Expand Down Expand Up @@ -125,8 +132,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = []
arguments.append(fileName)

arguments.append('-r')
arguments.append(self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1])
if self.RESAMPLING in parameters and parameters[self.RESAMPLING] is not None:
arguments.append('-r')
arguments.append(self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1])

ovrFormat = self.parameterAsEnum(parameters, self.FORMAT, context)
if ovrFormat == 1:
Expand Down
13 changes: 11 additions & 2 deletions python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
Expand Up @@ -34,6 +34,7 @@
unittest)

import AlgorithmsTestBase
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.algs.gdal.AssignProjection import AssignProjection
from processing.algs.gdal.ClipRasterByExtent import ClipRasterByExtent
from processing.algs.gdal.ClipRasterByMask import ClipRasterByMask
Expand Down Expand Up @@ -2145,15 +2146,23 @@ def testGdalAddo(self):
'CLEAN': False,
'EXTRA': '--config COMPRESS_OVERVIEW JPEG'}, context, feedback),
['gdaladdo',
source + ' ' + '-r nearest --config COMPRESS_OVERVIEW JPEG 2 4 8 16'])
source + ' ' + '--config COMPRESS_OVERVIEW JPEG 2 4 8 16'])

if GdalUtils.version() >= 230000:
# without levels
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'CLEAN': False}, context, feedback),
['gdaladdo',
source])

# without advanced params
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'LEVELS': '2 4 8 16',
'CLEAN': False}, context, feedback),
['gdaladdo',
source + ' ' + '-r nearest 2 4 8 16'])
source + ' ' + '2 4 8 16'])

def testSieve(self):
context = QgsProcessingContext()
Expand Down
Binary file not shown.

0 comments on commit 5385ae6

Please sign in to comment.