v.in.e00.py 5.0 KB

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