浏览代码

Raster3d info and mapcalc python functions inspired by the grass.script
python raster package


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@48130 15284696-431f-4ddb-bdfa-cd5b030d7da7

Soeren Gebbert 13 年之前
父节点
当前提交
a16017a48d
共有 3 个文件被更改,包括 88 次插入1 次删除
  1. 1 1
      lib/python/Makefile
  2. 1 0
      lib/python/__init__.py
  3. 86 0
      lib/python/raster3d.py

+ 1 - 1
lib/python/Makefile

@@ -8,7 +8,7 @@ PYDIR = $(ETC)/python
 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__)

+ 1 - 0
lib/python/__init__.py

@@ -1,4 +1,5 @@
 from core   import *
 from db     import *
 from raster import *
+from raster3d import *
 from vector import *

+ 86 - 0
lib/python/raster3d.py

@@ -0,0 +1,86 @@
+"""!@package grass.script.raster3d
+
+@brief GRASS Python scripting module (raster3d functions)
+
+Raster3d related functions to be used in Python scripts.
+
+Usage:
+
+@code
+from grass.script import raster3d as grass
+
+grass.raster3d_info(map)
+...
+@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.
+
+@author Glynn Clements
+@author Martin Landa <landa.martin gmail.com>
+@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"))