v.in.lines.py 3.5 KB

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