Skip to content

Commit

Permalink
Merge pull request #43720 from nicogodet/patch-1
Browse files Browse the repository at this point in the history
Add odd integer constraint on neighborhood size in r.neighbors
  • Loading branch information
alexbruy committed Jun 17, 2021
2 parents fad1800 + b265e20 commit cc41628
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
Expand Up @@ -4,7 +4,7 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Input raster layer|None|False
QgsProcessingParameterRasterLayer|selection|Raster layer to select the cells which should be processed|None|True
QgsProcessingParameterEnum|method|Neighborhood operation|average;median;mode;minimum;maximum;range;stddev;sum;count;variance;diversity;interspersion;quart1;quart3;perc90;quantile|False|0|True
QgsProcessingParameterNumber|size|Neighborhood size|QgsProcessingParameterNumber.Integer|3|True|1|None
QgsProcessingParameterNumber|size|Neighborhood size (must be odd)|QgsProcessingParameterNumber.Integer|3|True|1|None
QgsProcessingParameterNumber|gauss|Sigma (in cells) for Gaussian filter|QgsProcessingParameterNumber.Integer|None|True|0|None
QgsProcessingParameterString|quantile|Quantile to calculate for method=quantile|None|False|True
QgsProcessingParameterBoolean|-c|Use circular neighborhood|False
Expand Down
31 changes: 31 additions & 0 deletions python/plugins/processing/algs/grass7/ext/r_neighbors.py
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
r_neighbors.py
--------
Date : June 2021
Copyright : (C) 2021 by Nicolas Godet
Email : nicolas dot godet at outlook dot fr
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Nicolas Godet'
__date__ = 'June 2021'
__copyright__ = '(C) 2021, Nicolas Godet'


def checkParameterValuesBeforeExecuting(alg, parameters, context):
""" Verify if we have the right parameters """
# Verifiy that neighborhood size is odd
if (alg.parameterAsInt(parameters, 'size', context) % 2) == 0:
return False, alg.tr("The neighborhood size must be odd!")

return True, None
41 changes: 41 additions & 0 deletions python/plugins/processing/tests/Grass7AlgorithmsRasterTestPt2.py
Expand Up @@ -25,14 +25,24 @@

import nose2
import shutil
import os
import tempfile

from qgis.core import (
QgsApplication,
QgsProcessingContext,
QgsProcessingFeedback
)
from qgis.testing import (
start_app,
unittest
)
from processing.algs.grass7.Grass7Utils import Grass7Utils


testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')


class TestGrass7AlgorithmsRasterTest(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):

@classmethod
Expand All @@ -42,6 +52,9 @@ def setUpClass(cls):
Processing.initialize()
cls.cleanup_paths = []

cls.temp_dir = tempfile.mkdtemp()
cls.cleanup_paths.append(cls.temp_dir)

assert Grass7Utils.installedVersion()

@classmethod
Expand All @@ -54,6 +67,34 @@ def tearDownClass(cls):
def test_definition_file(self):
return 'grass7_algorithms_raster_tests2.yaml'

def testNeighbors(self):
context = QgsProcessingContext()
input_raster = os.path.join(testDataPath, 'custom', 'grass7', 'float_raster.tif')

alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:r.neighbors')
self.assertIsNotNone(alg)

temp_file = os.path.join(self.temp_dir, 'grass_output.tif')

# Test an even integer for neighborhood size
parameters = {'input': input_raster,
'selection': None,
'method': 0,
'size': 4,
'gauss': None,
'quantile': '',
'-c': False,
'-a': False,
'weight': '',
'output': temp_file,
'GRASS_REGION_PARAMETER': None,
'GRASS_REGION_CELLSIZE_PARAMETER': 0,
'GRASS_RASTER_FORMAT_OPT': '',
'GRASS_RASTER_FORMAT_META': ''}

ok, msg = alg.checkParameterValues(parameters, context)
self.assertFalse(ok)


if __name__ == '__main__':
nose2.main()
1 change: 0 additions & 1 deletion python/plugins/processing/tests/grass7_todo.md
Expand Up @@ -25,7 +25,6 @@
* r.mfilter
* r.mfilter.fp
* r.mode
* r.neighbors
* r.null
* r.out.gridatb
* r.out.ppm
Expand Down
Expand Up @@ -824,3 +824,22 @@ tests:
output:
hash: caa4870e0f0f1a42583cd562c26f0ad6b7795116961c7f6053f1ccde
type: rasterhash

- algorithm: grass7:r.neighbors
name: GRASS7 r.neighbors
params:
-a: false
-c: false
GRASS_RASTER_FORMAT_META: ''
GRASS_RASTER_FORMAT_OPT: ''
GRASS_REGION_CELLSIZE_PARAMETER: 0.0
input:
name: custom/grass7/float_raster.tif
type: raster
method: 0
quantile: ''
size: 3
results:
output:
hash: 7e78e8b556c9a39e07b2bf8edbd45c17fbd1897de974c52bc97009a8
type: rasterhash

0 comments on commit cc41628

Please sign in to comment.