~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/v.in.lines/v.in.lines.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
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
 
 
37
import sys
 
38
import os
 
39
import atexit
 
40
import string
 
41
from grass.script.utils import separator, try_remove
 
42
from grass.script import core as grass
 
43
 
 
44
def cleanup():
 
45
    try_remove(tmp)
 
46
 
 
47
def main():
 
48
    global tmp
 
49
 
 
50
    fs = separator(options['separator'])
 
51
    threeD = flags['z']
 
52
 
 
53
    prog = 'v.in.lines'
 
54
 
 
55
    if threeD:
 
56
        do3D = 'z'
 
57
    else:
 
58
        do3D = ''
 
59
 
 
60
 
 
61
    tmp = grass.tempfile()
 
62
 
 
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
 
 
75
    if not infile:
 
76
        # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
 
77
        outf = file(tmp, 'w')
 
78
        for line in inf:
 
79
            if len(line.lstrip()) == 0 or line[0] == '#':
 
80
                continue
 
81
            outf.write(line.replace(fs, ' '))
 
82
 
 
83
        outf.close()
 
84
        runfile = tmp
 
85
    else:
 
86
        # read from a real file
 
87
        if fs == ' ':
 
88
            runfile = infile
 
89
        else:
 
90
            inf = file(infile)
 
91
            outf = file(tmp, 'w')
 
92
 
 
93
            for line in inf:
 
94
                if len(line.lstrip()) == 0 or line[0] == '#':
 
95
                    continue
 
96
                outf.write(line.replace(fs, ' '))
 
97
 
 
98
            inf.close()
 
99
            outf.close()
 
100
            runfile = tmp
 
101
 
 
102
 
 
103
    ##### check that there are at least two columns (three if -z is given)
 
104
    inf = file(runfile)
 
105
    for line in inf:
 
106
        if len(line.lstrip()) == 0 or line[0] == '#':
 
107
            continue
 
108
        numcols = len(line.split())
 
109
        break
 
110
    inf.close()
 
111
    if (do3D and numcols < 3) or (not do3D and numcols < 2):
 
112
        grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))
 
113
 
 
114
 
 
115
    grass.run_command('v.in.mapgen', flags = 'f' + do3D,
 
116
                      input = runfile, output = options['output'])
 
117
 
 
118
 
 
119
if __name__ == "__main__":
 
120
    options, flags = grass.parser()
 
121
    atexit.register(cleanup)
 
122
    main()