v.in.e00.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.in.e00
  5. #
  6. # AUTHOR(S): Markus Neteler, Otto Dassau
  7. # Converted to Python by Glynn Clements
  8. #
  9. # PURPOSE: Import E00 data into a GRASS vector map
  10. # Imports single and split E00 files (.e00, .e01, .e02 ...)
  11. #
  12. # COPYRIGHT: (c) 2004, 2005 GDF Hannover bR, http://www.gdf-hannover.de
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. #############################################################################
  19. #
  20. # REQUIREMENTS:
  21. # - avcimport: http://avce00.maptools.org
  22. #%Module
  23. #% description: Import E00 file into a vector map.
  24. #% keywords: vector, import
  25. #%End
  26. #%flag
  27. #% key: v
  28. #% description: Verbose mode
  29. #%end
  30. #%option
  31. #% key: file
  32. #% type: string
  33. #% description: E00 file
  34. #% gisprompt: old_file,file,input
  35. #% required : yes
  36. #%end
  37. #%option
  38. #% key: type
  39. #% type: string
  40. #% options: point,line,area
  41. #% description: Input type point, line or area
  42. #% required : yes
  43. #%end
  44. #%option
  45. #% key: vect
  46. #% type: string
  47. #% gisprompt: new,vector,vector
  48. #% description: Name for output vector map
  49. #% required : no
  50. #%end
  51. import sys
  52. import os
  53. import shutil
  54. import subprocess
  55. import glob
  56. import grass
  57. def find_program(pgm):
  58. nuldev = file(os.devnull, 'w+')
  59. try:
  60. subprocess.call([pgm], stdin = nuldev, stdout = nuldev, stderr = nuldev)
  61. found = True
  62. except:
  63. found = False
  64. nuldev.close()
  65. return found
  66. def main():
  67. filename = options['file']
  68. type = options['type']
  69. vect = options['vect']
  70. # save command line
  71. cmdline = os.path.basename(sys.argv[0])
  72. cmdline += ' file=' + filename
  73. cmdline += ' type=' + type
  74. if vect:
  75. cmdline += ' vect=' + vect
  76. e00tmp = str(os.getpid())
  77. #### check for avcimport
  78. if not find_program('avcimport'):
  79. grass.fatal("'avcimport' program not found, install it first" +
  80. "\n" +
  81. "http://avce00.maptools.org")
  82. #### check for e00conv
  83. if not find_program('e00conv'):
  84. grass.fatal("'e00conv' program not found, install it first" +
  85. "\n" +
  86. "http://avce00.maptools.org")
  87. # check that the user didn't use all three, which gets past the parser.
  88. if type not in ['point','line','area']:
  89. grass.fatal('Must specify one of "point", "line", or "area".')
  90. e00name = os.path.basename(filename)
  91. fs = os.path.splitext(e00name)
  92. if fs[1].lower() == '.e00':
  93. e00name = fs[0]
  94. # avcimport only accepts 13 chars:
  95. e00shortname = e00name[:13]
  96. #check if this is a split E00 file (.e01, .e02 ...):
  97. merging = False
  98. if os.path.exists(e00name + '.e01') or os.path.exists(e00name + '.E01'):
  99. grass.message("Found that E00 file is split into pieces (.e01, ...). Merging...")
  100. merging = True
  101. if vect:
  102. name = vect
  103. else:
  104. name = e00name
  105. ### do import
  106. #make a temporary directory
  107. tmpdir = grass.tempfile()
  108. try:
  109. os.remove(tmpdir)
  110. except:
  111. pass
  112. os.mkdir(tmpdir)
  113. files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]')
  114. for f in files:
  115. shutil.copy(f, tmpdir)
  116. #change to temporary directory to later avoid removal problems (rm -r ...)
  117. os.chdir(tmpdir)
  118. #check for binay E00 file (we can just check if import fails):
  119. #avcimport doesn't set exist status :-(
  120. if merging:
  121. files.sort()
  122. filename = "%s.cat.%s.e00" % (e00name, e00tmp)
  123. outf = file(filename, 'wb')
  124. for f in files:
  125. inf = file(f, 'rb')
  126. shutil.copyfileobj(inf, outf)
  127. inf.close()
  128. outf.close()
  129. nuldev = file(os.devnull, 'w+')
  130. grass.message("An error may appear next which will be ignored...")
  131. if subprocess.call(['avcimport', filename, e00shortname], stdout = nuldev, stderr = nuldev) == 1:
  132. grass.message("E00 ASCII found and converted to Arc Coverage in current directory")
  133. else:
  134. grass.message("E00 Compressed ASCII found. Will uncompress first...")
  135. try:
  136. os.remove(e00shortname)
  137. os.remove(info)
  138. except:
  139. pass
  140. subprocess.call(['e00conv', filename, e00tmp + '.e00'])
  141. grass.message("...converted to Arc Coverage in current directory")
  142. subprocess.call(['avcimport', e00tmp + '.e00', e00shortname], stderr = nuldev)
  143. #SQL name fix:
  144. name = name.replace('-', '_')
  145. ## let's import...
  146. grass.message("Importing %ss..." % type)
  147. layer = dict(point = 'LAB', line = 'ARC', area = ['LAB','ARC'])
  148. itype = dict(point = 'point', line = 'line', area = 'centroid')
  149. if grass.run_command('v.in.ogr', flags = 'o', dsn = e00shortname,
  150. layer = layer[type], type = itype[type],
  151. output = name) != 0:
  152. grass.fatal("An error occurred while running v.in.ogr")
  153. grass.message("Imported <%s> vector map <%s>." % (type, name))
  154. #### clean up the mess
  155. for root, dirs, files in os.walk('.', False):
  156. for f in files:
  157. path = os.path.join(root, f)
  158. try:
  159. os.remove(path)
  160. except:
  161. pass
  162. for d in dirs:
  163. path = os.path.join(root, d)
  164. try:
  165. os.rmdir(path)
  166. except:
  167. pass
  168. os.chdir('..')
  169. os.rmdir(tmpdir)
  170. #### end
  171. grass.message("Done.")
  172. # write cmd history:
  173. grass.run_command('v.support', map = name, cmdhist = cmdline)
  174. if __name__ == "__main__":
  175. options, flags = grass.parser()
  176. main()