[GRASS-SVN] r52855 - in grass/branches/releasebranch_6_4: gui/wxpython/dbmgr gui/wxpython/mapdisp gui/wxpython/nviz lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 23 01:34:31 PDT 2012


Author: annakrat
Date: 2012-08-23 01:34:30 -0700 (Thu, 23 Aug 2012)
New Revision: 52855

Modified:
   grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/dialogs.py
   grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/vinfo.py
   grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/frame.py
   grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/mapwindow.py
   grass/branches/releasebranch_6_4/gui/wxpython/nviz/mapwindow.py
   grass/branches/releasebranch_6_4/lib/python/vector.py
Log:
wxGUI: reorganize querying, if vector has no db connection, show query output in gui commmand output instead of Nothing found message #1606, #1469, show more layers in attribute dialog - #1522 (merge from trunk - r52832, r52834, r52835)

Modified: grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/dialogs.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/dialogs.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/dialogs.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -394,7 +394,7 @@
                 idx = 0
                 for layer in data['Layer']:
                     layer = int(layer)
-                    if 'Id' in data:
+                    if data['Id'][idx] is not None:
                         tfid = int(data['Id'][idx])
                     else:
                         tfid = 0 # Area / Volume

Modified: grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/vinfo.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/vinfo.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/gui/wxpython/dbmgr/vinfo.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -96,28 +96,37 @@
                                  coord = (float(queryCoords[0]), float(queryCoords[1])),
                                  distance = float(qdist))
 
-        if len(data) < 1 or 'Table' not in data[0]:
+        if len(data) < 1 or all(('Table' not in record) for record in data):
             return None
         
         # process attributes
-        table = data[0]['Table']
-        for key, value in data[0]['Attributes'].iteritems():
-            if len(value) < 1:
-                value = None
-            else:
-                if self.tables[table][key]['ctype'] != types.StringType:
-                    value = self.tables[table][key]['ctype'] (value)
-                else:
-                    value = unicodeValue(value)
-            self.tables[table][key]['values'].append(value)
-        
         ret = dict()
-        for key, value in data[0].iteritems():
-            if key == 'Attributes':
+        for key in ['Category', 'Layer', 'Table', 'Id']:
+            ret[key] = list()
+
+        for record in data:
+            if not 'Table' in record:
                 continue
-            ret[key] = list()
-            ret[key].append(value)
-        
+
+            table = record['Table']
+            for key, value in record['Attributes'].iteritems():
+                if len(value) < 1:
+                    value = None
+                else:
+                    if self.tables[table][key]['ctype'] != types.StringType:
+                        value = self.tables[table][key]['ctype'] (value)
+                    else:
+                        value = unicodeValue(value)
+                self.tables[table][key]['values'].append(value)
+            
+            for key, value in record.iteritems():
+                if key == 'Attributes':
+                    continue
+                if key in ret:
+                    ret[key].append(value)
+            if 'Id' not in record.keys():
+                ret['Id'].append(None)
+
         return ret
     
     def SelectFromTable(self, layer, cols = '*', where = None):

Modified: grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/frame.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/frame.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/frame.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -51,7 +51,7 @@
 
 from mapdisp import statusbar as sb
 
-from grass.script import core as grass
+import grass.script as grass
 
 haveCtypes = False
 
@@ -628,54 +628,89 @@
             pgnum = self.layerbook.GetPageIndex(self.page)
             if pgnum > -1:
                 self.layerbook.DeletePage(pgnum)
-        
-    def QueryMap(self, x, y):
-        """!Query raster or vector map layers by r/v.what
-        
+
+    def Query(self, x, y, layers):
+        """!Query selected layers. 
+
+        Calls QueryMap in case of raster or more vectors,
+        or QueryVector in case of one vector with db connection.
+
         @param x,y coordinates
+        @param layers selected tree item layers
         """
+        num = 0
+        filteredLayers = []
+        for layer in layers:
+            ltype = self.tree.GetPyData(layer)[0]['maplayer'].GetType()
+            if ltype in ('raster', 'rgb', 'his',
+                         'vector', 'thememap', 'themechart'):
+                filteredLayers.append(layer)
+
+        if not filteredLayers:
+            GMessage(parent = self,
+                     message = _('No raster or vector map layer selected for querying.'))
+            return
+            
+        layers = filteredLayers
         # set query snap distance for v.what at map unit equivalent of 10 pixels
         qdist = 10.0 * ((self.Map.region['e'] - self.Map.region['w']) / self.Map.width)
         east, north = self.MapWindow.Pixel2Cell((x, y))
+
+        posWindow = self.ClientToScreen((x + self.MapWindow.dialogOffset,
+                                         y + self.MapWindow.dialogOffset))
+
+        isRaster = False
+        nVectors = 0
+        isDbConnection = False
+        for l in layers:
+            maplayer = self.tree.GetPyData(l)[0]['maplayer']
+            if maplayer.GetType() == 'raster':
+                isRaster = True
+                break
+            if maplayer.GetType() == 'vector':
+                nVectors += 1
+                isDbConnection = grass.vector_db(maplayer.GetName())
+
+        if not self.IsPaneShown('3d'):
+            if isRaster or nVectors > 1 or not isDbConnection:
+                self.QueryMap(east, north, qdist, layers)
+            else:
+                self.QueryVector(east, north, qdist, posWindow, layers[0])
+        else:
+            if isRaster:
+                self.MapWindow.QuerySurface(x, y)
+            if nVectors > 1 or not isDbConnection:
+                self.QueryMap(east, north, qdist, layers)
+            elif nVectors == 1:
+                self.QueryVector(east, north, qdist, posWindow, layers[0])
+
+    def QueryMap(self, east, north, qdist, layers):
+        """!Query raster or vector map layers by r/v.what
         
-        if not self.IsStandalone():
-            num = 0
-            for layer in self.tree.GetSelections():
-                ltype = self.tree.GetPyData(layer)[0]['maplayer'].GetType()
-                if ltype in ('raster', 'rgb', 'his',
-                             'vector', 'thememap', 'themechart'):
-                    num += 1
-            
-            if num < 1:
-                GMessage(parent = self,
-                         message = _('No raster or vector map layer selected for querying.'))
-                return
-        
+        @param east,north coordinates
+        @param qdist query distance
+        @param layers selected tree items
+        """
         rast = list()
         vect = list()
         rcmd = ['r.what', '--v']
         vcmd = ['v.what', '--v']
         
-        if self.IsStandalone():
-            pass
-        else:
-            for layer in self.tree.GetSelections():
-                ltype = self.tree.GetPyData(layer)[0]['maplayer'].GetType()
-                dcmd = self.tree.GetPyData(layer)[0]['cmd']
-                name, found = GetLayerNameFromCmd(dcmd)
-                
-                if not found:
-                    continue
-                if ltype == 'raster':
-                    rast.append(name)
-                elif ltype in ('rgb', 'his'):
-                    for iname in name.split('\n'):
-                        rast.append(iname)
-                elif ltype in ('vector', 'thememap', 'themechart'):
-                    vect.append(name)
-        # rasters are not queried this way in 3D, we don't want them now
-        if self.IsPaneShown('3d'):
-            rast = list()
+        for layer in layers:
+            ltype = self.tree.GetPyData(layer)[0]['maplayer'].GetType()
+            dcmd = self.tree.GetPyData(layer)[0]['cmd']
+            name, found = GetLayerNameFromCmd(dcmd)
+            
+            if not found:
+                continue
+            if ltype == 'raster':
+                rast.append(name)
+            elif ltype in ('rgb', 'his'):
+                for iname in name.split('\n'):
+                    rast.append(iname)
+            elif ltype in ('vector', 'thememap', 'themechart'):
+                vect.append(name)
+
         # use display region settings instead of computation region settings
         self.tmpreg = os.getenv("GRASS_REGION")
         os.environ["GRASS_REGION"] = self.Map.SetRegion(windres = False)
@@ -689,7 +724,7 @@
         
         if vect:
             # check for vector maps open to be edited
-            digitToolbar = self.toolbars['vdigit']
+            digitToolbar = self.GetToolbar('vdigit')
             if digitToolbar:
                 lmap = digitToolbar.GetLayer().GetName()
                 for name in vect:
@@ -710,19 +745,14 @@
         Debug.msg(1, "QueryMap(): raster=%s vector=%s" % (','.join(rast),
                                                           ','.join(vect)))
         # parse query command(s)
-        if not self.IsStandalone():
-            if rast:
-                self._layerManager.goutput.RunCmd(rcmd,
-                                                  compReg = False,
-                                                  onDone  =  self._QueryMapDone)
-            if vect:
-                self._layerManager.goutput.RunCmd(vcmd,
-                                                  onDone = self._QueryMapDone)
-        else:
-            if rast:
-                RunCommand(rcmd)
-            if vect:
-                RunCommand(vcmd)
+
+        if rast and not self.IsPaneShown('3d'):
+            self._layerManager.goutput.RunCmd(rcmd,
+                                              compReg = False,
+                                              onDone  =  self._QueryMapDone)
+        if vect:
+            self._layerManager.goutput.RunCmd(vcmd,
+                                              onDone = self._QueryMapDone)
         
     def _QueryMapDone(self, cmd, returncode):
         """!Restore settings after querying (restore GRASS_REGION)
@@ -740,29 +770,15 @@
         if hasattr(self, "tmpreg"):
             del self.tmpreg
         
-    def QueryVector(self, x, y):
+    def QueryVector(self, east, north, qdist, posWindow, layer):
         """!Query vector map layer features
 
         Attribute data of selected vector object are displayed in GUI dialog.
         Data can be modified (On Submit)
         """
-        if not self.tree.layer_selected or \
-                self.tree.GetPyData(self.tree.layer_selected)[0]['type'] != 'vector':
-            GMessage(parent = self,
-                     message = _("No map layer selected for querying."))
-            return
+        mapName = self.tree.GetPyData(layer)[0]['maplayer'].name
         
-        posWindow = self.ClientToScreen((x + self.MapWindow.dialogOffset,
-                                         y + self.MapWindow.dialogOffset))
-        
-        qdist = 10.0 * ((self.Map.region['e'] - self.Map.region['w']) /
-                        self.Map.width)
-        
-        east, north = self.MapWindow.Pixel2Cell((x, y))
-        
-        mapName = self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].name
-        
-        if self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].GetMapset() != \
+        if self.tree.GetPyData(layer)[0]['maplayer'].GetMapset() != \
                 grass.gisenv()['MAPSET']:
             mode = 'display'
         else:
@@ -808,7 +824,7 @@
                     qlayer = self.AddTmpVectorMapLayer(mapName, cats, useId = False)
                 
                 # set opacity based on queried layer
-                opacity = self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].GetOpacity(float = True)
+                opacity = self.tree.GetPyData(layer)[0]['maplayer'].GetOpacity(float = True)
                 qlayer.SetOpacity(opacity)
                 
                 self.MapWindow.UpdateMap(render = False, renderVector = False)

Modified: grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/mapwindow.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/mapwindow.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/gui/wxpython/mapdisp/mapwindow.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -26,7 +26,7 @@
 import grass.script as grass
 
 from gui_core.dialogs   import SavedRegion
-from core.gcmd          import RunCommand, GException, GError
+from core.gcmd          import RunCommand, GException, GError, GMessage
 from core.debug         import Debug
 from core.settings      import UserSettings
 from gui_core.mapwindow import MapWindow
@@ -1110,30 +1110,16 @@
             
         elif self.mouse["use"] == "query":
             # querying
-            layers = self.GetSelectedLayer(multi = True)
-            isRaster = False
-            nVectors = 0
-            for l in layers:
-                if l.GetType() == 'raster':
-                    isRaster = True
-                    break
-                if l.GetType() == 'vector':
-                    nVectors += 1
-            
-            if isRaster or nVectors > 1:
-                self.parent.QueryMap(self.mouse['begin'][0],self.mouse['begin'][1])
-            else:
-                self.parent.QueryVector(self.mouse['begin'][0], self.mouse['begin'][1])
-                # clear temp canvas
-                self.UpdateMap(render = False, renderVector = False)
-            
-        elif self.mouse["use"] == "queryVector":
-            # editable mode for vector map layers
-            self.parent.QueryVector(self.mouse['begin'][0], self.mouse['begin'][1])
-            
-            # clear temp canvas
+            if self.parent.IsStandalone():
+                GMessage(parent = self.parent,
+                         message = _("Querying is not implemented in standalone mode of Map Display"))
+                return
+
+            layers = self.GetSelectedLayer(type = 'item', multi = True)
+
+            self.parent.Query(self.mouse['begin'][0],self.mouse['begin'][1], layers)
             self.UpdateMap(render = False, renderVector = False)
-        
+            
         elif self.mouse["use"] in ["measure", "profile"]:
             # measure or profile
             if self.mouse["use"] == "measure":

Modified: grass/branches/releasebranch_6_4/gui/wxpython/nviz/mapwindow.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/nviz/mapwindow.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/gui/wxpython/nviz/mapwindow.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -763,21 +763,15 @@
         self.mouse['end'] = event.GetPositionTuple()
         if self.mouse["use"] == "query":
             # querying
-            layers = self.GetSelectedLayer(multi = True)
-            isRaster = False
-            nVectors = 0
-            for l in layers:
-                if l.GetType() == 'raster':
-                    isRaster = True
-                    break
-                if l.GetType() == 'vector':
-                    nVectors += 1
-            
-            if isRaster or nVectors > 1:
-                self.OnQueryMap(event)
-            else:
-                self.OnQueryVector(event)
-                    
+            if self.parent.IsStandalone():
+                GMessage(parent = self.parent,
+                         message = _("Querying is not implemented in standalone mode of Map Display"))
+                return
+
+            layers = self.GetSelectedLayer(type = 'item', multi = True)
+
+            self.parent.Query(self.mouse['begin'][0],self.mouse['begin'][1], layers)
+
         elif self.mouse["use"] in ('arrow', 'scalebar'):
             self.lmgr.nviz.FindWindowById(
                     self.lmgr.nviz.win['decoration'][self.mouse["use"]]['place']).SetValue(False)
@@ -998,16 +992,11 @@
         
         self.render['quick'] = False
         self.Refresh(False)
-    
-    def OnQueryMap(self, event):
-        """!Query raster and vector maps"""
-        self.OnQuerySurface(event)
-        self.parent.QueryMap(event.GetX(), event.GetY())
         
-    def OnQuerySurface(self, event):
+    def QuerySurface(self, x, y):
         """!Query surface on given position"""
         size = self.GetClientSizeTuple()
-        result = self._display.QueryMap(event.GetX(), size[1] - event.GetY())
+        result = self._display.QueryMap(x, size[1] - y)
         if result:
             self.qpoints.append((result['x'], result['y'], result['z']))
             self.log.WriteLog("%-30s: %.3f" % (_("Easting"),   result['x']))

Modified: grass/branches/releasebranch_6_4/lib/python/vector.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/vector.py	2012-08-23 08:32:22 UTC (rev 52854)
+++ grass/branches/releasebranch_6_4/lib/python/vector.py	2012-08-23 08:34:30 UTC (rev 52855)
@@ -24,6 +24,7 @@
 
 import os
 import types
+import copy
 import __builtin__
 
 from core import *
@@ -242,8 +243,17 @@
       'Table': 'archsites', 'Type': 'Point', 'Id': 8}]
     @endcode
 
-    To query one vector map at more locations
+    To query one vector map with multiple layers (no additional parameters required)
     @code
+    for q in grass.vector_what(map = 'some_map', coord = (596532.357143,4920486.21429), distance = 100.0):
+        print q['Map'], q['Layer'], q['Attributes']
+
+    new_bug_sites 1 {'str1': 'Beetle_site', 'GRASSRGB': '', 'cat': '80'}
+    new_bug_sites 2 {'cat': '80'}
+    @endcode
+
+    To query more vector maps at one location
+    @code
     for q in grass.vector_what(map = ('archsites', 'roads'), coord = (595743, 4925281),
                                distance = 250):
         print q['Map'], q['Attributes']
@@ -252,7 +262,7 @@
     roads {'label': 'interstate', 'cat': '1'}
     @endcode
 
-    To query more vector maps at one location
+    To query one vector map at more locations
     @code
     for q in grass.vector_what(map = 'archsites', coord = [(595743, 4925281), (597950, 4918898)],
                                distance = 250):
@@ -299,6 +309,9 @@
         return data
     
     dict_attrb = None
+    dict_map = None
+    dict_layer = None
+    attr_pseudo_key = 'Attributes'
     for item in ret.splitlines():
         try:
             key, value = __builtin__.map(lambda x: x.strip(), item.split('=', 1))
@@ -308,21 +321,42 @@
             continue
         
         if key == 'Map':
-            dict_main  = { 'Map' : value }
+            # attach the last one from the previous map
+            if dict_layer is not None:
+                dict_main = copy.copy(dict_map)
+                dict_main.update(dict_layer)
+                data.append(dict_main)
+            dict_map  = { key : value }
+            dict_layer = None
             dict_attrb = None
-            data.append(dict_main)
-            continue
+        elif key == 'Layer':
+            # attach the last the previous Layer
+            if dict_layer is not None:
+                dict_main = copy.copy(dict_map)
+                dict_main.update(dict_layer)
+                data.append(dict_main)
+            dict_layer = { key: int(value) }
+            dict_attrb = None
+        elif key == 'Key_column':
+            dict_layer[key] = value
+            dict_attrb = dict()
+            dict_layer[attr_pseudo_key] = dict_attrb
+        elif dict_attrb is not None:
+            dict_attrb[key] = value
+        elif dict_layer is not None:
+            if key == 'Category':
+                dict_layer[key] = int(value)
+            else:
+                dict_layer[key] = value
         else:
-            if dict_attrb is not None:
-                dict_attrb[key] = value
-            else:
-                if key in ('Category', 'Layer', 'Id'):
-                    dict_main[key] = int(value)
-                else:
-                    dict_main[key] = value
-            if key == 'Key_column':
-                # skip attributes
-                dict_attrb = dict()
-                dict_main['Attributes'] = dict_attrb
+            dict_map[key] = value
+            # TODO: there are some keys which has non-string values
+            # examples: Sq_Meters, Hectares, Acres, Sq_Miles
     
+    # attach the last one
+    if dict_layer is not None:
+        dict_main = copy.copy(dict_map)
+        dict_main.update(dict_layer)
+        data.append(dict_main)
+    
     return data



More information about the grass-commit mailing list