v.in.lines.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #% keywords: vector
  21. #% keywords: import
  22. #%end
  23. #%flag
  24. #% key: z
  25. #% description: Create a 3D line from 3 column data
  26. #%end
  27. #%option G_OPT_F_INPUT
  28. #% description: Name of input file (or "-" to read from stdin)
  29. #%end
  30. #%option G_OPT_V_OUTPUT
  31. #%end
  32. #%option G_OPT_F_SEP
  33. #%end
  34. import sys
  35. import os
  36. import atexit
  37. import string
  38. from grass.script import core as grass
  39. def cleanup():
  40. grass.try_remove(tmp)
  41. def main():
  42. global tmp
  43. fs = options['separator']
  44. threeD = flags['z']
  45. prog = 'v.in.lines'
  46. if threeD:
  47. do3D = 'z'
  48. else:
  49. do3D = ''
  50. tmp = grass.tempfile()
  51. #### parse field separator
  52. if fs in ('space', 'tab'):
  53. fs = ' '
  54. elif fs == 'comma':
  55. fs = ','
  56. else:
  57. if len(fs) > 1:
  58. grass.warning(_("Invalid field separator, using '%s'") % fs[0])
  59. try:
  60. fs = fs[0]
  61. except IndexError:
  62. grass.fatal(_("Invalid field separator '%s'") % fs)
  63. #### set up input file
  64. if options['input'] == '-':
  65. infile = None
  66. inf = sys.stdin
  67. else:
  68. infile = options['input']
  69. if not os.path.exists(infile):
  70. grass.fatal(_("Unable to read input file <%s>") % infile)
  71. grass.debug("input file=[%s]" % infile)
  72. if not infile:
  73. # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
  74. outf = file(tmp, 'w')
  75. for line in inf:
  76. if len(line.lstrip()) == 0 or line[0] == '#':
  77. continue
  78. outf.write(line.replace(fs, ' '))
  79. outf.close()
  80. runfile = tmp
  81. else:
  82. # read from a real file
  83. if fs == ' ':
  84. runfile = infile
  85. else:
  86. inf = file(infile)
  87. outf = file(tmp, 'w')
  88. for line in inf:
  89. if len(line.lstrip()) == 0 or line[0] == '#':
  90. continue
  91. outf.write(line.replace(fs, ' '))
  92. inf.close()
  93. outf.close()
  94. runfile = tmp
  95. ##### check that there are at least two columns (three if -z is given)
  96. inf = file(runfile)
  97. for line in inf:
  98. if len(line.lstrip()) == 0 or line[0] == '#':
  99. continue
  100. numcols = len(line.split())
  101. break
  102. inf.close()
  103. if (do3D and numcols < 3) or (not do3D and numcols < 2):
  104. grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))
  105. grass.run_command('v.in.mapgen', flags = 'f' + do3D,
  106. input = runfile, output = options['output'])
  107. if __name__ == "__main__":
  108. options, flags = grass.parser()
  109. atexit.register(cleanup)
  110. main()