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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 23 10:20:30 EDT 2011


Author: lucadelu
Date: 2011-05-23 07:20:30 -0700 (Mon, 23 May 2011)
New Revision: 46388

Added:
   grass-addons/grass7/vector/v.pack/
   grass-addons/grass7/vector/v.pack/Makefile
   grass-addons/grass7/vector/v.pack/v.pack.html
   grass-addons/grass7/vector/v.pack/v.pack.py
   grass-addons/grass7/vector/v.unpack/
   grass-addons/grass7/vector/v.unpack/Makefile
   grass-addons/grass7/vector/v.unpack/v.unpack.html
   grass-addons/grass7/vector/v.unpack/v.unpack.py
Log:
add v.pack and v.unpack modules based on r.pack and r.unpack

Added: grass-addons/grass7/vector/v.pack/Makefile
===================================================================
--- grass-addons/grass7/vector/v.pack/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.pack/Makefile	2011-05-23 14:20:30 UTC (rev 46388)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.pack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.pack/v.pack.html
===================================================================
--- grass-addons/grass7/vector/v.pack/v.pack.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.pack/v.pack.html	2011-05-23 14:20:30 UTC (rev 46388)
@@ -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
\ No newline at end of file

Added: grass-addons/grass7/vector/v.pack/v.pack.py
===================================================================
--- grass-addons/grass7/vector/v.pack/v.pack.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.pack/v.pack.py	2011-05-23 14:20:30 UTC (rev 46388)
@@ -0,0 +1,93 @@
+#!/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 vecto map and support files for copying.
+#% keywords: vector, export, copying
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% 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
+
+import os
+import sys
+import shutil
+import tarfile
+
+from grass.script import core as grass
+from grass.script import db as grassdb
+
+def main():
+    infile = options['input']
+    if options['output']:
+        outfile = options['output']
+    else:
+        outfile = infile + '.pack'
+        
+    gfile = grass.find_file(infile, element = 'vector')
+    if not gfile['name']:
+        grass.fatal(_("Vector map <%s> not found") % infile)
+
+    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()
+    
+    dbconn = grassdb.db_connection()
+    if dbconn['database'].find('GISDBASE'):
+      dbstr = os.path.sep.join(dbconn['database'].split(os.path.sep)[3:])
+      fromdb = os.path.join(basedir, dbstr)
+
+    sqlitedb = os.path.join(basedir, 'vector', infile, 'db.sqlite')
+    
+    cptable = grass.run_command('db.copy', from_driver = dbconn['driver'], 
+                               from_database = fromdb, from_table =  infile, 
+                               to_driver = 'sqlite', to_database = sqlitedb, 
+                               to_table= infile)
+
+    tar = tarfile.open(outfile, "w:gz")   
+    tar.add(os.path.join(basedir,'vector',infile),infile)
+    gisenv = grass.gisenv()
+    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()
+    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())


Property changes on: grass-addons/grass7/vector/v.pack/v.pack.py
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/grass7/vector/v.unpack/Makefile
===================================================================
--- grass-addons/grass7/vector/v.unpack/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.unpack/Makefile	2011-05-23 14:20:30 UTC (rev 46388)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.unpack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.unpack/v.unpack.html
===================================================================
--- grass-addons/grass7/vector/v.unpack/v.unpack.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.unpack/v.unpack.html	2011-05-23 14:20:30 UTC (rev 46388)
@@ -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: 2010-11-22 09:02:17 +0100 (Mon, 22 Nov 2010) $</i>

Added: grass-addons/grass7/vector/v.unpack/v.unpack.py
===================================================================
--- grass-addons/grass7/vector/v.unpack/v.unpack.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.unpack/v.unpack.py	2011-05-23 14:20:30 UTC (rev 46388)
@@ -0,0 +1,125 @@
+#!/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,cell,raster
+#% 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']
+    
+    global tmp_dir
+    tmp_dir = grass.tempdir()
+    grass.debug('tmp_dir = %s' % tmp_dir)
+    
+    if not os.path.exists(infile):
+        grass.fatal(_("File <%s> not found" % infile))
+    
+    gisenv = grass.gisenv()
+    mset_dir = os.path.join(gisenv['GISDBASE'],
+                            gisenv['LOCATION_NAME'],
+                            gisenv['MAPSET'])
+    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')
+    try:
+        data_name = tar.getnames()[0]
+    except:
+        grass.fatal(_("Pack file unreadable"))
+    
+    if options['output']:
+        map_name = options['output']
+    else:
+        map_name = data_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)
+    
+    # extract data
+    tar.extractall()
+
+    # check projection compatibility in a rather crappy way
+    if not filecmp.cmp(os.path.join(data_name,'PROJ_INFO'), os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')):
+        if flags['o']:
+            grass.warning(_("Projection information does not match. Proceeding..."))
+        else:
+            grass.fatal(_("Projection information does not match. Aborting."))
+
+    vect_dir = os.path.join(mset_dir,'vector')
+    new_dir = os.path.join(vect_dir,data_name)
+    shutil.copytree(data_name, new_dir)
+
+    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)
+
+    cptable = grass.run_command('db.copy', to_driver = dbconn['driver'], 
+                               to_database = todb, to_table =  map_name, 
+                               from_driver = 'sqlite', 
+                               from_database = os.path.join(new_dir, 'db.sqlite'),
+                               from_table= map_name)
+
+    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'))
+
+    import pdb; pdb.set_trace()
+
+    grass.verbose(_("Vector map saved to <%s>") % map_name)
+
+if __name__ == "__main__":
+  options, flags = grass.parser()
+  atexit.register(cleanup)
+  sys.exit(main())



More information about the grass-commit mailing list