[GRASS-SVN] r34402 - in grass/trunk: raster scripts scripts/r.grow

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 20 11:29:55 EST 2008


Author: glynn
Date: 2008-11-20 11:29:54 -0500 (Thu, 20 Nov 2008)
New Revision: 34402

Added:
   grass/trunk/scripts/r.grow/
   grass/trunk/scripts/r.grow/Makefile
   grass/trunk/scripts/r.grow/r.grow.html
   grass/trunk/scripts/r.grow/r.grow.py
Modified:
   grass/trunk/raster/Makefile
Log:
Replace r.grow with script


Modified: grass/trunk/raster/Makefile
===================================================================
--- grass/trunk/raster/Makefile	2008-11-20 16:29:23 UTC (rev 34401)
+++ grass/trunk/raster/Makefile	2008-11-20 16:29:54 UTC (rev 34402)
@@ -25,7 +25,6 @@
 	r.fill.dir \
 	r.flow \
 	r.grow.distance \
-	r.grow \
 	r.gwflow \
 	r.his \
 	r.in.arc \

Added: grass/trunk/scripts/r.grow/Makefile
===================================================================
--- grass/trunk/scripts/r.grow/Makefile	                        (rev 0)
+++ grass/trunk/scripts/r.grow/Makefile	2008-11-20 16:29:54 UTC (rev 34402)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.grow
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass/trunk/scripts/r.grow/r.grow.html
===================================================================
--- grass/trunk/scripts/r.grow/r.grow.html	                        (rev 0)
+++ grass/trunk/scripts/r.grow/r.grow.html	2008-11-20 16:29:54 UTC (rev 34402)
@@ -0,0 +1,77 @@
+<h2>DESCRIPTION</h2>
+
+
+<em>r.grow</em> adds cells around the perimeters of all areas
+in a user-specified raster map layer and stores the output in
+a new raster map layer. The user can use it to grow by one or
+more than one cell (by varying the size of the <b>radius</b>
+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>).
+
+<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
+the <b>metric</b> parameter): <i>Euclidean</i>, <i>Manhattan</i>, and 
+<i>Maximum</i>. 
+
+<p>
+
+The <i>Euclidean distance</i> or <i>Euclidean metric</i> is the "ordinary" distance 
+between two points that one would measure with a ruler, which can be 
+proven by repeated application of the Pythagorean theorem. 
+The formula is given by: 
+
+<div class="code"><pre>d(dx,dy) = sqrt(dx^2 + dy^2)</pre></div>
+
+Cells grown using this metric would form isolines of distance that are
+circular from a given point, with the distance given by the <b>radius</b>.
+
+<p>
+
+The <i>Manhattan metric</i>, or <i>Taxicab geometry</i>, is a form of geometry in 
+which the usual metric of Euclidean geometry is replaced by a new 
+metric in which the distance between two points is the sum of the (absolute) 
+differences of their coordinates. The name alludes to the grid layout of 
+most streets on the island of Manhattan, which causes the shortest path a 
+car could take between two points in the city to have length equal to the
+points' distance in taxicab geometry.
+The formula is given by:
+
+<div class="code"><pre>d(dx,dy) = abs(dx) + abs(dy)</pre></div>
+
+where cells grown using this metric would form isolines of distance that are
+rhombus-shaped from a given point. 
+
+<p>
+
+The <i>Maximum metric</i> is given by the formula
+
+<div class="code"><pre>d(dx,dy) = max(abs(dx),abs(dy))</pre></div>
+
+where the isolines of distance from a point are squares.
+
+<p>
+
+If there are two cells which are equal candidates to grow into an empty space, 
+<em>r.grow</em> will choose the northernmost candidate; if there are multiple 
+candidates with the same northing, the westernmost is chosen. 
+
+<h2>SEE ALSO</h2>
+
+<em><a href="r.buffer.html">r.buffer</a></em><br>
+<em><a href="r.patch.html">r.patch</a></em>
+
+<p>
+
+<em><a href="http://en.wikipedia.org/wiki/Euclidean_metric">Wikipedia Entry: Euclidean Metric</a></em><br>
+<em><a href="http://en.wikipedia.org/wiki/Manhattan_metric">Wikipedia Entry: Manhattan Metric</a></em>
+
+<h2>AUTHORS</h2>
+
+Marjorie Larson, 
+U.S. Army Construction Engineering Research Laboratory
+<p>
+Glynn Clements
+
+<p><i>Last changed: $Date: 2008-08-17 19:42:20 +0100 (Sun, 17 Aug 2008) $</i>

Added: grass/trunk/scripts/r.grow/r.grow.py
===================================================================
--- grass/trunk/scripts/r.grow/r.grow.py	                        (rev 0)
+++ grass/trunk/scripts/r.grow/r.grow.py	2008-11-20 16:29:54 UTC (rev 34402)
@@ -0,0 +1,141 @@
+#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:	r.grow
+# AUTHOR(S):	Glynn Clements
+# PURPOSE:	Fast replacement for r.grow using r.grow.distance
+#
+# COPYRIGHT:	(C) 2008 by Glynn Clements
+#
+#		This program is free software under the GNU General Public
+#		License (>=v2). Read the file COPYING that comes with GRASS
+#		for details.
+#
+#############################################################################
+
+#%Module
+#% description: Generates a raster map layer with contiguous areas grown by one cell.
+#% keywords: raster
+#%End
+#%Flag
+#% key: m
+#% description: radius is in map units rather than cells
+#%End
+#%Option
+#% key: input
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% description: Name of input raster map
+#% gisprompt: old,cell,raster
+#%End
+#%Option
+#% key: output
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% description: Name for output raster map
+#% gisprompt: new,cell,raster
+#%End
+#%Option
+#% key: radius
+#% type: double
+#% required: no
+#% multiple: no
+#% description: Radius of buffer in raster cells
+#% answer: 1.01
+#%End
+#%Option
+#% key: metric
+#% type: string
+#% required: no
+#% multiple: no
+#% options: euclidean,maximum,manhattan
+#% description: Metric
+#% answer: euclidean
+#%End
+#%Option
+#% key: old
+#% type: integer
+#% required: no
+#% multiple: no
+#% description: Value to write for input cells which are non-NULL (-1 => NULL)
+#%End
+#%Option
+#% key: new
+#% type: integer
+#% required: no
+#% multiple: no
+#% description: Value to write for "grown" cells
+#%End
+
+import sys
+import os
+import atexit
+import string
+import math
+import grass
+
+# what to do in case of user break:
+def cleanup():
+    for map in [temp_dist, temp_val]:
+	if map:
+	    grass.run_command('g.remove', quiet = True, flags = 'f', rast = map)
+
+def main():
+    global temp_dist, temp_val
+
+    input = options['input']
+    output = options['output']
+    radius = float(options['radius'])
+    metric = options['metric']
+    old = options['old']
+    new = options['new']
+    mapunits = flags['m']
+
+    tmp = str(os.getpid())
+
+    temp_dist = "r.grow.tmp.%s.dist" % tmp
+
+    if new == '':
+	temp_val = "r.grow.tmp.%s.val" % tmp
+	new = temp_val
+    else:
+	temp_val = None
+
+    if old == '':
+	old = input
+
+    if not mapunits:
+	kv = grass.region()
+	scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
+	radius *= scale
+
+    if metric == 'euclidean':
+	metric = 'squared'
+	radius = radius * radius
+
+    #check if input file exists
+    if not grass.find_file(input)['file']:
+	grass.fatal("<%s> does not exist." % input)
+
+    grass.run_command('r.grow.distance',  input = input, metric = metric,
+		      distance = temp_dist, value = temp_val)
+
+    t = string.Template("$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))")
+    e = t.substitute(output = output, input = input, radius = radius,
+		     old = old, new = new, dist = temp_dist)
+    grass.run_command('r.mapcalc', expression = e)
+
+    grass.run_command('r.colors', map = output, raster = input)
+
+    # write cmd history:
+    grass.raster_history(output)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()


Property changes on: grass/trunk/scripts/r.grow/r.grow.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list