[GRASS-SVN] r70187 - grass/branches/releasebranch_7_2/scripts/r.grow

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jan 1 12:37:35 PST 2017


Author: neteler
Date: 2017-01-01 12:37:35 -0800 (Sun, 01 Jan 2017)
New Revision: 70187

Modified:
   grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.html
   grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.py
Log:
r.grow: support to shrink added (trunk, r69110)

Modified: grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.html
===================================================================
--- grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.html	2017-01-01 20:27:44 UTC (rev 70186)
+++ grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.html	2017-01-01 20:37:35 UTC (rev 70187)
@@ -8,6 +8,10 @@
 option of preserving the original cells (similar to combining
 <em>r.buffer</em> and <em>r.patch</em>).
 
+<p>
+If <b>radius</b> is negative,<em>r.grow</em> shrinks areas by removing 
+cells around the perimeters of all areas.
+
 <h2>NOTES</h2>
 The user has the option of specifying three different metrics which
 control the geometry in which grown cells are created, (controlled by
@@ -55,9 +59,16 @@
 
 <div class="code"><pre>
 g.region raster=lakes -p
-r.grow input=lakes output=lakes_grown_50m radius=10
+r.grow input=lakes output=lakes_grown_100m radius=10
 </pre></div>
 
+Shrinking instead of growing:
+
+<div class="code"><pre>
+r.grow input=lakes output=lakes_shrunk_100m radius=-10
+</pre></div>
+
+
 <h2>SEE ALSO</h2>
 
 <em>

Modified: grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.py
===================================================================
--- grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.py	2017-01-01 20:27:44 UTC (rev 70186)
+++ grass/branches/releasebranch_7_2/scripts/r.grow/r.grow.py	2017-01-01 20:37:35 UTC (rev 70187)
@@ -90,8 +90,13 @@
     tmp = str(os.getpid())
 
     temp_dist = "r.grow.tmp.%s.dist" % tmp
+    
+    shrink = False
+    if radius < 0.0:
+        shrink = True
+        radius = -radius
 
-    if new == '':
+    if new == '' and shrink == False:
         temp_val = "r.grow.tmp.%s.val" % tmp
         new = temp_val
     else:
@@ -113,17 +118,29 @@
     if not grass.find_file(input)['file']:
         grass.fatal(_("Raster map <%s> not found") % input)
 
-    try:
-        grass.run_command('r.grow.distance', input=input, metric=metric,
-                          distance=temp_dist, value=temp_val)
-    except CalledModuleError:
-        grass.fatal(_("Growing failed. Removing temporary maps."))
+    if shrink == False:
+        try:
+            grass.run_command('r.grow.distance', input=input, metric=metric,
+                              distance=temp_dist, value=temp_val)
+        except CalledModuleError:
+            grass.fatal(_("Growing failed. Removing temporary maps."))
 
-    grass.mapcalc(
-        "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
-        output=output, input=input, radius=radius,
-        old=old, new=new, dist=temp_dist)
+        grass.mapcalc(
+            "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
+            output=output, input=input, radius=radius,
+            old=old, new=new, dist=temp_dist)
+    else:
+        # shrink
+        try:
+            grass.run_command('r.grow.distance', input=input, metric=metric,
+                              distance=temp_dist, value=temp_val, flags='n')
+        except CalledModuleError:
+            grass.fatal(_("Shrinking failed. Removing temporary maps."))
 
+        grass.mapcalc(
+            "$output = if($dist < $radius,null(),$old)",
+            output=output, radius=radius, old=old, dist=temp_dist)
+
     grass.run_command('r.colors', map=output, raster=input)
 
     # write cmd history:



More information about the grass-commit mailing list