v.in.e00.py 4.6 KB

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