v.pack.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  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 shutil
  37. import tarfile
  38. import atexit
  39. from grass.script.utils import try_rmdir, try_remove
  40. from grass.script import core as grass
  41. from grass.script import vector as vector
  42. # i18N
  43. import gettext
  44. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  45. def cleanup():
  46. try_rmdir(basedir)
  47. def main():
  48. infile = options['input']
  49. compression_off = flags['c']
  50. global basedir
  51. basedir = grass.tempdir()
  52. # check if vector map exists
  53. gfile = grass.find_file(infile, element='vector')
  54. if not gfile['name']:
  55. grass.fatal(_("Vector map <%s> not found") % infile)
  56. # check if input vector map is in the native format
  57. if vector.vector_info(gfile['fullname'])['format'] != 'native':
  58. grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") %
  59. gfile['fullname'])
  60. # split the name if there is the mapset name
  61. if infile.find('@'):
  62. infile = infile.split('@')[0]
  63. # output name
  64. if options['output']:
  65. outfile = options['output']
  66. else:
  67. outfile = infile + '.pack'
  68. # check if exists the output file
  69. if os.path.exists(outfile):
  70. if os.getenv('GRASS_OVERWRITE'):
  71. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  72. try_remove(outfile)
  73. else:
  74. grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
  75. # prepare for packing
  76. grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
  77. # write tar file, optional compression
  78. if compression_off:
  79. tar = tarfile.open(name=outfile, mode='w:')
  80. else:
  81. tar = tarfile.open(name=outfile, mode='w:gz')
  82. tar.add(gfile['file'], infile)
  83. # check if exist a db connection for the vector
  84. db_vect = vector.vector_db(gfile['fullname'])
  85. if not db_vect:
  86. grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
  87. else:
  88. # for each layer connection save a table in sqlite database
  89. sqlitedb = os.path.join(basedir, 'db.sqlite')
  90. for i, dbconn in db_vect.items():
  91. grass.run_command('db.copy', from_driver=dbconn['driver'],
  92. from_database=dbconn['database'],
  93. from_table=dbconn['table'],
  94. to_driver='sqlite', to_database=sqlitedb,
  95. to_table=dbconn['table'])
  96. tar.add(sqlitedb, 'db.sqlite')
  97. # add to the tar file the PROJ files to check when unpack file
  98. gisenv = grass.gisenv()
  99. for support in ['INFO', 'UNITS', 'EPSG']:
  100. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  101. 'PERMANENT', 'PROJ_' + support)
  102. if os.path.exists(path):
  103. tar.add(path, 'PROJ_' + support)
  104. tar.close()
  105. grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
  106. if __name__ == "__main__":
  107. options, flags = grass.parser()
  108. atexit.register(cleanup)
  109. sys.exit(main())