Bläddra i källkod

r.grow: +shrink

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@69110 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 8 år sedan
förälder
incheckning
d7fe2313fb
2 ändrade filer med 40 tillägg och 12 borttagningar
  1. 12 1
      scripts/r.grow/r.grow.html
  2. 28 11
      scripts/r.grow/r.grow.py

+ 12 - 1
scripts/r.grow/r.grow.html

@@ -8,6 +8,10 @@ parameter), or like <em>r.buffer</em>, but with the
 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 @@ North Carolina sample dataset location is buffered:
 
 <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>

+ 28 - 11
scripts/r.grow/r.grow.py

@@ -90,8 +90,13 @@ def main():
     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,16 +118,28 @@ def main():
     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."))
-
-    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)
+    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)
+    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)