v.pack.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. def cleanup():
  43. try_rmdir(basedir)
  44. def main():
  45. infile = options['input']
  46. compression_off = flags['c']
  47. global basedir
  48. basedir = grass.tempdir()
  49. # check if vector map exists
  50. gfile = grass.find_file(infile, element='vector')
  51. if not gfile['name']:
  52. grass.fatal(_("Vector map <%s> not found") % infile)
  53. # check if input vector map is in the native format
  54. if vector.vector_info(gfile['fullname'])['format'] != 'native':
  55. grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") %
  56. gfile['fullname'])
  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(_("Pack file <%s> already exists and will be overwritten") % outfile)
  69. try_remove(outfile)
  70. else:
  71. grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
  72. # prepare for packing
  73. grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
  74. # write tar file, optional compression
  75. if compression_off:
  76. tar = tarfile.open(name=outfile, mode='w:')
  77. else:
  78. tar = tarfile.open(name=outfile, mode='w:gz')
  79. tar.add(gfile['file'], infile)
  80. # check if exist a db connection for the vector
  81. db_vect = vector.vector_db(gfile['fullname'])
  82. if not db_vect:
  83. grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
  84. else:
  85. # for each layer connection save a table in sqlite database
  86. sqlitedb = os.path.join(basedir, 'db.sqlite')
  87. for i, dbconn in db_vect.iteritems():
  88. grass.run_command('db.copy', from_driver=dbconn['driver'],
  89. from_database=dbconn['database'],
  90. from_table=dbconn['table'],
  91. to_driver='sqlite', to_database=sqlitedb,
  92. to_table=dbconn['table'])
  93. tar.add(sqlitedb, 'db.sqlite')
  94. # add to the tar file the PROJ files to check when unpack file
  95. gisenv = grass.gisenv()
  96. for support in ['INFO', 'UNITS', 'EPSG']:
  97. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  98. 'PERMANENT', 'PROJ_' + support)
  99. if os.path.exists(path):
  100. tar.add(path, 'PROJ_' + support)
  101. tar.close()
  102. grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
  103. if __name__ == "__main__":
  104. options, flags = grass.parser()
  105. atexit.register(cleanup)
  106. sys.exit(main())