Skip to content

Commit

Permalink
Merge pull request #8773 from alexbruy/processing-gdal_sieve
Browse files Browse the repository at this point in the history
[processing] fix handling of the mask layer in the gdal_sieve algorithm
  • Loading branch information
alexbruy committed Jan 1, 2019
2 parents 1233a21 + a5365e2 commit 655dc1f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/sieve.py
Expand Up @@ -104,7 +104,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if self.parameterAsBool(parameters, self.NO_MASK, context): if self.parameterAsBool(parameters, self.NO_MASK, context):
arguments.append('-nomask') arguments.append('-nomask')


mask = self.parameterAsRasterLayer(parameters, self.INPUT, context) mask = self.parameterAsRasterLayer(parameters, self.MASK_LAYER, context)
if mask: if mask:
arguments.append('-mask {}'.format(mask.source())) arguments.append('-mask {}'.format(mask.source()))


Expand Down
66 changes: 66 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -60,6 +60,7 @@
from processing.algs.gdal.fillnodata import fillnodata from processing.algs.gdal.fillnodata import fillnodata
from processing.algs.gdal.rearrange_bands import rearrange_bands from processing.algs.gdal.rearrange_bands import rearrange_bands
from processing.algs.gdal.gdaladdo import gdaladdo from processing.algs.gdal.gdaladdo import gdaladdo
from processing.algs.gdal.sieve import sieve


from processing.tools.system import isWindows from processing.tools.system import isWindows


Expand Down Expand Up @@ -2448,6 +2449,71 @@ def testGdalAddo(self):
['gdaladdo', ['gdaladdo',
source + ' ' + '-r nearest 2 4 8 16']) 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): class TestGdalOgrToPostGis(unittest.TestCase):


Expand Down
18 changes: 0 additions & 18 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -420,24 +420,6 @@ tests:
hash: ee2b317e022da1001378fac60c9b613a74d3566b9870f9d121e6e322 hash: ee2b317e022da1001378fac60c9b613a74d3566b9870f9d121e6e322
type: rasterhash 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 - algorithm: gdal:slope
name: Slope name: Slope
params: params:
Expand Down

0 comments on commit 655dc1f

Please sign in to comment.