Przeglądaj źródła

grass.py: vector_info_topo() added

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@36554 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 16 lat temu
rodzic
commit
e0a5e83344

+ 12 - 2
lib/python/grass.py

@@ -216,7 +216,7 @@ def tempfile():
 
 
 # key-value parsers
 # 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
     """Parse a string into a dictionary, where entries are separated
     by newlines and the key and value are separated by `sep' (default: `=')
     by newlines and the key and value are separated by `sep' (default: `=')
     """
     """
@@ -228,7 +228,10 @@ def parse_key_val(s, sep = '=', dflt = None):
 	    v = kv[1]
 	    v = kv[1]
 	else:
 	else:
 	    v = dflt
 	    v = dflt
-	result[k] = v
+        if val_type:
+            result[k] = val_type(v)
+        else:
+            result[k] = v
     return result
     return result
 
 
 # interface to g.gisenv
 # interface to g.gisenv
@@ -542,6 +545,13 @@ def vector_history(map):
     """
     """
     run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
     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
 # add raster history
 
 
 def raster_history(map):
 def raster_history(map):

+ 1 - 7
scripts/r.fillnulls/r.fillnulls.py

@@ -123,13 +123,7 @@ def main():
 	grass.fatal("abandoned. Removing temporary maps, restoring user mask if needed:")
 	grass.fatal("abandoned. Removing temporary maps, restoring user mask if needed:")
 
 
     # count number of points to control segmax parameter for interpolation:
     # 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)
     grass.message("Interpolating %d points" % pointsnumber)
 
 

+ 1 - 7
scripts/v.centroids/v.centroids.py

@@ -78,13 +78,7 @@ def main():
 	num_bound = 0
 	num_bound = 0
 	tenv = os.environ.copy()
 	tenv = os.environ.copy()
 	tenv['LC_ALL'] = 'C'
 	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:
 	if num_bound == 0:
 	    grass.fatal("Input vector map contains no boundaries.")
 	    grass.fatal("Input vector map contains no boundaries.")