Ver código fonte

Moved v.pack and v.unpack from grass-addons to trunk.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@53397 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 12 anos atrás
pai
commit
2a1a3b689c

+ 2 - 0
scripts/Makefile

@@ -65,6 +65,8 @@ SUBDIRS = \
 	v.rast.stats \
 	v.rast.stats \
 	v.report \
 	v.report \
 	v.out.gps \
 	v.out.gps \
+	v.pack \
+	v.unpack \
 	v.what.vect \
 	v.what.vect \
 	wxpyimgview
 	wxpyimgview
 
 

+ 7 - 0
scripts/v.pack/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.pack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 36 - 0
scripts/v.pack/v.pack.html

@@ -0,0 +1,36 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.pack</em> collects vector map elements and support files and
+compressed them using <em>gzip</em> algorithm for copying. The packed
+file can be afterwards unpacked
+by <em><a href="v.unpack.html">v.unpack</a></em>.
+
+<h2>NOTES</h2>
+
+Name of the pack file is determined by default from <b>input</b>
+parameter. Optionaly the name can be given by <b>output</b> parameter.
+
+<h2>EXAMPLE</h2>
+
+Pack up vector map <i>random_point</i> into <i>random_point.pack</i> file.
+
+<div class="code"><pre>
+v.pack input=random_point
+</pre></div>
+
+the vector map can be afterwards unpacked by
+
+<div class="code"><pre>
+v.unpack input=random_point.pack
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="v.unpack.html">v.unpack</a>,
+  <a href="v.in.gdal.html">v.in.gdal</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Luca Delucchi, Fondazione E. Mach (Italy), based on the <em>r.pack</em> code

+ 112 - 0
scripts/v.pack/v.pack.py

@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       v.pack
+# AUTHOR(S):    Luca Delucchi, Fondazione E. Mach (Italy)
+#
+# PURPOSE:      Pack up a vector map, collect vector map elements => gzip
+# COPYRIGHT:    (C) 2011 by the GRASS Development Team
+#
+#               This program is free software under the GNU General
+#               Public License (>=v2). Read the file COPYING that
+#               comes with GRASS for details.
+#
+#############################################################################
+
+#%module
+#% description: Packs up a vector map and support files for copying.
+#% keywords: vector, export, copying
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,vector,vector
+#% description: Name of vector map to pack up
+#% key_desc: name
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new_file,file,output
+#% description: Name for output file (default is <input>.pack)
+#% key_desc: path
+#% required : no
+#%end
+#%flag
+#% key: c
+#% description: Switch the compression off
+#%end
+
+import os
+import sys
+import shutil
+import tarfile
+
+from grass.script import core as grass
+from grass.script import vector as vector
+
+def main():
+    infile = options['input']
+    compression_off = flags['c']
+    #search if file exist
+    gfile = grass.find_file(infile, element = 'vector')
+    if not gfile['name']:
+        grass.fatal(_("Vector map <%s> not found") % infile)
+    #split the name if there is the mapset name
+    if infile.find('@'):
+        infile = infile.split('@')[0]    
+    #output name
+    if options['output']:
+        outfile = options['output']
+    else:
+        outfile = infile + '.pack'
+    #check if exists the output file
+    if os.path.exists(outfile):
+        if os.getenv('GRASS_OVERWRITE'):
+            grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
+            grass.try_remove(outfile)
+        else:
+            grass.fatal(_("option <output>: <%s> exists.") % outfile)
+    grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
+    basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
+    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
+    sqlitedb = None
+    if not db_vect:
+        grass.message(_('There is not database connected with vector %s') % gfile['fullname'])
+    else:
+        # for each layer connection save a table
+        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'])
+        
+    # 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)
+    gisenv = grass.gisenv()
+    #add to the tar file the PROJ files to check when unpack file
+    for support in ['INFO', 'UNITS']:
+        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
+                            'PERMANENT', 'PROJ_' + support)
+        if os.path.exists(path):
+          tar.add(path,os.path.join(infile,'PROJ_' + support))
+    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.verbose(_("Vector map saved to '%s'" % os.path.join(olddir, outfile)))
+            
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())

+ 7 - 0
scripts/v.unpack/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.unpack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 59 - 0
scripts/v.unpack/test_suite/test.v.unpack.sh

@@ -0,0 +1,59 @@
+# This script tests v.pack and v.unpack
+
+# We specific a small region in the
+# @preprocess step
+# The region setting should work for UTM and LL test locations
+g.region s=0 n=70 w=0 e=100 b=0 t=50 -p
+
+# We need different vector maps, with and without tables 
+# and with multiple layers
+v.random --o -z output=probe_1 n=100 zmin=0 zmax=100 seed=1
+v.random --o -z output=probe_2 n=100 zmin=0 zmax=100 column=height seed=1
+v.random --o -z output=probe_orig n=100 zmin=0 zmax=100 column=height seed=1
+# Adding new layer with categories
+v.category input=probe_orig out=probe_3 option=transfer layer=1,2,3 --o
+
+# Creating new tables for each layer
+db.copy from_table=probe_orig to_table=probe_3_1
+db.copy from_table=probe_orig to_table=probe_3_2
+db.copy from_table=probe_orig to_table=probe_3_3
+
+# Removing un-needed vectors and tables
+g.remove vect=probe_orig
+v.db.droptable -f map=probe_3 table=probe_3 layer=1
+
+# Adding tables to layer
+v.db.addtable --o map=probe_3 table=probe_3_1 layer=1 
+v.db.addtable --o map=probe_3 table=probe_3_2 layer=2 
+v.db.addtable --o map=probe_3 table=probe_3_3 layer=3 
+
+# First we @test the packing/export with v.pack
+v.pack --o input=probe_1
+v.pack --o input=probe_2
+v.pack --o input=probe_3
+
+v.pack --o -c input=probe_1 output=probe_1_uncompressed.pack
+v.pack --o -c input=probe_2 output=probe_2_uncompressed.pack
+v.pack --o -c input=probe_3 output=probe_3_uncompressed.pack
+
+# We need to clean before import
+g.remove vect=probe_1,probe_2,probe_3
+
+# Test the import with v.unpack
+v.unpack --o input=probe_1.pack
+v.category input=probe_1 option=report
+v.unpack --o input=probe_2.pack
+v.category input=probe_2 option=report
+v.unpack --o input=probe_3.pack
+v.category input=probe_3 option=report
+
+# Test the import with v.unpack
+v.unpack --o input=probe_1.pack output=probe_1_uncompressed
+v.category input=probe_1_uncompressed option=report
+v.unpack --o input=probe_2.pack output=probe_2_uncompressed
+v.category input=probe_2_uncompressed option=report
+v.unpack --o input=probe_3.pack output=probe_3_uncompressed
+v.category input=probe_3_uncompressed option=report
+
+g.remove vect=probe_1_uncompressed,probe_2_uncompressed,probe_3_uncompressed
+rm *.pack

+ 36 - 0
scripts/v.unpack/v.unpack.html

@@ -0,0 +1,36 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.unpack</em> allows to unpack vector maps packed by <em><a href="v.pack.html">v.pack</a></em>.
+
+<h2>NOTES</h2>
+
+Name of the vector map is determined by default from pack file
+internals. Optionaly the name can be given by <b>output</b> parameter.
+
+<h2>EXAMPLE</h2>
+
+Pack up vector map <i>random_point</i> into <i>random_point.pack</i> file.
+
+<div class="code"><pre>
+v.pack input=random_point
+</pre></div>
+
+the vector map can be afterwards unpacked by
+
+<div class="code"><pre>
+v.unpack input=random_point.pack
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="v.pack.html">v.pack</a>,
+  <a href="v.in.gdal.html">v.in.gdal</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Luca Delucchi, Fondazione E. Mach (Italy), based on the <em>r.unpack</em> code
+
+<p>
+<i>Last changed: $Date$</i>

+ 177 - 0
scripts/v.unpack/v.unpack.py

@@ -0,0 +1,177 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       v.unpack
+# AUTHOR(S):    Luca Delucchi
+#               
+# PURPOSE:      Unpack up a vector map packed with v.pack
+# COPYRIGHT:    (C) 2004-2008, 2010 by the GRASS Development Team
+#
+#               This program is free software under the GNU General
+#               Public License (>=v2). Read the file COPYING that
+#               comes with GRASS for details.
+#
+#############################################################################
+
+#%module
+#% description: Unpacks a vector map packed with r.pack.
+#% keywords: vector, import, copying
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,file,input
+#% description: Name of input pack file
+#% key_desc: path
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,vector,vector
+#% description: Name for output vector map (default: taken from input file internals)
+#% key_desc: name
+#% required : no
+#%end
+#%flag
+#% key: o
+#% description: Override projection check (use current location's projection)
+#%end
+
+
+import os
+import sys
+import shutil
+import tarfile
+import atexit
+import filecmp
+
+from grass.script import core as grass
+from grass.script import db as grassdb
+
+def cleanup():
+    grass.try_rmdir(tmp_dir)
+
+def main():
+    infile = options['input']
+    # create temporary directory
+    global tmp_dir
+    tmp_dir = grass.tempdir()
+    grass.debug('tmp_dir = %s' % tmp_dir)
+    #check if the file exist
+    if not os.path.exists(infile):
+        grass.fatal(_("File <%s> not found" % infile))
+    #copy the files to tmp dir
+    input_base = os.path.basename(infile)
+    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
+    os.chdir(tmp_dir)
+    tar = tarfile.TarFile.open(name = input_base, mode = 'r')
+    try:
+        data_name = tar.getnames()[0]
+    except:
+        grass.fatal(_("Pack file unreadable"))
+    #set the output name
+    if options['output']:
+        map_name = options['output']
+    else:
+        map_name = data_name
+    # grass env
+    gisenv = grass.gisenv()
+    mset_dir = os.path.join(gisenv['GISDBASE'],
+                            gisenv['LOCATION_NAME'],
+                            gisenv['MAPSET'])
+    
+    new_dir = os.path.join(mset_dir,'vector',map_name)
+
+    gfile = grass.find_file(name = map_name, element = 'vector',
+                            mapset = '.')
+    overwrite = os.getenv('GRASS_OVERWRITE')
+    if gfile['file'] and overwrite != '1':
+        grass.fatal(_("Vector map <%s> already exists") % map_name)
+    elif overwrite == '1' and gfile['file']:
+        grass.warning(_("Vector map <%s> already exists and will be overwritten") % map_name)
+        grass.run_command('g.remove', quiet = True, vect = map_name)
+        shutil.rmtree(new_dir,True)
+    
+    # extract data
+    tar.extractall()
+
+    # check projection compatibility in a rather crappy way
+    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
+    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
+    if not grass.compare_key_value_text_files(os.path.join(data_name,'PROJ_INFO'), loc_proj) or \
+       not grass.compare_key_value_text_files(os.path.join(data_name,'PROJ_UNITS'), loc_proj_units):
+        if flags['o']:
+            grass.warning(_("Projection information does not match. Proceeding..."))
+        else:
+            grass.fatal(_("Projection information does not match. Aborting."))
+
+    #new db
+    fromdb = os.path.join(new_dir, 'db.sqlite')
+    #copy file
+    shutil.copytree(data_name, new_dir)
+    #exist fromdb
+    if os.path.exists(fromdb):
+        #the db connection in the output mapset
+        dbconn = grassdb.db_connection()
+        if dbconn['database'].find('GISDBASE'):
+            dbstr = os.path.sep.join(dbconn['database'].split(os.path.sep)[3:])
+            todb = os.path.join(mset_dir, dbstr)
+        else:
+            todb = dbconn['database']
+        #return all tables
+        list_fromtable = grass.read_command('db.tables',driver='sqlite',database=fromdb)
+        list_fromtable = list_fromtable.split('\n')
+        #return the list of old connection for extract layer number and key
+        dbln = open(os.path.join(new_dir,'dbln'),'r')
+        dbnlist = dbln.readlines()
+        dbln.close()
+        #for each old connection
+        for t in dbnlist:
+            #it split the line of each connection, to found layer number and key
+            if len(t.split('|')) != 1:
+                values = t.split('|')
+            else:
+                values = t.split(' ')
+            
+            from_table = values[1]
+            layer = values[0].split('/')[0]
+            # We need to take care about the table name in case of several layer
+            if options["output"]:
+                to_table = "%s_%s"%(map_name, layer)
+            else:
+                to_table = from_table
+            
+            grass.verbose(_("Copy table %s to table %s"%(from_table, to_table)))
+            
+            #copy the table in the default database
+            ret = grass.run_command('db.copy', to_driver = dbconn['driver'], 
+		      to_database = todb, to_table = to_table, 
+		      from_driver = 'sqlite', from_database = fromdb,
+		      from_table = from_table)
+            if ret != 0:
+                grass.fatal(_("Unable to copy table %s to table %s"%(from_table, to_table)))
+                
+            grass.verbose(_("Connect table %s to vector %s at layer %s"%(to_table, map_name, layer)))
+
+            #and connect the new tables with the right layer
+            ret = grass.run_command('v.db.connect', flags = "o", 
+		      driver = dbconn['driver'], database = todb, 
+		      map =  map_name, key = values[2],
+		      layer = layer, table = to_table)
+            if ret != 0:
+                grass.fatal(_("Unable to connect table %s to vector map %s"%(to_table, map_name)))
+
+    #remove 
+    os.remove(os.path.join(new_dir,'PROJ_INFO'))
+    os.remove(os.path.join(new_dir,'PROJ_UNITS'))
+    if os.path.exists(fromdb):
+        os.remove(os.path.join(new_dir,'db.sqlite'))
+
+    grass.verbose(_("Vector map saved to <%s>") % map_name)
+
+if __name__ == "__main__":
+  options, flags = grass.parser()
+  atexit.register(cleanup)
+  sys.exit(main())