[GRASS-SVN] r40344 - grass-addons/vector/v.in.lines
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jan 9 07:30:49 EST 2010
Author: hamish
Date: 2010-01-09 07:30:44 -0500 (Sat, 09 Jan 2010)
New Revision: 40344
Added:
grass-addons/vector/v.in.lines/v.in.lines.py
Log:
port shell script to python (work in progress)
Copied: grass-addons/vector/v.in.lines/v.in.lines.py (from rev 40343, grass-addons/vector/v.in.lines/v.in.lines)
===================================================================
--- grass-addons/vector/v.in.lines/v.in.lines.py (rev 0)
+++ grass-addons/vector/v.in.lines/v.in.lines.py 2010-01-09 12:30:44 UTC (rev 40344)
@@ -0,0 +1,149 @@
+#!/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']
+ fsep = options['fs']
+ threeD = flags['z']
+
+ prog = 'v.in.lines'
+
+ opts = ""
+
+ if threeD:
+ do3D = 'z'
+ else:
+ do3D = ''
+
+ #### 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 data"))
+ grass.debug("input file=[%s]" % infile)
+
+
+ tmp = grass.tempfile()
+
+ 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) == 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) == 0 or line[0] == '#':
+ continue
+ outf.write(line.replace(fs, ' '))
+
+ outf.close()
+ runfile = tmp
+
+ if infile:
+ inf.close()
+
+
+ ##### check that there are at least two columns (three if -z is given)
+ inf = file(infile)
+ line = inf.readline()
+ numcols = len(line.split(' '))
+ if (do3D and numcols < 3) or (not do3D and numcols < 2):
+ g.message -e "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()
+
More information about the grass-commit
mailing list