[GRASS-SVN] r61800 - in grass-addons/grass7/raster: . r.lfp

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 5 00:25:08 PDT 2014


Author: hcho
Date: 2014-09-05 00:25:08 -0700 (Fri, 05 Sep 2014)
New Revision: 61800

Added:
   grass-addons/grass7/raster/r.lfp/
   grass-addons/grass7/raster/r.lfp/Makefile
   grass-addons/grass7/raster/r.lfp/r.lfp.html
   grass-addons/grass7/raster/r.lfp/r.lfp.py
Log:
r.lfp: Added a new addon for calculating the longest flow path

Added: grass-addons/grass7/raster/r.lfp/Makefile
===================================================================
--- grass-addons/grass7/raster/r.lfp/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.lfp/Makefile	2014-09-05 07:25:08 UTC (rev 61800)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.lfp
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/raster/r.lfp/r.lfp.html
===================================================================
--- grass-addons/grass7/raster/r.lfp/r.lfp.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.lfp/r.lfp.html	2014-09-05 07:25:08 UTC (rev 61800)
@@ -0,0 +1,36 @@
+<h2>DESCRIPTION</h2>
+<em>r.lfp</em> calculates the longest flow path for a given outlet point and
+creates a new raster map with a thin line representing the longest flow path.
+
+<h2>NOTES</h2>
+
+<em>r.lfp</em> creates a longest flow path raster map using a drainage
+direction raster map and the coordinates of an outlet point. The module
+internally runs <em>r.stream.distance</em> twice to calculate flow length
+downstream and upstream raster maps, and combines them to get the longest flow
+path. An input drainage map can be created using <em>r.watershed</em> or
+<em>r.stream.extract</em>.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+r.watershed elevation=elev drainage=drain
+r.water.outlet input=drain output=basin coordinates=-888857.11,1117788.38
+r.lfp input=drain output=lfp coordinates=-888857.11,1117788.38
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.watershed.html">r.watershed</a>,
+<a href="r.stream.extract.html">r.stream.extract</a>,
+<a href="r.stream.distance.html">r.stream.distance</a>
+</em>
+<br>
+<a href="http://idea.isnew.info/grass_gis/how_to_calculate_the_longest_flow_path_in_grass_gis">How to calculate the longest flow path in GRASS GIS</a>
+
+<h2>AUTHOR</h2>
+
+<a href="mailto:grass4u at gmail com">Huidae Cho</a>
+
+<p><i>Last changed: $Date$</i>

Added: grass-addons/grass7/raster/r.lfp/r.lfp.py
===================================================================
--- grass-addons/grass7/raster/r.lfp/r.lfp.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.lfp/r.lfp.py	2014-09-05 07:25:08 UTC (rev 61800)
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       r.lfp
+# AUTHOR(S):    Huidae Cho
+# PURPOSE:      Calculates the longest flow path for a given outlet point.
+#
+# COPYRIGHT:    (C) 2014 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Calculates the longest flow path for a given outlet point.
+#% keywords: hydrology
+#% keywords: watershed
+#%end
+#%option G_OPT_R_INPUT
+#% description: Name of input drainage direction map
+#%end
+#%option G_OPT_R_OUTPUT
+#% description: Name for output longest flow path map
+#%end
+#%option G_OPT_M_COORDS
+#% description: Coordinates of outlet point
+#%end
+
+import sys
+import os
+import grass.script as grass
+
+def main():
+    input = options["input"]
+    output = options["output"]
+    coords = options["coordinates"]
+
+    calculate_lfp(input, output, coords)
+
+def calculate_lfp(input, output, coords):
+    prefix = "r_lfp_%d_" % os.getpid()
+
+    outlet = prefix + "outlet"
+    p = grass.feed_command("v.in.ascii", overwrite=True,
+            input="-", output=outlet, separator=",")
+    p.stdin.write(coords)
+    p.stdin.close()
+    p.wait()
+    if p.returncode != 0:
+        grass.fatal(_("Cannot create outlet vector map"))
+
+    if grass.run_command("v.to.rast", overwrite=True,
+            input=outlet, output=outlet, use="cat") != 0:
+        grass.fatal(_("Cannot convert outlet vector to raster"))
+
+    flds = prefix + "flds"
+    if grass.run_command("r.stream.distance", overwrite=True, flags="om",
+            stream_rast=outlet, direction=input, method="downstream",
+            distance=flds) != 0:
+        grass.fatal(_("Cannot create flow length downstream raster map"))
+
+    flus = prefix + "flus"
+    if grass.run_command("r.stream.distance", overwrite=True, flags="o",
+            stream_rast=outlet, direction=input, method="upstream",
+            distance=flus) != 0:
+        grass.fatal(_("Cannot create flow length upstream raster map"))
+
+    fldsus = prefix + "fldsus"
+    if grass.run_command("r.mapcalc", overwrite=True,
+            expression="%s=%s+%s" % (fldsus, flds, flus)) != 0:
+        grass.fatal(_("Cannot create flds+flus raster map"))
+
+    p = grass.pipe_command("r.info", flags="r", map=fldsus)
+    max = ""
+    for line in p.stdout:
+        line = line.rstrip("\n")
+        if line.startswith("max="):
+            max = line.split("=")[1]
+            break
+    p.wait()
+    if p.returncode != 0 or max == "":
+        grass.fatal(_("Cannot find max flds+flus cell value"))
+
+    max = float(max) + 1
+    min = max - 2
+
+    if grass.run_command("r.mapcalc",
+            expression="%s=if(%s>=%f, 1, null())" % (output, fldsus, min)) != 0:
+        grass.fatal(_("Cannot create longest flow path raster map"))
+
+    grass.run_command("g.mremove", flags="f",
+            type="rast,vect", pattern="%s*" % prefix)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())


Property changes on: grass-addons/grass7/raster/r.lfp/r.lfp.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list