[GRASS-SVN] r70703 - grass/trunk/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 28 20:34:48 PST 2017


Author: annakrat
Date: 2017-02-28 20:34:48 -0800 (Tue, 28 Feb 2017)
New Revision: 70703

Modified:
   grass/trunk/lib/python/script/vector.py
Log:
pythonlib vector_what: add support for finding multiple features implemented in v.what (see #3292)

Modified: grass/trunk/lib/python/script/vector.py
===================================================================
--- grass/trunk/lib/python/script/vector.py	2017-02-28 20:13:17 UTC (rev 70702)
+++ grass/trunk/lib/python/script/vector.py	2017-03-01 04:34:48 UTC (rev 70703)
@@ -269,7 +269,7 @@
 orderedDict = None
 
 
-def vector_what(map, coord, distance=0.0, ttype=None, encoding=None, skip_attributes=False, layer=None):
+def vector_what(map, coord, distance=0.0, ttype=None, encoding=None, skip_attributes=False, layer=None, multiple=False):
     """Query vector map at given locations
 
     To query one vector map at one location
@@ -329,6 +329,7 @@
     :param skip_attributes: True to skip quering attributes
     :param layer: layer number or list of layers (one for each vector),
                   if None, all layers (-1) are used
+    :param multiple: find multiple features within threshold distance
 
     :return: parsed list
     """
@@ -360,12 +361,17 @@
         for e, n in coord:
             coord_list.append('%f,%f' % (e, n))
 
-    cmdParams = dict(quiet      = True,
-                     flags      = 'j' if skip_attributes else 'aj',
-                     map        = ','.join(map_list),
-                     layer      = ','.join(layer_list),
-                     coordinates = ','.join(coord_list),
-                     distance   = float(distance))
+    flags = 'j'
+    if not skip_attributes:
+        flags += 'a'
+    if multiple:
+        flags += 'm'
+    cmdParams = dict(quiet=True,
+                     flags=flags,
+                     map=','.join(map_list),
+                     layer=','.join(layer_list),
+                     coordinates=','.join(coord_list),
+                     distance=float(distance))
     if ttype:
         cmdParams['type'] = ','.join(ttype)
 
@@ -406,14 +412,27 @@
     except ValueError:
         raise ScriptError(_("v.what output is not valid JSON format:\n {ret}").format(ret=ret))
 
-    for vmap in result['Maps']:
-        cats = vmap.pop('Categories', None)
-        if cats:
-            for cat in cats:
-                tmp = vmap.copy()
-                tmp.update(cat)
-                data.append(tmp)
-        else:
-            data.append(vmap)
-
+    if multiple:
+        for vmap in result['Maps']:
+            features = vmap.pop('Features', None)
+            if features:
+                for feature in features:
+                    cats = feature.pop('Categories', None)
+                    if cats:
+                        for cat in cats:
+                            tmp = feature.copy()
+                            tmp.update(cat)
+                            tmp2 = vmap.copy()
+                            tmp2.update(tmp)
+                            data.append(tmp2)
+    else:
+        for vmap in result['Maps']:
+            cats = vmap.pop('Categories', None)
+            if cats:
+                for cat in cats:
+                    tmp = vmap.copy()
+                    tmp.update(cat)
+                    data.append(tmp)
+            else:
+                data.append(vmap)
     return data



More information about the grass-commit mailing list