v.in.e00.py 5.0 KB

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