[GRASS-SVN] r52934 - in grass-addons/grass7/vector: v.pack v.unpack v.unpack/test_suite

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Aug 27 06:53:28 PDT 2012


Author: huhabla
Date: 2012-08-27 06:53:27 -0700 (Mon, 27 Aug 2012)
New Revision: 52934

Added:
   grass-addons/grass7/vector/v.unpack/test_suite/
   grass-addons/grass7/vector/v.unpack/test_suite/test.v.unpack.sh
Modified:
   grass-addons/grass7/vector/v.pack/v.pack.py
   grass-addons/grass7/vector/v.unpack/v.unpack.py
Log:
Added compression option and tests to v.pack and v.unpack.
A new projection comparision method was implemented.


Modified: grass-addons/grass7/vector/v.pack/v.pack.py
===================================================================
--- grass-addons/grass7/vector/v.pack/v.pack.py	2012-08-27 13:17:13 UTC (rev 52933)
+++ grass-addons/grass7/vector/v.pack/v.pack.py	2012-08-27 13:53:27 UTC (rev 52934)
@@ -34,6 +34,10 @@
 #% key_desc: path
 #% required : no
 #%end
+#%flag
+#% key: c
+#% description: Switch the compression off
+#%end
 
 import os
 import sys
@@ -45,6 +49,7 @@
 
 def main():
     infile = options['input']
+    compression_off = flags['c']
     #search if file exist
     gfile = grass.find_file(infile, element = 'vector')
     if not gfile['name']:
@@ -71,8 +76,9 @@
     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'])
+        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():
@@ -81,8 +87,12 @@
                       from_database = dbconn['database'], from_table =  dbconn['table'], 
                       to_driver = 'sqlite', to_database = sqlitedb, 
                       to_table = dbconn['table'])
-    #write tar file
-    tar = tarfile.open(outfile, "w:gz")   
+        
+    # 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
@@ -93,7 +103,7 @@
           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:
+    if db_vect and sqlitedb:
         os.remove(sqlitedb)
     grass.verbose(_("Vector map saved to '%s'" % os.path.join(olddir, outfile)))
             

Added: grass-addons/grass7/vector/v.unpack/test_suite/test.v.unpack.sh
===================================================================
--- grass-addons/grass7/vector/v.unpack/test_suite/test.v.unpack.sh	                        (rev 0)
+++ grass-addons/grass7/vector/v.unpack/test_suite/test.v.unpack.sh	2012-08-27 13:53:27 UTC (rev 52934)
@@ -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

Modified: grass-addons/grass7/vector/v.unpack/v.unpack.py
===================================================================
--- grass-addons/grass7/vector/v.unpack/v.unpack.py	2012-08-27 13:17:13 UTC (rev 52933)
+++ grass-addons/grass7/vector/v.unpack/v.unpack.py	2012-08-27 13:53:27 UTC (rev 52934)
@@ -39,6 +39,7 @@
 #% description: Override projection check (use current location's projection)
 #%end
 
+
 import os
 import sys
 import shutil
@@ -54,7 +55,7 @@
 
 def main():
     infile = options['input']
-    # create tempory directory
+    # create temporary directory
     global tmp_dir
     tmp_dir = grass.tempdir()
     grass.debug('tmp_dir = %s' % tmp_dir)
@@ -65,7 +66,7 @@
     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:gz')
+    tar = tarfile.TarFile.open(name = input_base, mode = 'r')
     try:
         data_name = tar.getnames()[0]
     except:
@@ -98,7 +99,9 @@
 
     # check projection compatibility in a rather crappy way
     loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
-    if not filecmp.cmp(os.path.join(data_name,'PROJ_INFO'), loc_proj):
+    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:
@@ -131,22 +134,40 @@
                 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
-            print "prima di db.copy"
-            grass.run_command('db.copy', to_driver = dbconn['driver'], 
-		      to_database = todb, to_table = map_name, 
+            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 = values[1])
-            #and connect the new tables with the rigth layer
-            grass.run_command('v.db.connect', flags = "o", 
+		      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 = values[0].split('/')[0], table = map_name)
+		      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'))
-    os.remove(os.path.join(new_dir,'db.sqlite'))
+    if os.path.exists(fromdb):
+        os.remove(os.path.join(new_dir,'db.sqlite'))
 
     grass.verbose(_("Vector map saved to <%s>") % map_name)
 



More information about the grass-commit mailing list