v.in.e00.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 main():
  58. filename = options['file']
  59. type = options['type']
  60. vect = options['vect']
  61. # save command line
  62. cmdline = grass.basename(sys.argv[0])
  63. cmdline += ' file=' + filename
  64. cmdline += ' type=' + type
  65. if vect:
  66. cmdline += ' vect=' + vect
  67. e00tmp = str(os.getpid())
  68. #### check for avcimport
  69. if not grass.find_program('avcimport'):
  70. grass.fatal("'avcimport' program not found, install it first" +
  71. "\n" +
  72. "http://avce00.maptools.org")
  73. #### check for e00conv
  74. if not grass.find_program('e00conv'):
  75. grass.fatal("'e00conv' program not found, install it first" +
  76. "\n" +
  77. "http://avce00.maptools.org")
  78. # check that the user didn't use all three, which gets past the parser.
  79. if type not in ['point','line','area']:
  80. grass.fatal('Must specify one of "point", "line", or "area".')
  81. e00name = grass.basename(filename, 'e00')
  82. # avcimport only accepts 13 chars:
  83. e00shortname = e00name[:13]
  84. #check if this is a split E00 file (.e01, .e02 ...):
  85. merging = False
  86. if os.path.exists(e00name + '.e01') or os.path.exists(e00name + '.E01'):
  87. grass.message("Found that E00 file is split into pieces (.e01, ...). Merging...")
  88. merging = True
  89. if vect:
  90. name = vect
  91. else:
  92. name = e00name
  93. ### do import
  94. #make a temporary directory
  95. tmpdir = grass.tempfile()
  96. grass.try_remove(tmpdir)
  97. os.mkdir(tmpdir)
  98. files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]')
  99. for f in files:
  100. shutil.copy(f, tmpdir)
  101. #change to temporary directory to later avoid removal problems (rm -r ...)
  102. os.chdir(tmpdir)
  103. #check for binay E00 file (we can just check if import fails):
  104. #avcimport doesn't set exist status :-(
  105. if merging:
  106. files.sort()
  107. filename = "%s.cat.%s.e00" % (e00name, e00tmp)
  108. outf = file(filename, 'wb')
  109. for f in files:
  110. inf = file(f, 'rb')
  111. shutil.copyfileobj(inf, outf)
  112. inf.close()
  113. outf.close()
  114. nuldev = file(os.devnull, 'w+')
  115. grass.message("An error may appear next which will be ignored...")
  116. if subprocess.call(['avcimport', filename, e00shortname], stdout = nuldev, stderr = nuldev) == 1:
  117. grass.message("E00 ASCII found and converted to Arc Coverage in current directory")
  118. else:
  119. grass.message("E00 Compressed ASCII found. Will uncompress first...")
  120. grass.try_remove(e00shortname)
  121. grass.try_remove(info)
  122. subprocess.call(['e00conv', filename, e00tmp + '.e00'])
  123. grass.message("...converted to Arc Coverage in current directory")
  124. subprocess.call(['avcimport', e00tmp + '.e00', e00shortname], stderr = nuldev)
  125. #SQL name fix:
  126. name = name.replace('-', '_')
  127. ## let's import...
  128. grass.message("Importing %ss..." % type)
  129. layer = dict(point = 'LAB', line = 'ARC', area = ['LAB','ARC'])
  130. itype = dict(point = 'point', line = 'line', area = 'centroid')
  131. if grass.run_command('v.in.ogr', flags = 'o', dsn = e00shortname,
  132. layer = layer[type], type = itype[type],
  133. output = name) != 0:
  134. grass.fatal("An error occurred while running v.in.ogr")
  135. grass.message("Imported <%s> vector map <%s>." % (type, name))
  136. #### clean up the mess
  137. for root, dirs, files in os.walk('.', False):
  138. for f in files:
  139. path = os.path.join(root, f)
  140. grass.try_remove(path)
  141. for d in dirs:
  142. path = os.path.join(root, d)
  143. grass.try_rmdir(path)
  144. os.chdir('..')
  145. os.rmdir(tmpdir)
  146. #### end
  147. grass.message("Done.")
  148. # write cmd history:
  149. grass.run_command('v.support', map = name, cmdhist = cmdline)
  150. if __name__ == "__main__":
  151. options, flags = grass.parser()
  152. main()