v.in.lines.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. import sys
  37. import os
  38. import atexit
  39. import string
  40. from grass.script.utils import separator, try_remove
  41. from grass.script import core as grass
  42. def cleanup():
  43. try_remove(tmp)
  44. def main():
  45. global tmp
  46. fs = separator(options['separator'])
  47. threeD = flags['z']
  48. prog = 'v.in.lines'
  49. if threeD:
  50. do3D = 'z'
  51. else:
  52. do3D = ''
  53. tmp = grass.tempfile()
  54. #### set up input file
  55. if options['input'] == '-':
  56. infile = None
  57. inf = sys.stdin
  58. else:
  59. infile = options['input']
  60. if not os.path.exists(infile):
  61. grass.fatal(_("Unable to read input file <%s>") % infile)
  62. grass.debug("input file=[%s]" % infile)
  63. if not infile:
  64. # read from stdin and write to tmpfile (v.in.mapgen wants a real file)
  65. outf = file(tmp, 'w')
  66. for line in inf:
  67. if len(line.lstrip()) == 0 or line[0] == '#':
  68. continue
  69. outf.write(line.replace(fs, ' '))
  70. outf.close()
  71. runfile = tmp
  72. else:
  73. # read from a real file
  74. if fs == ' ':
  75. runfile = infile
  76. else:
  77. inf = file(infile)
  78. outf = file(tmp, 'w')
  79. for line in inf:
  80. if len(line.lstrip()) == 0 or line[0] == '#':
  81. continue
  82. outf.write(line.replace(fs, ' '))
  83. inf.close()
  84. outf.close()
  85. runfile = tmp
  86. ##### check that there are at least two columns (three if -z is given)
  87. inf = file(runfile)
  88. for line in inf:
  89. if len(line.lstrip()) == 0 or line[0] == '#':
  90. continue
  91. numcols = len(line.split())
  92. break
  93. inf.close()
  94. if (do3D and numcols < 3) or (not do3D and numcols < 2):
  95. grass.fatal(_("Not enough data columns. (incorrect fs setting?)"))
  96. grass.run_command('v.in.mapgen', flags = 'f' + do3D,
  97. input = runfile, output = options['output'])
  98. if __name__ == "__main__":
  99. options, flags = grass.parser()
  100. atexit.register(cleanup)
  101. main()