Browse Source

libpy: vector_columns() - get dictionary / list

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39902 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 15 years ago
parent
commit
d1b1c3961c
1 changed files with 29 additions and 11 deletions
  1. 29 11
      lib/python/vector.py

+ 29 - 11
lib/python/vector.py

@@ -88,22 +88,40 @@ def vector_layer_db(map, layer):
 
 
 # run "v.info -c ..." and parse output
 # run "v.info -c ..." and parse output
 
 
-def vector_columns(map, layer = None, **args):
-    """!Return a dictionary of the columns for the database table connected to
-    a vector map (interface to `v.info -c').
-
+def vector_columns(map, layer = None, getDict = True, **args):
+    """!Return a dictionary (or a list) of the columns for the
+    database table connected to a vector map (interface to `v.info
+    -c').
+
+    @code
+    >>> vector_columns(urbanarea, getDict = True)
+    {'UA_TYPE': {'index': 4, 'type': 'CHARACTER'}, 'UA': {'index': 2, 'type': 'CHARACTER'}, 'NAME': {'index': 3, 'type': 'CHARACTER'}, 'OBJECTID': {'index': 1, 'type': 'INTEGER'}, 'cat': {'index': 0, 'type': 'INTEGER'}}
+
+    >>> vector_columns(urbanarea, getDict = False)
+    ['cat', 'OBJECTID', 'UA', 'NAME', 'UA_TYPE']
+    @endcode
+    
     @param map map name
     @param map map name
-    @param layer layer number (None for all layers)
-    @param args
+    @param layer layer number or name (None for all layers)
+    @param getDict True to return dictionary of columns otherwise list of column names is returned
+    @param args (v.info's arguments)
     
     
-    @return parsed output
+    @return dictionary/list of columns
     """
     """
     s = read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True, **args)
     s = read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True, **args)
-    result = {}
+    if getDict:
+        result = dict()
+    else:
+        result = list()
+    i = 0
     for line in s.splitlines():
     for line in s.splitlines():
-	f = line.split('|')
-	if len(f) == 2:
-            result[f[1]] = f[0]
+	ctype, cname = line.split('|')
+        if getDict:
+            result[cname] = { 'type' : ctype,
+                              'index' : i }
+        else:
+            result.append(cname)
+        i+=1
     
     
     return result
     return result