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 vector as gvect
  42. from grass.script import core as grass
  43. from grass.exceptions import CalledModuleError
  44. def main():
  45. filename = options['input']
  46. type = options['type']
  47. vect = options['output']
  48. e00tmp = str(os.getpid())
  49. #### check for avcimport
  50. if not grass.find_program('avcimport'):
  51. grass.fatal(_("'avcimport' program not found, install it first") +
  52. "\n" +
  53. "http://avce00.maptools.org")
  54. #### check for e00conv
  55. if not grass.find_program('e00conv'):
  56. grass.fatal(_("'e00conv' program not found, install it first") +
  57. "\n" +
  58. "http://avce00.maptools.org")
  59. # check that the user didn't use all three, which gets past the parser.
  60. if type not in ['point','line','area']:
  61. grass.fatal(_('Must specify one of "point", "line", or "area".'))
  62. e00name = basename(filename, 'e00')
  63. # avcimport only accepts 13 chars:
  64. e00shortname = e00name[:13]
  65. #check if this is a split E00 file (.e01, .e02 ...):
  66. merging = False
  67. if os.path.exists(e00name + '.e01') or os.path.exists(e00name + '.E01'):
  68. grass.message(_("Found that E00 file is split into pieces (.e01, ...). 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 = grass.tempfile()
  77. try_remove(tmpdir)
  78. os.mkdir(tmpdir)
  79. files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]')
  80. for f in files:
  81. shutil.copy(f, tmpdir)
  82. #change to temporary directory to later avoid removal problems (rm -r ...)
  83. os.chdir(tmpdir)
  84. #check for binay E00 file (we can just check if import fails):
  85. #avcimport doesn't set exist status :-(
  86. if merging:
  87. files.sort()
  88. filename = "%s.cat.%s.e00" % (e00name, e00tmp)
  89. outf = file(filename, 'wb')
  90. for f in files:
  91. inf = file(f, 'rb')
  92. shutil.copyfileobj(inf, outf)
  93. inf.close()
  94. outf.close()
  95. nuldev = file(os.devnull, 'w+')
  96. grass.message(_("An error may appear next which will be ignored..."))
  97. if grass.call(['avcimport', filename, e00shortname], stdout = nuldev, stderr = nuldev) == 1:
  98. grass.message(_("E00 ASCII found and converted to Arc Coverage in current directory"))
  99. else:
  100. grass.message(_("E00 Compressed ASCII found. Will uncompress first..."))
  101. try_remove(e00shortname)
  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. gvect.vector_history(name)
  132. if __name__ == "__main__":
  133. options, flags = grass.parser()
  134. main()