v.pack.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: v.pack
  5. # AUTHOR(S): Luca Delucchi, Fondazione E. Mach (Italy)
  6. #
  7. # PURPOSE: Pack up a vector map, collect vector map elements => gzip
  8. # COPYRIGHT: (C) 2011-2013 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General
  11. # Public License (>=v2). Read the file COPYING that
  12. # comes with GRASS for details.
  13. #
  14. #############################################################################
  15. # %module
  16. # % description: Exports a vector map as GRASS GIS specific archive file
  17. # % keyword: vector
  18. # % keyword: export
  19. # % keyword: copying
  20. # %end
  21. # %option G_OPT_V_INPUT
  22. # % label: Name of vector map to pack up
  23. # % description:
  24. # %end
  25. # %option G_OPT_F_OUTPUT
  26. # % description: Name for output file (default is <input>.pack)
  27. # % required : no
  28. # %end
  29. # %flag
  30. # % key: c
  31. # % description: Switch the compression off
  32. # %end
  33. import os
  34. import sys
  35. import tarfile
  36. import atexit
  37. from grass.script.utils import try_rmdir, try_remove
  38. from grass.script import core as grass
  39. from grass.script import vector as vector
  40. def cleanup():
  41. try_rmdir(basedir)
  42. def main():
  43. infile = options["input"]
  44. compression_off = flags["c"]
  45. global basedir
  46. basedir = grass.tempdir()
  47. # check if vector map exists
  48. gfile = grass.find_file(infile, element="vector")
  49. if not gfile["name"]:
  50. grass.fatal(_("Vector map <%s> not found") % infile)
  51. # check if input vector map is in the native format
  52. if vector.vector_info(gfile["fullname"])["format"] != "native":
  53. grass.fatal(
  54. _("Unable to pack vector map <%s>. Only native format supported.")
  55. % gfile["fullname"]
  56. )
  57. # split the name if there is the mapset name
  58. if infile.find("@"):
  59. infile = infile.split("@")[0]
  60. # output name
  61. if options["output"]:
  62. outfile = options["output"]
  63. else:
  64. outfile = infile + ".pack"
  65. # check if exists the output file
  66. if os.path.exists(outfile):
  67. if os.getenv("GRASS_OVERWRITE"):
  68. grass.warning(
  69. _("Pack file <%s> already exists and will be overwritten") % outfile
  70. )
  71. try_remove(outfile)
  72. else:
  73. grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
  74. # prepare for packing
  75. grass.verbose(_("Packing <%s>...") % (gfile["fullname"]))
  76. # write tar file, optional compression
  77. if compression_off:
  78. tar = tarfile.open(name=outfile, mode="w:")
  79. else:
  80. tar = tarfile.open(name=outfile, mode="w:gz")
  81. tar.add(gfile["file"], infile)
  82. # check if exist a db connection for the vector
  83. db_vect = vector.vector_db(gfile["fullname"])
  84. if not db_vect:
  85. grass.verbose(
  86. _("There is not database connected with vector map <%s>")
  87. % gfile["fullname"]
  88. )
  89. else:
  90. # for each layer connection save a table in sqlite database
  91. sqlitedb = os.path.join(basedir, "db.sqlite")
  92. for i, dbconn in db_vect.items():
  93. grass.run_command(
  94. "db.copy",
  95. from_driver=dbconn["driver"],
  96. from_database=dbconn["database"],
  97. from_table=dbconn["table"],
  98. to_driver="sqlite",
  99. to_database=sqlitedb,
  100. to_table=dbconn["table"],
  101. )
  102. tar.add(sqlitedb, "db.sqlite")
  103. # add to the tar file the PROJ files to check when unpack file
  104. gisenv = grass.gisenv()
  105. for support in ["INFO", "UNITS", "EPSG"]:
  106. path = os.path.join(
  107. gisenv["GISDBASE"], gisenv["LOCATION_NAME"], "PERMANENT", "PROJ_" + support
  108. )
  109. if os.path.exists(path):
  110. tar.add(path, "PROJ_" + support)
  111. tar.close()
  112. grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
  113. if __name__ == "__main__":
  114. options, flags = grass.parser()
  115. atexit.register(cleanup)
  116. sys.exit(main())