1#!/usr/local/bin/python3.8 2# 3############################################################################ 4# 5# MODULE: v.in.lines 6# 7# AUTHOR(S): Hamish Bowman 8# 9# PURPOSE: Import point data as lines ('v.in.mapgen -f' wrapper script) 10# 11# COPYRIGHT: (c) 2009-2010 The GRASS Development Team 12# 13# This program is free software under the GNU General Public 14# License (>=v2). Read the file COPYING that comes with GRASS 15# for details. 16# 17############################################################################# 18#%module 19#% description: Imports ASCII x,y[,z] coordinates as a series of lines. 20#% keyword: vector 21#% keyword: import 22#% keyword: line 23#% keyword: point 24#%end 25#%flag 26#% key: z 27#% description: Create a 3D line from 3 column data 28#%end 29#%option G_OPT_F_INPUT 30#% description: Name of input file (or "-" to read from stdin) 31#%end 32#%option G_OPT_V_OUTPUT 33#%end 34#%option G_OPT_F_SEP 35#%end 36 37import sys 38import os 39import atexit 40import string 41from grass.script.utils import separator, try_remove 42from grass.script import core as grass 43 44 45def cleanup(): 46 try_remove(tmp) 47 48 49def main(): 50 global tmp 51 52 fs = separator(options['separator']) 53 threeD = flags['z'] 54 55 prog = 'v.in.lines' 56 57 if threeD: 58 do3D = 'z' 59 else: 60 do3D = '' 61 62 tmp = grass.tempfile() 63 64 # set up input file 65 if options['input'] == '-': 66 infile = None 67 inf = sys.stdin 68 else: 69 infile = options['input'] 70 if not os.path.exists(infile): 71 grass.fatal(_("Unable to read input file <%s>") % infile) 72 grass.debug("input file=[%s]" % infile) 73 74 if not infile: 75 # read from stdin and write to tmpfile (v.in.mapgen wants a real file) 76 outf = open(tmp, 'w') 77 for line in inf: 78 if len(line.lstrip()) == 0 or line[0] == '#': 79 continue 80 outf.write(line.replace(fs, ' ')) 81 82 outf.close() 83 runfile = tmp 84 else: 85 # read from a real file 86 if fs == ' ': 87 runfile = infile 88 else: 89 inf = open(infile) 90 outf = open(tmp, 'w') 91 92 for line in inf: 93 if len(line.lstrip()) == 0 or line[0] == '#': 94 continue 95 outf.write(line.replace(fs, ' ')) 96 97 inf.close() 98 outf.close() 99 runfile = tmp 100 101 # check that there are at least two columns (three if -z is given) 102 inf = open(runfile) 103 for line in inf: 104 if len(line.lstrip()) == 0 or line[0] == '#': 105 continue 106 numcols = len(line.split()) 107 break 108 inf.close() 109 if (do3D and numcols < 3) or (not do3D and numcols < 2): 110 grass.fatal(_("Not enough data columns. (incorrect fs setting?)")) 111 112 grass.run_command('v.in.mapgen', flags='f' + do3D, 113 input=runfile, output=options['output']) 114 115 116if __name__ == "__main__": 117 options, flags = grass.parser() 118 atexit.register(cleanup) 119 main() 120