[GRASS-SVN] r61818 - in grass-addons/grass7/vector: . v.lfp

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Sep 6 09:50:49 PDT 2014


Author: hcho
Date: 2014-09-06 09:50:49 -0700 (Sat, 06 Sep 2014)
New Revision: 61818

Added:
   grass-addons/grass7/vector/v.lfp/
   grass-addons/grass7/vector/v.lfp/Makefile
   grass-addons/grass7/vector/v.lfp/v.lfp.html
   grass-addons/grass7/vector/v.lfp/v.lfp.py
Log:
v.lfp: Add a new module that converts an r.lfp output map to a vector map

Added: grass-addons/grass7/vector/v.lfp/Makefile
===================================================================
--- grass-addons/grass7/vector/v.lfp/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.lfp/Makefile	2014-09-06 16:50:49 UTC (rev 61818)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.lfp
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.lfp/v.lfp.html
===================================================================
--- grass-addons/grass7/vector/v.lfp/v.lfp.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.lfp/v.lfp.html	2014-09-06 16:50:49 UTC (rev 61818)
@@ -0,0 +1,43 @@
+<h2>DESCRIPTION</h2>
+<em>v.lfp</em> converts a longest flow path raster map created by
+<em>r.lfp</em> to a vector map.
+
+<h2>NOTES</h2>
+
+<em>v.lfp</em> creates a longest flow path vector map by processing an output
+raster map from <em>r.lfp</em>. Since a longest flow path raster map can have
+cell clusters, simply converting the raster map to a vector map using
+<em>r.to.vect</em> can result in closed rectangular shapes in the output vector
+map, which represent multiple but equivalent longest flow paths. This module
+first converts the input raster map to a vector map, removes dangles, and merge
+the remaining lines. The module repeats this process until there remains only
+one line feature, which is the final longest flow path.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+# Creates a longest flow path raster map.
+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
+
+# The end point of lfp can be the headwater of the longest flow path.
+v.lfp input=lfp output=lfp
+
+# Make sure the end point of lfp2 is the outlet point.
+v.lfp input=lfp output=lfp2 coordinates=-888857.11,1117788.38
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.lfp.html">r.lfp</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/vector/v.lfp/v.lfp.py
===================================================================
--- grass-addons/grass7/vector/v.lfp/v.lfp.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.lfp/v.lfp.py	2014-09-06 16:50:49 UTC (rev 61818)
@@ -0,0 +1,166 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       v.lfp
+# AUTHOR(S):    Huidae Cho
+# PURPOSE:      Converts a longest flow path raster map created by r.lfp to
+#               a vector map.
+#
+# 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: Converts a longest flow path raster map created by r.lfp to a vector map.
+#% keywords: hydrology
+#% keywords: watershed
+#%end
+#%option G_OPT_R_INPUT
+#% description: Name of input longest flow path raster map
+#%end
+#%option G_OPT_V_OUTPUT
+#% description: Name for output longest flow path vector map
+#%end
+#%option G_OPT_M_COORDS
+#% label: Coordinates of outlet point
+#% description: Optionally required to ensure the downstream direction of the output line
+#%end
+
+import sys
+import os
+import math
+import grass.script as grass
+
+def main():
+    input = options["input"]
+    output = options["output"]
+    coords = options["coordinates"]
+
+    convert_lfp(input, output, coords)
+
+def convert_lfp(input, output, coords):
+    p = grass.pipe_command("r.info", flags="g", map=input)
+    res = ""
+    for line in p.stdout:
+        line = line.rstrip("\n")
+        if line.startswith("nsres="):
+            res = line.split("=")[1]
+            break
+    p.wait()
+    if p.returncode != 0 or res == "":
+        grass.fatal(_("Cannot read the resolution of int input raster map"))
+    res = float(res)
+    danglelen = math.ceil(math.sqrt(2)*res)
+
+    if grass.run_command("r.to.vect",
+            input=input, output=output, type="line") != 0:
+        grass.fatal(_("Cannot convert the input raster map to a vector map"))
+
+    if grass.run_command("v.edit", map=output, tool="delete",
+            query="dangle", threshold="0,0,-%f" % danglelen) != 0:
+        grass.fatal(_("Cannot delete dangles from the output vector map"))
+
+    success = False
+    for i in range(0, 100):
+        if grass.run_command("v.edit", map=output, tool="merge", where="") != 0:
+            grass.fatal(_("Cannot merge features in the output vector map"))
+
+        p = grass.pipe_command("v.info", flags="t", map=output)
+        lines = ""
+        for line in p.stdout:
+            line = line.rstrip("\n")
+            if line.startswith("lines="):
+                lines = line.split("=")[1]
+                break
+        p.wait()
+        if p.returncode != 0 or lines == "":
+            grass.fatal(_("Cannot read lines from the output vector map info"))
+
+        lines = int(lines)
+        if lines == 0:
+            grass.fatal(_("Cannot process the output vector map"))
+        elif lines == 1:
+            success = True
+            break
+
+        p = grass.pipe_command("v.report", map=output, option="length",
+                sort="asc")
+        firstcat = ""
+        for line in p.stdout:
+            line = line.rstrip("\n")
+            if line == "cat|value|length":
+                continue
+            cols = line.split("|")
+            firstcat = cols[0]
+            break
+        p.wait()
+        if p.returncode != 0:
+            grass.fatal(_("Cannot read the output vector map report"))
+
+        if firstcat == "":
+            grass.fatal(_("Cannot further simplify the longest flow path"))
+
+        if grass.run_command("v.edit", map=output, tool="delete",
+                cats=firstcat) != 0:
+            grass.fatal(_("Cannot delete short segments from the output vector map"))
+
+    if success == False:
+        grass.fatal(_("Cannot simplify the longest flow path"))
+
+    p = grass.pipe_command("v.report", map=output, option="length")
+    mincat = ""
+    maxcat = ""
+    for line in p.stdout:
+        line = line.rstrip("\n")
+        if line.startswith("cat|value|"):
+            continue
+        cat = line.split("|")[0]
+        if mincat == "":
+            mincat = cat
+        maxcat = cat
+    p.wait()
+    if p.returncode != 0 or mincat == "" or maxcat == "":
+        grass.fatal(_("Cannot read min/max categories from the output vector map"))
+
+    mincat = int(mincat)
+    maxcat = int(maxcat)
+
+    if grass.run_command("v.edit", map=output, tool="catdel",
+            cats="%d-%d" % (mincat+1, maxcat), where="") != 0:
+        grass.fatal(_("Cannot delete categories from the output vector map"))
+
+    if coords != "":
+        p = grass.pipe_command("v.to.db", flags="p", map=output, option="start")
+        startx = ""
+        starty = ""
+        for line in p.stdout:
+            line = line.rstrip("\n")
+            if line == "cat|x|y|z":
+                continue
+            cols = line.split("|")
+            startx = cols[1]
+            starty = cols[2]
+        p.wait()
+        if p.returncode != 0 or startx == "" or starty == "":
+            grass.fatal(_("Cannot read the start point of the longest flow path"))
+
+        startx = float(startx)
+        starty = float(starty)
+
+        outxy = coords.split(",")
+        outx = float(outxy[0])
+        outy = float(outxy[1])
+
+        if startx >= outx - res * 0.5 and startx <= outx + res * 0.5 and \
+           starty >= outy - res * 0.5 and starty <= outy + res * 0.5:
+            if grass.run_command("v.edit", map=output, tool="flip",
+                    where="") != 0:
+                grass.fatal(_("Cannot flip the longest flow path"))
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())


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



More information about the grass-commit mailing list