Skip to content
Permalink
Browse files

[processing] fix handling of the mask layer in the gdal_sieve algorithm

(fix #20713)

(cherry picked from commit a5365e2)
  • Loading branch information
alexbruy committed Jan 1, 2019
1 parent 5b66c05 commit c50a5cd8bb5226dfb5c6a518bf54a84fab57c992
@@ -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()))

@@ -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):

@@ -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:

0 comments on commit c50a5cd

Please sign in to comment.
You can’t perform that action at this time.