[GRASS-SVN] r37514 - grass/branches/develbranch_6/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 26 12:58:03 EDT 2009


Author: martinl
Date: 2009-05-26 12:58:03 -0400 (Tue, 26 May 2009)
New Revision: 37514

Modified:
   grass/branches/develbranch_6/lib/python/grasspythonlib.dox
   grass/branches/develbranch_6/lib/python/vector.py
Log:
grass.script.vector: vector_db_select() added
		     (merge from trunk, r37511)


Modified: grass/branches/develbranch_6/lib/python/grasspythonlib.dox
===================================================================
--- grass/branches/develbranch_6/lib/python/grasspythonlib.dox	2009-05-26 16:57:52 UTC (rev 37513)
+++ grass/branches/develbranch_6/lib/python/grasspythonlib.dox	2009-05-26 16:58:03 UTC (rev 37514)
@@ -227,6 +227,8 @@
 
  - vector_db()
 
+ - vector_db_select()
+
  - vector_history()
 
  - vector_info_topo()

Modified: grass/branches/develbranch_6/lib/python/vector.py
===================================================================
--- grass/branches/develbranch_6/lib/python/vector.py	2009-05-26 16:57:52 UTC (rev 37513)
+++ grass/branches/develbranch_6/lib/python/vector.py	2009-05-26 16:58:03 UTC (rev 37514)
@@ -123,3 +123,58 @@
     """
     s = read_command('v.info', flags = 't', map = map)
     return parse_key_val(s, val_type = int)
+
+# interface for v.db.select
+
+def vector_db_select(map, layer = 1, **kwargs):
+    """Get attribute data of selected vector map layer.
+
+    Function returns list of columns and dictionary of values ordered by
+    key column value. Example:
+
+    \code
+    >>> print grass.vector_select('lakes')['values'][3]
+    ['3', '19512.86146', '708.44683', '4', '55652', 'LAKE/POND', '39000', '']
+    \endcode
+
+    @param map map name
+    @param layer layer number
+    @param kwargs v.db.select options
+
+    @return dictionary ('columns' and 'values')
+    """
+    try:
+        key = vector_db(map = map)[layer]['key']
+    except KeyError:
+        error('Missing layer %d in vector map <%s>' % (layer, map))
+        return { 'columns' : [], 'values' : {} }
+        
+    if kwargs.has_key('columns'):
+        if key not in kwargs['columns'].split(','):
+            # add key column if missing
+            info("Adding key column to the output")
+            kwargs['columns'] += ',' + key
+    
+    ret = read_command('v.db.select',
+                       map = map,
+                       layer = layer,
+                       fs = '|', **kwargs)
+    
+    if not ret:
+        error('vector_select() failed')
+        return { 'columns' : [], 'values' : {} }
+    
+    columns = []
+    values = {}
+    for line in ret.splitlines():
+        if not columns:
+            columns = line.split('|')
+            key_index = columns.index(key)
+            continue
+        
+        value = line.split('|')
+        key_value = int(value[key_index])
+        values[key_value] = line.split('|')
+    
+    return { 'columns' : columns,
+             'values' : values }



More information about the grass-commit mailing list