Просмотр исходного кода

wx.vnet: basic refactoring (work in progress), added code for turns support (curently hidden until turns backend will be in GRASS)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@56736 15284696-431f-4ddb-bdfa-cd5b030d7da7
Štěpán Turek 12 лет назад
Родитель
Сommit
8ef808fe88

+ 3 - 0
gui/wxpython/vnet/__init__.py

@@ -2,4 +2,7 @@ all = [
     'widgets',
     'dialogs',
     'toolbars',
+    'vnet_core',
+    'vnet_data',
+    'vnet_utils'
     ]

Разница между файлами не показана из-за своего большого размера
+ 724 - 1794
gui/wxpython/vnet/dialogs.py


+ 40 - 19
gui/wxpython/vnet/toolbars.py

@@ -13,6 +13,8 @@ This program is free software under the GNU General Public License
 (>=v2). Read the file COPYING that comes with GRASS for details.
 
 @author Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
+@author Lukas Bocan <silent_bob centrum.cz> (turn costs support)
+@author Eliska Kyzlikova <eliska.kyzlikova gmail.com> (turn costs support)
 """
 
 import wx
@@ -25,9 +27,13 @@ class PointListToolbar(BaseToolbar):
 
     @param parent reference to VNETDialog
     """
-    def __init__(self, parent, list):
+    def __init__(self, parent, dialog, vnet_mgr):
         BaseToolbar.__init__(self, parent)
-        self.list = list
+        self.vnet_mgr = vnet_mgr
+        self.vnet_pts_mgr = self.vnet_mgr.GetPointsManager()
+
+        self.dialog = dialog
+
         self.InitToolbar(self._toolbarData())
         
         # realize the toolbar
@@ -41,6 +47,10 @@ class PointListToolbar(BaseToolbar):
                                     label = _('Insert points from Map Display')),
             'snapping'  : MetaIcon(img = 'move',
                                     label = _('Activate snapping to nodes')),
+            'isec_turn_edit'  : MetaIcon(img = 'line-edit',
+                                    label = _('Activate mode for turns editing')),
+            'global_turn_edit'  : MetaIcon(img = 'vector-tools',
+                                          label = _('Activate mode for global turns editing')),
             'pointAdd'     : MetaIcon(img = 'point-create',
                                     label = _('Add new point')),
             'pointDelete'  : MetaIcon(img = 'gcp-delete',
@@ -48,35 +58,45 @@ class PointListToolbar(BaseToolbar):
             }
 
         return  self._getToolbarData((('insertPoint', icons['insertPoint'],
-                                      self.list.dialog.OnInsertPoint,#TODO self.list.dialog
+                                      self.OnEditPointMode,#TODO self.list.dialog
                                       wx.ITEM_CHECK),
                                       ('snapping', icons['snapping'],
-                                      self.list.dialog.OnSnapping,
+                                      lambda event : self.vnet_mgr.Snapping(event.IsChecked()),
                                       wx.ITEM_CHECK),
                                       (None, ),
                                      ('pointAdd', icons["pointAdd"],
-                                        self.list.AddItem),
+                                        lambda event : self.vnet_pts_mgr.AddPoint()),
                                      ('pointDelete', icons["pointDelete"],
-                                        self.list.DeleteItem)))
+                                       self.OnDeletePoint),
+                                      (None, )#,
+                                      #('isec_turn_edit', icons['isec_turn_edit'],
+                                      #self.dialog.OnDefIsecTurnCosts,
+                                      #wx.ITEM_CHECK),
+                                      #('global_turn_edit', icons['global_turn_edit'],
+                                      #self.dialog.OnDefGlobalTurnCosts)
+                                      ))
                                     
     def GetToolId(self, toolName): #TODO can be useful in base
-
         return vars(self)[toolName]
 
+    def OnEditPointMode(self, event):
+        self.vnet_pts_mgr.EditPointMode(not self.vnet_pts_mgr.IsEditPointModeActive())
+
+    def OnDeletePoint(self, event):
+        pt_id = self.vnet_pts_mgr.GetSelected()
+        self.vnet_pts_mgr.DeletePoint(pt_id)
+
+
 class MainToolbar(BaseToolbar):
     """!Main toolbar
     """
-    def __init__(self, parent):
+    def __init__(self, parent, vnet_mgr):
         BaseToolbar.__init__(self, parent)
         
         self.InitToolbar(self._toolbarData())
 
-        choices = []
-
-        for moduleName in self.parent.vnetModulesOrder:
-            choices.append(self.parent.vnetParams[moduleName]['label'])
+        self.vnet_mgr = vnet_mgr
 
-        self.UpdateUndoRedo()
         # realize the toolbar
         self.Realize()
 
@@ -121,18 +141,18 @@ class MainToolbar(BaseToolbar):
                                     ))
 
 
-    def UpdateUndoRedo(self):
+    def UpdateUndoRedo(self, curr_step, steps_num):
 
         id = vars(self)['showResult']
         self.ToggleTool(id =id,
                         toggle = True)
 
-        if self.parent.history.GetCurrHistStep() >= self.parent.history.GetStepsNum():
+        if curr_step >= steps_num:
            self.Enable("undo", False)
         else:
            self.Enable("undo", True)
 
-        if self.parent.history.GetCurrHistStep() <= 0:
+        if curr_step <= 0:
            self.Enable("redo", False)
         else:
            self.Enable("redo", True)  
@@ -144,15 +164,16 @@ class MainToolbar(BaseToolbar):
 class AnalysisToolbar(BaseToolbar):
     """!Main toolbar
     """
-    def __init__(self, parent):
+    def __init__(self, parent, vnet_mgr):
         BaseToolbar.__init__(self, parent)
         
+        self.vnet_mgr = vnet_mgr
         self.InitToolbar(self._toolbarData())
 
         choices = []
 
-        for moduleName in self.parent.vnetModulesOrder:
-            choices.append(self.parent.vnetParams[moduleName]['label'])
+        for moduleName in self.vnet_mgr.GetAnalyses():
+            choices.append(self.vnet_mgr.GetAnalysisProperties(moduleName)['label'])
 
         self.anChoice = wx.ComboBox(parent = self, id = wx.ID_ANY,
                                     choices = choices,

Разница между файлами не показана из-за своего большого размера
+ 1120 - 0
gui/wxpython/vnet/vnet_core.py


Разница между файлами не показана из-за своего большого размера
+ 1449 - 0
gui/wxpython/vnet/vnet_data.py


+ 153 - 0
gui/wxpython/vnet/vnet_utils.py

@@ -0,0 +1,153 @@
+"""!
+@package vnet.vnet_utils
+
+@brief Vector network analysis utilities.
+
+Classes:
+ - vnet_core::VNETTmpVectMaps
+ - vnet_core::VectMap
+ - vnet_core::History
+
+(C) 2013 by the GRASS Development Team
+
+This program is free software under the GNU General Public License
+(>=v2). Read the file COPYING that comes with GRASS for details.
+
+@author Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
+@author Lukas Bocan <silent_bob centrum.cz> (turn costs support)
+@author Eliska Kyzlikova <eliska.kyzlikova gmail.com> (turn costs support)
+"""
+
+import  math
+from grass.script     import core as grass
+
+try:
+    import grass.lib.vector as vectlib
+    from ctypes import pointer, byref, c_char_p, c_int, c_double, POINTER
+    haveCtypes = True
+except ImportError:
+    haveCtypes = False
+
+def ParseMapStr(mapStr):
+    """!Create full map name (add current mapset if it is not present in name)"""
+    mapValSpl = mapStr.strip().split("@")
+    if len(mapValSpl) > 1:
+        mapSet = mapValSpl[1]
+    else:
+        mapSet = grass.gisenv()['MAPSET']
+    mapName = mapValSpl[0] 
+        
+    return mapName, mapSet
+
+def DegreesToRadians(degrees):
+    return degrees * math.pi / 180
+
+def RadiansToDegrees(radians):
+    return radians * 180 / math.pi   
+
+def SnapToNode(e, n, tresh, vectMap):
+    """!Find nearest node to click coordinates (within given threshold)"""
+    if not haveCtypes:
+        return None
+
+    vectMap, mapSet = ParseMapStr(vectMap)
+
+    openedMap = pointer(vectlib.Map_info())
+    ret = vectlib.Vect_open_old(openedMap, 
+                                     c_char_p(vectMap),
+                                     c_char_p(mapSet))
+    if ret == 1:
+        vectlib.Vect_close(openedMap)
+    if ret != 2: 
+        return None
+
+    nodeNum =  vectlib.Vect_find_node(openedMap,     
+                                      c_double(e), 
+                                      c_double(n), 
+                                      c_double(0), 
+                                      c_double(tresh),
+                                      vectlib.WITHOUT_Z)
+
+    if nodeNum > 0:
+        e = c_double(0)
+        n = c_double(0)
+        vectlib.Vect_get_node_coor(openedMap, 
+                                   nodeNum, 
+                                   byref(e), 
+                                   byref(n), 
+                                   None); # z
+        e = e.value
+        n = n.value
+    else:
+        vectlib.Vect_close(openedMap)
+        return False
+
+    return e, n
+
+def GetNearestNodeCat(e, n, layer, tresh, vectMap):
+
+    if  not haveCtypes:
+        return -2
+
+    vectMapName, mapSet = ParseMapStr(vectMap)
+
+    openedMap = pointer(vectlib.Map_info())
+    ret = vectlib.Vect_open_old(openedMap, 
+                                     c_char_p(vectMapName),
+                                     c_char_p(mapSet))
+    if ret == 1:
+        vectlib.Vect_close(openedMap)
+    if ret != 2: 
+        return -1
+
+    nodeNum = vectlib.Vect_find_node(openedMap,     
+                                     c_double(e), 
+                                     c_double(n), 
+                                     c_double(0), 
+                                     c_double(tresh),
+                                     vectlib.WITHOUT_Z)
+
+    if nodeNum > 0:
+        e = c_double(0)
+        n = c_double(0)
+        vectlib.Vect_get_node_coor(openedMap, 
+                                   nodeNum, 
+                                   byref(e), 
+                                   byref(n), 
+                                   None); # z
+        e = e.value
+        n = n.value
+    else:
+        vectlib.Vect_close(openedMap)
+        return -1
+
+    box = vectlib.bound_box();
+    List = POINTER(vectlib.boxlist);
+    List = vectlib.Vect_new_boxlist(c_int(0));
+
+    box.E = box.W = e;
+    box.N = box.S = n;
+    box.T = box.B = 0;
+    vectlib.Vect_select_lines_by_box(openedMap, byref(box), vectlib.GV_POINT, List);
+
+    found = 0;
+    dcost = 0;
+
+    Cats = POINTER(vectlib.line_cats)
+    Cats = vectlib.Vect_new_cats_struct()
+ 
+    cat = c_int(0)
+
+    for j in range(List.contents.n_values): 
+        line = List.contents.id[j]
+        type = vectlib.Vect_read_line(openedMap, None, Cats, line)
+        if type != vectlib.GV_POINT:
+            continue
+
+        if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)): 
+            found = 1
+            break
+    if found:
+        return cat.value
+
+    return -1

+ 11 - 4
gui/wxpython/vnet/widgets.py

@@ -146,7 +146,7 @@ class PointsList(wx.ListCtrl,
                 info.SetText(col[1][iLabel]) 
                 self.InsertColumnInfo(col[0], info)
 
-    def AddItem(self, event):
+    def AddItem(self):
         """!Appends an item to list with default values"""
         iDefVal = self.dataTypes["itemDefaultValue"]
         iColEd = self.dataTypes["colEditable"]
@@ -192,7 +192,14 @@ class PointsList(wx.ListCtrl,
     def GetCellValue(self, key, colName):
         """!Get value in cell of list using key (same regardless of sorting)"""
         colNum = self._getColumnNum(colName)
-        iColEd = self.dataTypes["colEditable"]      
+
+        if colNum < 0:
+            return None
+
+        iColEd = self.dataTypes["colEditable"]
+        if self.selIdxs[key][colNum] != -1:
+            return self.selIdxs[key][colNum]
+
         return self.itemDataMap[key][colNum]
 
     def GetCellSelIdx(self, key, colName):
@@ -256,7 +263,7 @@ class PointsList(wx.ListCtrl,
         iColEd = self.dataTypes["colEditable"]
         self.colsData[colNum][iColEd] = colType
 
-    def DeleteItem(self, event = None):
+    def DeleteItem(self):
         """!Delete selected item in list"""
         if self.selected == wx.NOT_FOUND:
             return
@@ -373,7 +380,7 @@ class PointsList(wx.ListCtrl,
 
                 self.selIdxs[key] = dlg.GetSelectionIndexes()
         dlg.Destroy()
-        return changed
+        return changed, key
         
     def CreateEditDialog(self, data, pointNo):
         """!Helper function