[GRASS-SVN] r36554 - in grass/trunk: lib/python scripts/r.fillnulls scripts/v.centroids

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 31 17:40:33 EDT 2009


Author: martinl
Date: 2009-03-31 17:40:33 -0400 (Tue, 31 Mar 2009)
New Revision: 36554

Modified:
   grass/trunk/lib/python/grass.py
   grass/trunk/scripts/r.fillnulls/r.fillnulls.py
   grass/trunk/scripts/v.centroids/v.centroids.py
Log:
grass.py: vector_info_topo() added


Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py	2009-03-31 21:26:43 UTC (rev 36553)
+++ grass/trunk/lib/python/grass.py	2009-03-31 21:40:33 UTC (rev 36554)
@@ -216,7 +216,7 @@
 
 # key-value parsers
 
-def parse_key_val(s, sep = '=', dflt = None):
+def parse_key_val(s, sep = '=', dflt = None, val_type = None):
     """Parse a string into a dictionary, where entries are separated
     by newlines and the key and value are separated by `sep' (default: `=')
     """
@@ -228,7 +228,10 @@
 	    v = kv[1]
 	else:
 	    v = dflt
-	result[k] = v
+        if val_type:
+            result[k] = val_type(v)
+        else:
+            result[k] = v
     return result
 
 # interface to g.gisenv
@@ -542,6 +545,13 @@
     """
     run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
 
+# run "v.info -t" and parse output
+
+def vector_info_topo(map):
+    """Return information about a vector map (interface to `v.info -t')."""
+    s = read_command('v.info', flags = 't', map = map)
+    return parse_key_val(s, val_type = int)
+    
 # add raster history
 
 def raster_history(map):

Modified: grass/trunk/scripts/r.fillnulls/r.fillnulls.py
===================================================================
--- grass/trunk/scripts/r.fillnulls/r.fillnulls.py	2009-03-31 21:26:43 UTC (rev 36553)
+++ grass/trunk/scripts/r.fillnulls/r.fillnulls.py	2009-03-31 21:40:33 UTC (rev 36554)
@@ -123,13 +123,7 @@
 	grass.fatal("abandoned. Removing temporary maps, restoring user mask if needed:")
 
     # count number of points to control segmax parameter for interpolation:
-    s = grass.read_command('v.info -t', map = vecttmp)
-    for l in s.splitlines():
-	if "points" in l:
-	    s = l.split('=')[1].strip()
-	    s = s.split()[0].strip()
-	    pointsnumber = int(s)
-	    break
+    pointsnumber = grass.vector_info_topo(map = vecttmp)['points']
 
     grass.message("Interpolating %d points" % pointsnumber)
 

Modified: grass/trunk/scripts/v.centroids/v.centroids.py
===================================================================
--- grass/trunk/scripts/v.centroids/v.centroids.py	2009-03-31 21:26:43 UTC (rev 36553)
+++ grass/trunk/scripts/v.centroids/v.centroids.py	2009-03-31 21:40:33 UTC (rev 36554)
@@ -78,13 +78,7 @@
 	num_bound = 0
 	tenv = os.environ.copy()
 	tenv['LC_ALL'] = 'C'
-	lines = grass.read_command("v.info -t", map = options['input'], env = tenv).splitlines()
-	e = re.compile("boundaries= +([0-9]+) +")
-	for line in lines:
-	    mo = e.search(line)
-	    if mo:
-		num_bound = int(mo.group(1))
-		break
+        num_bound = grass.vector_info_topo(map = options['input'])['boundaries']
 	if num_bound == 0:
 	    grass.fatal("Input vector map contains no boundaries.")
 



More information about the grass-commit mailing list