v.in.lines.py 3.0 KB

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