Skip to content

Commit

Permalink
Merge pull request #8775 from alexbruy/backport-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 authored Jan 1, 2019
2 parents 5b66c05 + c50a5cd commit e39fdd5
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
66 changes: 66 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):

Expand Down
18 changes: 0 additions & 18 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e39fdd5

Please sign in to comment.