v.in.lines.py 2.9 KB

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