v.pack.py 3.9 KB

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