|
@@ -6,7 +6,7 @@
|
|
# AUTHOR(S): Luca Delucchi, Fondazione E. Mach (Italy)
|
|
# AUTHOR(S): Luca Delucchi, Fondazione E. Mach (Italy)
|
|
#
|
|
#
|
|
# PURPOSE: Pack up a vector map, collect vector map elements => gzip
|
|
# PURPOSE: Pack up a vector map, collect vector map elements => gzip
|
|
-# COPYRIGHT: (C) 2011 by the GRASS Development Team
|
|
|
|
|
|
+# COPYRIGHT: (C) 2011-2013 by the GRASS Development Team
|
|
#
|
|
#
|
|
# This program is free software under the GNU General
|
|
# This program is free software under the GNU General
|
|
# Public License (>=v2). Read the file COPYING that
|
|
# Public License (>=v2). Read the file COPYING that
|
|
@@ -18,13 +18,9 @@
|
|
#% description: Packs up a vector map and support files for copying.
|
|
#% description: Packs up a vector map and support files for copying.
|
|
#% keywords: vector, export, copying
|
|
#% keywords: vector, export, copying
|
|
#%end
|
|
#%end
|
|
-#%option
|
|
|
|
-#% key: input
|
|
|
|
-#% type: string
|
|
|
|
-#% gisprompt: old,vector,vector
|
|
|
|
-#% description: Name of vector map to pack up
|
|
|
|
-#% key_desc: name
|
|
|
|
-#% required : yes
|
|
|
|
|
|
+#%option G_OPT_V_INPUT
|
|
|
|
+#% label: Name of vector map to pack up
|
|
|
|
+#% description:
|
|
#%end
|
|
#%end
|
|
#%option G_OPT_F_OUTPUT
|
|
#%option G_OPT_F_OUTPUT
|
|
#% description: Name for output file (default is <input>.pack)
|
|
#% description: Name for output file (default is <input>.pack)
|
|
@@ -47,12 +43,12 @@ def main():
|
|
infile = options['input']
|
|
infile = options['input']
|
|
compression_off = flags['c']
|
|
compression_off = flags['c']
|
|
|
|
|
|
- # check if vector map exist
|
|
|
|
|
|
+ # check if vector map exists
|
|
gfile = grass.find_file(infile, element = 'vector')
|
|
gfile = grass.find_file(infile, element = 'vector')
|
|
if not gfile['name']:
|
|
if not gfile['name']:
|
|
grass.fatal(_("Vector map <%s> not found") % infile)
|
|
grass.fatal(_("Vector map <%s> not found") % infile)
|
|
|
|
|
|
- # check if input vector map is in native format
|
|
|
|
|
|
+ # check if input vector map is in the native format
|
|
if vector.vector_info(gfile['fullname'])['format'] != 'native':
|
|
if vector.vector_info(gfile['fullname'])['format'] != 'native':
|
|
grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
|
|
grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
|
|
gfile['fullname'])
|
|
gfile['fullname'])
|
|
@@ -73,48 +69,44 @@ def main():
|
|
grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
|
|
grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
|
|
grass.try_remove(outfile)
|
|
grass.try_remove(outfile)
|
|
else:
|
|
else:
|
|
- grass.fatal(_("option <output>: <%s> exists.") % outfile)
|
|
|
|
|
|
+ grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
|
|
|
|
|
|
# prepare for packing
|
|
# prepare for packing
|
|
grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
|
|
grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
|
|
- basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
|
|
|
|
- olddir = os.getcwd()
|
|
|
|
|
|
+ basedir = grass.tempdir()
|
|
|
|
|
|
|
|
+ # write tar file, optional compression
|
|
|
|
+ if compression_off:
|
|
|
|
+ tar = tarfile.open(name = outfile, mode = 'w:')
|
|
|
|
+ else:
|
|
|
|
+ tar = tarfile.open(name = outfile, mode = 'w:gz')
|
|
|
|
+ tar.add(gfile['file'], infile)
|
|
|
|
+
|
|
# check if exist a db connection for the vector
|
|
# check if exist a db connection for the vector
|
|
db_vect = vector.vector_db(gfile['fullname'])
|
|
db_vect = vector.vector_db(gfile['fullname'])
|
|
- sqlitedb = None
|
|
|
|
if not db_vect:
|
|
if not db_vect:
|
|
grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
|
|
grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
|
|
else:
|
|
else:
|
|
# for each layer connection save a table in sqlite database
|
|
# for each layer connection save a table in sqlite database
|
|
|
|
+ sqlitedb = os.path.join(basedir, 'db.sqlite')
|
|
for i, dbconn in db_vect.iteritems():
|
|
for i, dbconn in db_vect.iteritems():
|
|
- sqlitedb = os.path.join(basedir, 'vector', infile, 'db.sqlite')
|
|
|
|
- cptable = grass.run_command('db.copy', from_driver = dbconn['driver'],
|
|
|
|
- from_database = dbconn['database'], from_table = dbconn['table'],
|
|
|
|
- to_driver = 'sqlite', to_database = sqlitedb,
|
|
|
|
- to_table = dbconn['table'])
|
|
|
|
|
|
+ grass.run_command('db.copy', from_driver = dbconn['driver'],
|
|
|
|
+ from_database = dbconn['database'],
|
|
|
|
+ from_table = dbconn['table'],
|
|
|
|
+ to_driver = 'sqlite', to_database = sqlitedb,
|
|
|
|
+ to_table = dbconn['table'])
|
|
|
|
+ tar.add(sqlitedb, os.path.join(infile, 'db.sqlite'))
|
|
|
|
|
|
- # write tar file, optional compression
|
|
|
|
- if compression_off:
|
|
|
|
- tar = tarfile.open(name = outfile, mode = 'w:')
|
|
|
|
- else:
|
|
|
|
- tar = tarfile.open(name = outfile, mode = 'w:gz')
|
|
|
|
- tar.add(os.path.join(basedir,'vector', infile), infile)
|
|
|
|
-
|
|
|
|
# add to the tar file the PROJ files to check when unpack file
|
|
# add to the tar file the PROJ files to check when unpack file
|
|
gisenv = grass.gisenv()
|
|
gisenv = grass.gisenv()
|
|
for support in ['INFO', 'UNITS']:
|
|
for support in ['INFO', 'UNITS']:
|
|
path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
|
|
path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
|
|
'PERMANENT', 'PROJ_' + support)
|
|
'PERMANENT', 'PROJ_' + support)
|
|
if os.path.exists(path):
|
|
if os.path.exists(path):
|
|
- tar.add(path,os.path.join(infile,'PROJ_' + support))
|
|
|
|
|
|
+ tar.add(path, os.path.join(infile, 'PROJ_' + support))
|
|
tar.close()
|
|
tar.close()
|
|
|
|
|
|
- # remove the db from the vector directory (ONLY THE DB FOR THE COPY NOT DB OF GRASS)
|
|
|
|
- if db_vect and sqlitedb:
|
|
|
|
- os.remove(sqlitedb)
|
|
|
|
-
|
|
|
|
- grass.message(_("Pack file <%s> created") % os.path.join(olddir, outfile))
|
|
|
|
|
|
+ grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
options, flags = grass.parser()
|
|
options, flags = grass.parser()
|