Browse Source

r.grow: Correctly handle shrinking of maps without NULL values (#343)

* r.grow: Output unchanged map when shrinking if there are no null values in the input.
Fixes r.grow producing empty maps on shrink.

* testsuite: r.grow test shrinking map without NULL values
Based on PR #343

Co-authored-by: Māris Nartišs <maris.nartiss@lu.lv>
Māris Nartišs 5 years ago
parent
commit
dd5de23ca3
2 changed files with 19 additions and 3 deletions
  1. 1 1
      scripts/r.grow/r.grow.py
  2. 18 2
      scripts/r.grow/testsuite/test_r_grow.py

+ 1 - 1
scripts/r.grow/r.grow.py

@@ -147,7 +147,7 @@ def main():
             grass.fatal(_("Shrinking failed. Removing temporary maps."))
 
         grass.mapcalc(
-            "$output = if($dist < $radius,null(),$old)",
+            "$output = if(isnull($dist), $old, if($dist < $radius,null(),$old))",
             output=output, radius=radius, old=old, dist=temp_dist)
 
     grass.run_command('r.colors', map=output, raster=input)

+ 18 - 2
scripts/r.grow/testsuite/test_r_grow.py

@@ -1,7 +1,7 @@
 """
 Created on Sun Jun 07 22:09:41 2018
 
-@author: Sanjeet Bhatti
+@author: Sanjeet Bhatti, Maris Nartiss
 """
 
 from grass.gunittest.case import TestCase
@@ -16,6 +16,8 @@ class TestRGrow(TestCase):
     mapName = 'lakes'
     mapGrownOutput = 'lakes_grown_100m'
     mapShrunkOutput = 'lakes_shrunk_100m'
+    mapNoNULL = 'elevation'
+    mapShrunkNoNULL = 'elevation_shrunk'
 
     @classmethod
     def setUpClass(cls):
@@ -28,7 +30,8 @@ class TestRGrow(TestCase):
         """Remove temporary region"""
         cls.runModule('g.remove', flags='f', type='raster',
                       name=(cls.mapGrownOutput,
-                            cls.mapShrunkOutput))
+                            cls.mapShrunkOutput,
+                            cls.mapShrunkNoNULL))
         cls.del_temp_region()
 
     def test_grow(self):
@@ -45,5 +48,18 @@ class TestRGrow(TestCase):
                               radius=-10)
         self.assertModule(module)
 
+    def test_shrink_null(self):
+        """Shrinking of map without NULL values
+        Based on https://github.com/OSGeo/grass/pull/343"""
+        shrinked_string = '56-156'
+        shrinked = SimpleModule('r.grow', input=self.mapNoNULL,
+                              output=self.mapShrunkNoNULL,
+                              radius=-10)
+        self.assertModule(shrinked)
+
+        shrined_range = SimpleModule('r.describe', flags='i', _map=self.mapShrunkNoNULL)
+        self.runModule(shrined_range)
+        self.assertLooksLike(shrinked_string, str(shrined_range.outputs.stdout).strip())
+
 if __name__ == '__main__':
     test()