[GRASS-SVN] r44363 - in grass/trunk/scripts: . r.pack r.unpack

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 22 03:02:17 EST 2010


Author: martinl
Date: 2010-11-22 00:02:17 -0800 (Mon, 22 Nov 2010)
New Revision: 44363

Added:
   grass/trunk/scripts/r.pack/
   grass/trunk/scripts/r.pack/Makefile
   grass/trunk/scripts/r.pack/r.pack.html
   grass/trunk/scripts/r.pack/r.pack.py
   grass/trunk/scripts/r.unpack/
   grass/trunk/scripts/r.unpack/Makefile
   grass/trunk/scripts/r.unpack/r.unpack.html
   grass/trunk/scripts/r.unpack/r.unpack.py
Modified:
   grass/trunk/scripts/Makefile
Log:
AddOns Bash scripts r.pack and r.unpack converted to Python and added to trunk


Modified: grass/trunk/scripts/Makefile
===================================================================
--- grass/trunk/scripts/Makefile	2010-11-21 23:42:05 UTC (rev 44362)
+++ grass/trunk/scripts/Makefile	2010-11-22 08:02:17 UTC (rev 44363)
@@ -32,10 +32,12 @@
 	r.in.wms \
 	r.mask \
 	r.out.xyz \
+	r.pack \
 	r.plane \
 	r.reclass.area \
 	r.shaded.relief \
 	r.tileset \
+	r.unpack \
 	v.build.all \
 	v.centroids \
 	v.colors \

Added: grass/trunk/scripts/r.pack/Makefile
===================================================================
--- grass/trunk/scripts/r.pack/Makefile	                        (rev 0)
+++ grass/trunk/scripts/r.pack/Makefile	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.pack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass/trunk/scripts/r.pack/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass/trunk/scripts/r.pack/r.pack.html
===================================================================
--- grass/trunk/scripts/r.pack/r.pack.html	                        (rev 0)
+++ grass/trunk/scripts/r.pack/r.pack.html	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,43 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.pack</em> collects raster 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="r.unpack.html">r.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.
+
+Currently only 2D raster maps are supported.
+
+<h2>EXAMPLE</h2>
+
+Pack up raster map <i>aspect</i> into <i>aspect.pack</i> file.
+
+<div class="code"><pre>
+r.pack input=aspect
+</pre></div>
+
+the raster map can be afterwards unpacked by
+
+<div class="code"><pre>
+r.unpack input=aspect.pack
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="r.unpack.html">r.unpack</a>,
+  <a href="r.out.gdal.html">r.in.gdal</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Original Bash script written by Hamish Bowman, Otago University, New Zealand as GRASS AddOns
+<br>
+Converted to Python and updated for GRASS 7 by Martin Landa, CTU in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>


Property changes on: grass/trunk/scripts/r.pack/r.pack.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/trunk/scripts/r.pack/r.pack.py
===================================================================
--- grass/trunk/scripts/r.pack/r.pack.py	                        (rev 0)
+++ grass/trunk/scripts/r.pack/r.pack.py	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:	r.pack
+# AUTHOR(S):	Hamish Bowman, Otago University, New Zealand
+#               Converted to Python by Martin Landa <landa.martin gmail.com>
+# PURPOSE:	Pack up a raster map, collect raster map elements => gzip
+# 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: Packs up a raster map and support files for copying.
+#% keywords: raster, export, copying
+#%end
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Name of raster 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 atexit
+import tarfile
+
+from grass.script import core as grass
+
+def cleanup():
+    grass.try_rmdir(tmp)
+
+def main():
+    infile = options['input']
+    if options['output']:
+        outfile = options['output']
+    else:
+        outfile = infile + '.pack'
+    
+    global tmp
+    tmp = grass.tempdir()
+    tmp_dir = os.path.join(tmp, infile)
+    os.mkdir(tmp_dir)
+    grass.debug('tmp_dir = %s' % tmp_dir)
+    
+    gfile = grass.find_file(name = infile, element = 'cell')
+    if not gfile['name']:
+        grass.fatal(_("Raster 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()
+    
+    # copy elements
+    for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
+        path = os.path.join(basedir, element, infile)
+        if os.path.exists(path):
+            grass.debug('copying %s' % path)
+            shutil.copyfile(path,
+                            os.path.join(tmp_dir, element))
+            
+    if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
+        shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
+                        os.path.join(tmp_dir, 'cell_misc'))
+        
+    if not os.listdir(tmp_dir):
+        grass.fatal(_("No raster map components found"))
+                    
+    # copy projection info
+    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
+    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):
+            shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
+    
+    # pack it all up
+    os.chdir(tmp)
+    tar = tarfile.TarFile.open(name = outfile, mode = 'w:gz')
+    tar.add(infile, recursive = True)
+    tar.close()
+    try:
+        shutil.move(outfile, olddir)
+    except shutil.Error, e:
+        grass.fatal(e)
+        
+    os.chdir(olddir)
+    
+    grass.verbose(_("Raster map saved to '%s'" % \
+                        os.path.join(olddir, outfile)))
+    
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())


Property changes on: grass/trunk/scripts/r.pack/r.pack.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Added: grass/trunk/scripts/r.unpack/Makefile
===================================================================
--- grass/trunk/scripts/r.unpack/Makefile	                        (rev 0)
+++ grass/trunk/scripts/r.unpack/Makefile	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.unpack
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass/trunk/scripts/r.unpack/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass/trunk/scripts/r.unpack/r.unpack.html
===================================================================
--- grass/trunk/scripts/r.unpack/r.unpack.html	                        (rev 0)
+++ grass/trunk/scripts/r.unpack/r.unpack.html	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,40 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.unpack</em> allows to unpack raster maps packed by <em><a href="r.pack.html">r.pack</a></em>.
+
+<h2>NOTES</h2>
+
+Name of the raster map is determined by default from pack file
+internals. Optionaly the name can be given by <b>output</b> parameter.
+
+Currently only 2D raster maps are supported.
+
+<h2>EXAMPLE</h2>
+
+Pack up raster map <i>aspect</i> into <i>aspect.pack</i> file.
+
+<div class="code"><pre>
+r.pack input=aspect
+</pre></div>
+
+the raster map can be afterwards unpacked by
+
+<div class="code"><pre>
+r.unpack input=aspect.pack
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="r.pack.html">r.pack</a>,
+  <a href="r.in.gdal.html">r.in.gdal</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Original Bash script written by Hamish Bowman, Otago University, New Zealand as GRASS AddOns
+<br>
+Converted to Python and updated for GRASS 7 by Martin Landa, CTU in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>


Property changes on: grass/trunk/scripts/r.unpack/r.unpack.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/trunk/scripts/r.unpack/r.unpack.py
===================================================================
--- grass/trunk/scripts/r.unpack/r.unpack.py	                        (rev 0)
+++ grass/trunk/scripts/r.unpack/r.unpack.py	2010-11-22 08:02:17 UTC (rev 44363)
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:	r.pack
+# AUTHOR(S):	Hamish Bowman, Otago University, New Zealand
+#               Converted to Python by Martin Landa <landa.martin gmail.com>
+# PURPOSE:	Unpack up a raster map packed with r.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 raster map packed with r.pack.
+#% keywords: raster, 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 raster 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
+
+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 = 'cell',
+                            mapset = '.')
+    overwrite = os.getenv('GRASS_OVERWRITE')
+    if gfile['file'] and overwrite != '1':
+        grass.fatal(_("Raster map <%s> already exists") % map_name)
+    
+    # extract data
+    tar.extractall()
+    os.chdir(data_name)
+    
+    # check projection compatibility in a rather crappy way
+    if not filecmp.cmp('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."))
+    
+    # install in $MAPSET
+    for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
+        if not os.path.exists(element):
+            continue
+        path = os.path.join(mset_dir, element)
+        if not os.path.exists(path):
+            os.mkdir(path)
+        if element == 'cell_misc':
+            path = os.path.join(mset_dir, element, map_name)
+            if os.path.exists(path):
+                shutil.rmtree(path)
+            shutil.copytree('cell_misc', path)
+        else:
+            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
+    
+    grass.verbose(_("Raster map saved to <%s>") % map_name)
+    
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())


Property changes on: grass/trunk/scripts/r.unpack/r.unpack.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list