v.pack.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 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: Packs up a vector map and support files for copying.
  18. #% keywords: vector, export, copying
  19. #%end
  20. #%option
  21. #% key: input
  22. #% type: string
  23. #% gisprompt: old,vector,vector
  24. #% description: Name of vector map to pack up
  25. #% key_desc: name
  26. #% required : yes
  27. #%end
  28. #%option G_OPT_F_OUTPUT
  29. #% description: Name for output file (default is <input>.pack)
  30. #% required : no
  31. #%end
  32. #%flag
  33. #% key: c
  34. #% description: Switch the compression off
  35. #%end
  36. import os
  37. import sys
  38. import shutil
  39. import tarfile
  40. from grass.script import core as grass
  41. from grass.script import vector as vector
  42. def main():
  43. infile = options['input']
  44. compression_off = flags['c']
  45. #search if file exist
  46. gfile = grass.find_file(infile, element = 'vector')
  47. if not gfile['name']:
  48. grass.fatal(_("Vector map <%s> not found") % infile)
  49. #split the name if there is the mapset name
  50. if infile.find('@'):
  51. infile = infile.split('@')[0]
  52. #output name
  53. if options['output']:
  54. outfile = options['output']
  55. else:
  56. outfile = infile + '.pack'
  57. #check if exists the output file
  58. if os.path.exists(outfile):
  59. if os.getenv('GRASS_OVERWRITE'):
  60. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  61. grass.try_remove(outfile)
  62. else:
  63. grass.fatal(_("option <output>: <%s> exists.") % outfile)
  64. grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
  65. basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
  66. olddir = os.getcwd()
  67. #check if exist a db connection for the vector
  68. db_vect = vector.vector_db(gfile['fullname'])
  69. #db not exist and skip the db copy
  70. sqlitedb = None
  71. if not db_vect:
  72. grass.message(_('There is not database connected with vector %s') % gfile['fullname'])
  73. else:
  74. # for each layer connection save a table
  75. for i, dbconn in db_vect.iteritems():
  76. sqlitedb = os.path.join(basedir, 'vector', infile, 'db.sqlite')
  77. cptable = grass.run_command('db.copy', from_driver = dbconn['driver'],
  78. from_database = dbconn['database'], from_table = dbconn['table'],
  79. to_driver = 'sqlite', to_database = sqlitedb,
  80. to_table = dbconn['table'])
  81. # write tar file, optional compression
  82. if compression_off:
  83. tar = tarfile.open(name = outfile, mode = 'w:')
  84. else:
  85. tar = tarfile.open(name = outfile, mode = 'w:gz')
  86. tar.add(os.path.join(basedir,'vector',infile),infile)
  87. gisenv = grass.gisenv()
  88. #add to the tar file the PROJ files to check when unpack file
  89. for support in ['INFO', 'UNITS']:
  90. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  91. 'PERMANENT', 'PROJ_' + support)
  92. if os.path.exists(path):
  93. tar.add(path,os.path.join(infile,'PROJ_' + support))
  94. tar.close()
  95. #remove the db from the vector directory #ONLY THE DB FOR THE COPY NOT DB OF GRASS
  96. if db_vect and sqlitedb:
  97. os.remove(sqlitedb)
  98. grass.verbose(_("Vector map saved to '%s'" % os.path.join(olddir, outfile)))
  99. if __name__ == "__main__":
  100. options, flags = grass.parser()
  101. sys.exit(main())