v.pack.py 3.9 KB

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