additional parameters for sieve algorithm

This commit is contained in:
Alexander Bruy 2019-06-24 19:53:31 +03:00
parent 27aa8113de
commit 61445ad0a2
2 changed files with 24 additions and 12 deletions

View File

@ -27,9 +27,11 @@ from qgis.PyQt.QtGui import QIcon
from qgis.core import (QgsRasterFileWriter,
QgsProcessingException,
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterString,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.tools.system import isWindows
@ -45,6 +47,7 @@ class sieve(GdalAlgorithm):
EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
NO_MASK = 'NO_MASK'
MASK_LAYER = 'MASK_LAYER'
EXTRA = 'EXTRA'
OUTPUT = 'OUTPUT'
def __init__(self):
@ -67,6 +70,13 @@ class sieve(GdalAlgorithm):
self.tr('Validity mask'),
optional=True))
extra_param = QgsProcessingParameterString(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('Sieved')))
def name(self):
@ -109,6 +119,10 @@ class sieve(GdalAlgorithm):
arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
extra = self.parameterAsString(parameters, self.EXTRA, context)
arguments.append(extra)
raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if raster is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

View File

@ -3093,10 +3093,6 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
# defaults
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'THRESHOLD': 10,
'EIGHT_CONNECTEDNESS': False,
'NO_MASK': False,
'MASK_LAYER': None,
'OUTPUT': outsource}, context, feedback),
['gdal_sieve.py',
'-st 10 -4 -of GTiff ' +
@ -3108,8 +3104,6 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
alg.getConsoleCommands({'INPUT': source,
'THRESHOLD': 16,
'EIGHT_CONNECTEDNESS': True,
'NO_MASK': False,
'MASK_LAYER': None,
'OUTPUT': outsource}, context, feedback),
['gdal_sieve.py',
'-st 16 -8 -of GTiff ' +
@ -3119,10 +3113,7 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
# without default mask layer
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'THRESHOLD': 10,
'EIGHT_CONNECTEDNESS': False,
'NO_MASK': True,
'MASK_LAYER': None,
'OUTPUT': outsource}, context, feedback),
['gdal_sieve.py',
'-st 10 -4 -nomask -of GTiff ' +
@ -3132,9 +3123,6 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
# defaults with external validity mask
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'THRESHOLD': 10,
'EIGHT_CONNECTEDNESS': False,
'NO_MASK': False,
'MASK_LAYER': mask,
'OUTPUT': outsource}, context, feedback),
['gdal_sieve.py',
@ -3144,6 +3132,16 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
source + ' ' +
outsource])
# additional parameters
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTRA': '-q',
'OUTPUT': outsource}, context, feedback),
['gdal_sieve.py',
'-st 10 -4 -of GTiff -q ' +
source + ' ' +
outsource])
def testGdal2Xyz(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()