v.in.e00.py 4.7 KB

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