v.in.e00.py 4.5 KB

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