|
@@ -46,61 +46,74 @@ from grass.script import vector as vector
|
|
def main():
|
|
def main():
|
|
infile = options['input']
|
|
infile = options['input']
|
|
compression_off = flags['c']
|
|
compression_off = flags['c']
|
|
- #search if file exist
|
|
|
|
|
|
+
|
|
|
|
+ # check if vector map exist
|
|
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)
|
|
- #split the name if there is the mapset name
|
|
|
|
|
|
+
|
|
|
|
+ # check if input vector map is in native format
|
|
|
|
+ if vector.vector_info(gfile['fullname'])['format'] != 'native':
|
|
|
|
+ grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
|
|
|
|
+ gfile['fullname'])
|
|
|
|
+
|
|
|
|
+ # split the name if there is the mapset name
|
|
if infile.find('@'):
|
|
if infile.find('@'):
|
|
- infile = infile.split('@')[0]
|
|
|
|
- #output name
|
|
|
|
|
|
+ infile = infile.split('@')[0]
|
|
|
|
+
|
|
|
|
+ # output name
|
|
if options['output']:
|
|
if options['output']:
|
|
outfile = options['output']
|
|
outfile = options['output']
|
|
else:
|
|
else:
|
|
outfile = infile + '.pack'
|
|
outfile = infile + '.pack'
|
|
- #check if exists the output file
|
|
|
|
|
|
+
|
|
|
|
+ # check if exists the output file
|
|
if os.path.exists(outfile):
|
|
if os.path.exists(outfile):
|
|
if os.getenv('GRASS_OVERWRITE'):
|
|
if os.getenv('GRASS_OVERWRITE'):
|
|
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 <output>: <%s> exists.") % outfile)
|
|
|
|
+
|
|
|
|
+ # 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])
|
|
basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
|
|
olddir = os.getcwd()
|
|
olddir = os.getcwd()
|
|
- #check if exist a db connection for the vector
|
|
|
|
- db_vect = vector.vector_db(gfile['fullname'])
|
|
|
|
|
|
|
|
- #db not exist and skip the db copy
|
|
|
|
|
|
+ # check if exist a db connection for the vector
|
|
|
|
+ db_vect = vector.vector_db(gfile['fullname'])
|
|
sqlitedb = None
|
|
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
|
|
|
|
|
|
+ # for each layer connection save a table in sqlite database
|
|
for i, dbconn in db_vect.iteritems():
|
|
for i, dbconn in db_vect.iteritems():
|
|
sqlitedb = os.path.join(basedir, 'vector', infile, 'db.sqlite')
|
|
sqlitedb = os.path.join(basedir, 'vector', infile, 'db.sqlite')
|
|
cptable = grass.run_command('db.copy', from_driver = dbconn['driver'],
|
|
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'])
|
|
|
|
-
|
|
|
|
|
|
+ from_database = dbconn['database'], from_table = dbconn['table'],
|
|
|
|
+ to_driver = 'sqlite', to_database = sqlitedb,
|
|
|
|
+ to_table = dbconn['table'])
|
|
|
|
+
|
|
# write tar file, optional compression
|
|
# write tar file, optional compression
|
|
if compression_off:
|
|
if compression_off:
|
|
tar = tarfile.open(name = outfile, mode = 'w:')
|
|
tar = tarfile.open(name = outfile, mode = 'w:')
|
|
else:
|
|
else:
|
|
tar = tarfile.open(name = outfile, mode = 'w:gz')
|
|
tar = tarfile.open(name = outfile, mode = 'w:gz')
|
|
- tar.add(os.path.join(basedir,'vector',infile),infile)
|
|
|
|
|
|
+ tar.add(os.path.join(basedir,'vector', infile), infile)
|
|
|
|
+
|
|
|
|
+ # add to the tar file the PROJ files to check when unpack file
|
|
gisenv = grass.gisenv()
|
|
gisenv = grass.gisenv()
|
|
- #add to the tar file the PROJ files to check when unpack file
|
|
|
|
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
|
|
|
|
|
|
+
|
|
|
|
+ # remove the db from the vector directory (ONLY THE DB FOR THE COPY NOT DB OF GRASS)
|
|
if db_vect and sqlitedb:
|
|
if db_vect and sqlitedb:
|
|
os.remove(sqlitedb)
|
|
os.remove(sqlitedb)
|
|
|
|
+
|
|
grass.message(_("Pack file <%s> created") % os.path.join(olddir, outfile))
|
|
grass.message(_("Pack file <%s> created") % os.path.join(olddir, outfile))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|