Pārlūkot izejas kodu

module to import vector lines from a stream of x,y data (move from addons)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@40349 15284696-431f-4ddb-bdfa-cd5b030d7da7
Hamish Bowman 15 gadi atpakaļ
vecāks
revīzija
36d336c7cc

+ 1 - 0
scripts/Makefile

@@ -54,6 +54,7 @@ SUBDIRS = \
 	v.in.e00 \
 	v.in.geonames \
 	v.in.gns \
+	v.in.lines \
 	v.in.mapgen \
 	v.in.sites.all \
 	v.in.wfs \

+ 7 - 0
scripts/v.in.lines/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.in.lines
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 56 - 0
scripts/v.in.lines/description.html

@@ -0,0 +1,56 @@
+<H2>DESCRIPTION</H2>
+
+Imports a stream of ASCII x,y[,z] coordinates as a line or series of lines.
+
+
+<H2>NOTES</H2>
+
+Input ASCII coordinates are simply a series of "x&nbsp;y" data points.
+Lines are separated by a row containing "<tt>NaN&nbsp;NaN</tt>".
+<!-- TODO:? Line categories start at 1 and increase sequentially. -->
+<P>
+You can import 3D lines by providing 3 columns of data in the input
+stream and using the <B>-z</B> flag.
+<P>
+This script is a simple wrapper around the <em>v.in.mapgen</em> module.
+
+
+<H2>EXAMPLE</H2>
+
+<div class="code"><pre>
+v.in.lines in=- out=two_lines fs=, &lt;&lt;EOF
+167.846717,-46.516653
+167.846663,-46.516645
+167.846656,-46.516644
+167.846649,-46.516644
+167.846642,-46.516643
+NaN,NaN
+167.846520,-46.516457
+167.846528,-46.516461
+167.846537,-46.516464
+167.846535,-46.516486
+167.846544,-46.516489
+167.846552,-46.516493
+EOF
+
+v.category in=two_lines out=lines_with_cats option=add
+</pre></div>
+
+
+<H2>SEE ALSO</H2>
+<em>
+<A HREF="v.in.ascii.html">v.in.ascii</A><br>
+<A HREF="v.in.mapgen.html">v.in.mapgen</A><br>
+<A HREF="v.out.ascii.html">v.out.ascii</A><br>
+<A HREF="d.graph.html">d.graph</a><br>
+<A HREF="r.in.poly.html">r.in.poly</A>
+</em>
+
+
+<H2>AUTHOR</H2>
+
+Hamish Bowman<BR>
+Dunedin, New Zealand
+
+<p>
+<i>Last changed: $Date$</i>

+ 150 - 0
scripts/v.in.lines/v.in.lines.py

@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:       v.in.lines
+#
+# AUTHOR(S):    Hamish Bowman
+#
+# PURPOSE:      Import point data as lines ('v.in.mapgen -f' wrapper script)
+#
+# COPYRIGHT:    (c) 2009-2010 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: Import ASCII x,y[,z] coordinates as a series of lines.
+#% keywords: vector, import
+#%End
+#%flag
+#% key: z
+#% description: Create a 3D line from 3 column data 
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old_file,file,input
+#% description: Name of input file (or "-" to read from stdin)
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,vector,vector
+#% description: Name for output vector map
+#% required : yes
+#%end
+#%option
+#% key: fs
+#% type: string
+#% key_desc: character
+#% description: Field separator
+#% answer: |
+#% required: no
+#%end
+
+import sys
+import os
+import atexit
+import string
+from grass.script import core as grass
+
+def cleanup():
+    grass.try_remove(tmp)
+
+def main():
+    global tmp
+
+    infile_opt = options['input']
+    output = options['output']
+    fs = options['fs']
+    threeD = flags['z']
+
+    prog = 'v.in.lines'
+
+    opts = ""
+
+    if threeD:
+        do3D = 'z'
+    else:
+        do3D = ''
+
+
+    tmp = grass.tempfile()
+
+
+    #### parse field separator
+    if fs in ('space', 'tab'):
+        fs = ' '
+    elif fs == 'comma':
+        fs = ','
+    else:
+        if len(fs) > 1:
+            grass.warning(_("Invalid field separator, using '%s'") % fs[0])
+        try:
+            fs = fs[0]
+        except IndexError:
+            grass.fatal(_("Invalid field separator '%s'") % fs)
+
+    #### set up input file
+    if infile_opt == '-':
+        infile = None
+        inf = sys.stdin
+    else:
+        infile = infile_opt
+        if not os.path.exists(infile):
+            grass.fatal(_("Unable to read input file <%s>") % infile)
+        grass.debug("input file=[%s]" % infile)
+
+
+    if not infile:
+        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
+        outf = file(tmp, 'w')
+        for line in inf:
+            if len(line.lstrip()) == 0 or line[0] == '#':
+                continue
+            outf.write(line.replace(fs, ' '))
+
+        outf.close()
+        runfile = tmp
+    else:
+        # read from a real file
+        if fs == ' ':
+            runfile = infile
+        else:
+            inf = file(infile)
+            outf = file(tmp, 'w')
+
+            for line in inf:
+                if len(line.lstrip()) == 0 or line[0] == '#':
+                    continue
+                outf.write(line.replace(fs, ' '))
+
+            inf.close()
+            outf.close()
+            runfile = tmp
+
+
+    ##### check that there are at least two columns (three if -z is given)
+    inf = file(runfile)
+    for line in inf:
+        if len(line.lstrip()) == 0 or line[0] == '#':
+            continue
+        numcols = len(line.split())
+        break
+    if (do3D and numcols < 3) or (not do3D and numcols < 2):
+        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))
+    inf.close()
+
+
+    grass.run_command('v.in.mapgen', flags = 'f' + do3D,
+                      input = runfile, output = output)
+
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()