v.in.e00.py 4.9 KB

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