From c50a5cd8bb5226dfb5c6a518bf54a84fab57c992 Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Tue, 1 Jan 2019 15:20:21 +0200 Subject: [PATCH] [processing] fix handling of the mask layer in the gdal_sieve algorithm (fix #20713) (cherry picked from commit a5365e26ebdfab2330eacb56167040a044f97f3c) --- python/plugins/processing/algs/gdal/sieve.py | 2 +- .../processing/tests/GdalAlgorithmsTest.py | 66 +++++++++++++++++++ .../tests/testdata/gdal_algorithm_tests.yaml | 18 ----- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/python/plugins/processing/algs/gdal/sieve.py b/python/plugins/processing/algs/gdal/sieve.py index 7f8eb59450aa..c20ef7ef10d2 100644 --- a/python/plugins/processing/algs/gdal/sieve.py +++ b/python/plugins/processing/algs/gdal/sieve.py @@ -104,7 +104,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True): if self.parameterAsBool(parameters, self.NO_MASK, context): arguments.append('-nomask') - mask = self.parameterAsRasterLayer(parameters, self.INPUT, context) + mask = self.parameterAsRasterLayer(parameters, self.MASK_LAYER, context) if mask: arguments.append('-mask {}'.format(mask.source())) diff --git a/python/plugins/processing/tests/GdalAlgorithmsTest.py b/python/plugins/processing/tests/GdalAlgorithmsTest.py index ce2cb375b90f..7fd34c416b22 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsTest.py @@ -60,6 +60,7 @@ from processing.algs.gdal.fillnodata import fillnodata from processing.algs.gdal.rearrange_bands import rearrange_bands from processing.algs.gdal.gdaladdo import gdaladdo +from processing.algs.gdal.sieve import sieve from processing.tools.system import isWindows @@ -2448,6 +2449,71 @@ def testGdalAddo(self): ['gdaladdo', source + ' ' + '-r nearest 2 4 8 16']) + def testSieve(self): + context = QgsProcessingContext() + feedback = QgsProcessingFeedback() + source = os.path.join(testDataPath, 'dem.tif') + mask = os.path.join(testDataPath, 'raster.tif') + + with tempfile.TemporaryDirectory() as outdir: + outsource = outdir + '/check.tif' + alg = sieve() + alg.initAlgorithm() + + # 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 ' + + source + ' ' + + outsource]) + + # Eight connectedness and custom threshold + self.assertEqual( + 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 ' + + source + ' ' + + outsource]) + + # 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 ' + + source + ' ' + + outsource]) + + # 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', + '-st 10 -4 -mask ' + + mask + + ' -of GTiff ' + + source + ' ' + + outsource]) + class TestGdalOgrToPostGis(unittest.TestCase): diff --git a/python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml index 3f72bd203dbc..13af06d4ad5f 100644 --- a/python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml @@ -419,24 +419,6 @@ tests: hash: ee2b317e022da1001378fac60c9b613a74d3566b9870f9d121e6e322 type: rasterhash -# Disabled as gdal_sieve is not available on Travis -# - algorithm: gdal:sieve -# name: Sieve -# params: -# EIGHT_CONNECTEDNESS: false -# INPUT: -# name: dem.tif -# type: raster -# MASK_LAYER: -# name: dem.tif -# type: raster -# NO_MASK: false -# THRESHOLD: 10 -# results: -# OUTPUT: -# hash: 1ea6a8c838add299dc3f6f9f529eb5945664f68bae97be9ca80b1754 -# type: rasterhash - - algorithm: gdal:slope name: Slope params: