[GRASS-SVN] r48130 - grass/trunk/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 5 09:05:03 EDT 2011


Author: huhabla
Date: 2011-09-05 06:05:03 -0700 (Mon, 05 Sep 2011)
New Revision: 48130

Added:
   grass/trunk/lib/python/raster3d.py
Modified:
   grass/trunk/lib/python/Makefile
   grass/trunk/lib/python/__init__.py
Log:
Raster3d info and mapcalc python functions inspired by the grass.script
python raster package


Modified: grass/trunk/lib/python/Makefile
===================================================================
--- grass/trunk/lib/python/Makefile	2011-09-05 12:56:24 UTC (rev 48129)
+++ grass/trunk/lib/python/Makefile	2011-09-05 13:05:03 UTC (rev 48130)
@@ -8,7 +8,7 @@
 GDIR = $(PYDIR)/grass
 DSTDIR = $(GDIR)/script
 
-MODULES = core db raster vector array setup task
+MODULES = core db raster raster3d vector array setup task
 
 PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
 PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)

Modified: grass/trunk/lib/python/__init__.py
===================================================================
--- grass/trunk/lib/python/__init__.py	2011-09-05 12:56:24 UTC (rev 48129)
+++ grass/trunk/lib/python/__init__.py	2011-09-05 13:05:03 UTC (rev 48130)
@@ -1,4 +1,5 @@
 from core   import *
 from db     import *
 from raster import *
+from raster3d import *
 from vector import *

Added: grass/trunk/lib/python/raster3d.py
===================================================================
--- grass/trunk/lib/python/raster3d.py	                        (rev 0)
+++ grass/trunk/lib/python/raster3d.py	2011-09-05 13:05:03 UTC (rev 48130)
@@ -0,0 +1,86 @@
+"""!@package grass.script.raster3d
+
+ at brief GRASS Python scripting module (raster3d functions)
+
+Raster3d related functions to be used in Python scripts.
+
+Usage:
+
+ at code
+from grass.script import raster3d as grass
+
+grass.raster3d_info(map)
+...
+ at endcode
+
+(C) 2008-2009 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.
+
+ at author Glynn Clements
+ at author Martin Landa <landa.martin gmail.com>
+ at author Soeren Gebbert <soeren.gebbert gmail.com>
+"""
+
+import os
+import string
+
+from core import *
+
+# add raster history
+
+# run "r3.info -rstgip ..." and parse output
+
+def raster3d_info(map):
+    """!Return information about a raster map (interface to
+    `r3.info'). Example:
+
+    \code
+    >>> grass.raster3d_info('volume')
+    {'tiledimz': 1, 'tbres': 1.0, 'tiledimx': 27, 'tiledimy': 16, 'north': 60.490001999999997, 'tilenumy': 1, 'tilenumz': 1, 
+    'min': 1.0, 'datatype': '"DCELL"', 'max': 1.0, 'top': 0.5, 'bottom': -0.5, 'west': -3.2200000000000002, 'tilenumx': 1, 
+    'ewres': 0.98222219, 'east': 23.299999, 'nsres': 0.99937511999999995, 'Timestamp': '"none"', 'south': 44.5}
+    \endcode
+
+    @param map map name
+    
+    @return parsed raster3d info
+    """
+
+    def float_or_null(s):
+        if s == 'NULL':
+            return None
+        else:
+            return float(s)
+
+    s = read_command('r3.info', flags = 'rstgip', map = map)
+    kv = parse_key_val(s)
+    for k in ['min', 'max']:
+	kv[k] = float_or_null(kv[k])
+    for k in ['north', 'south', 'east', 'west', 'top', 'bottom']:
+	kv[k] = float(kv[k])
+    for k in ['nsres', 'ewres', 'tbres']:
+	kv[k] = float_or_dms(kv[k])
+    for k in ['tilenumx', 'tilenumy', 'tilenumz']:
+	kv[k] = int(kv[k])
+    for k in ['tiledimx', 'tiledimy', 'tiledimz']:
+	kv[k] = int(kv[k])
+    return kv
+
+# interface to r3.mapcalc
+
+def mapcalc3d(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
+    """!Interface to r3.mapcalc.
+
+    @param exp expression
+    @param kwargs
+    """
+    t = string.Template(exp)
+    e = t.substitute(**kwargs)
+
+    if run_command('r3.mapcalc', expression = e,
+                   quiet = quiet,
+                   verbose = verbose,
+                   overwrite = overwrite) != 0:
+	fatal(_("An error occurred while running r3.mapcalc"))



More information about the grass-commit mailing list