[GRASS-SVN] r37907 - in grass/trunk/gui/wxpython: gui_modules vdigit

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 16 06:25:10 EDT 2009


Author: martinl
Date: 2009-06-16 06:25:09 -0400 (Tue, 16 Jun 2009)
New Revision: 37907

Modified:
   grass/trunk/gui/wxpython/gui_modules/dbm_dialogs.py
   grass/trunk/gui/wxpython/gui_modules/gselect.py
   grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py
   grass/trunk/gui/wxpython/gui_modules/preferences.py
   grass/trunk/gui/wxpython/gui_modules/vdigit.py
   grass/trunk/gui/wxpython/vdigit/digit.h
   grass/trunk/gui/wxpython/vdigit/line.cpp
Log:
wxGUI: register geometry attributes (currently only line length)


Modified: grass/trunk/gui/wxpython/gui_modules/dbm_dialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/dbm_dialogs.py	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/gui_modules/dbm_dialogs.py	2009-06-16 10:25:09 UTC (rev 37907)
@@ -182,9 +182,9 @@
         sqlCommands = []
         # find updated values for each layer/category
         for layer in self.mapDBInfo.layers.keys(): # for each layer
-            table = self.mapDBInfo.layers[layer]["table"]
-            key = self.mapDBInfo.layers[layer]["key"]
-            columns = self.mapDBInfo.tables[table]
+            table = self.mapDBInfo.GetTable(layer)
+            key = self.mapDBInfo.GetKeyColumn(layer)
+            columns = self.mapDBInfo.GetTableDesc(table)
             for idx in range(len(columns[key]['values'])): # for each category
                 updatedColumns = []
                 updatedValues = []
@@ -247,7 +247,7 @@
 
         return sqlCommands
 
-    def OnReset(self, event):
+    def OnReset(self, event = None):
         """!Reset form"""
         for layer in self.mapDBInfo.layers.keys():
             table = self.mapDBInfo.layers[layer]["table"]
@@ -257,6 +257,8 @@
                 for name in columns.keys():
                     type  = columns[name]['type']
                     value = columns[name]['values'][idx]
+                    if value is None:
+                        value = ''
                     try:
                         id = columns[name]['ids'][idx]
                     except IndexError:
@@ -484,6 +486,20 @@
         
         return True
 
+    def SetColumnValue(self, layer, column, value):
+        """!Set attrbute value
+
+        @param column column name
+        @param value value
+        """
+        table = self.mapDBInfo.GetTable(layer)
+        columns = self.mapDBInfo.GetTableDesc(table)
+        
+        for key, col in columns.iteritems():
+            if key == column:
+                col['values'] = [col['ctype'](value),]
+                break
+        
 class ModifyTableRecord(wx.Dialog):
     """!Dialog for inserting/updating table record"""
     def __init__(self, parent, id, title, data, keyEditable=(-1, True),

Modified: grass/trunk/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gselect.py	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/gui_modules/gselect.py	2009-06-16 10:25:09 UTC (rev 37907)
@@ -407,6 +407,27 @@
         """!Get vector name"""
         return self.map
     
+    def GetKeyColumn(self, layer):
+        """!Get key column of given layer
+        
+        @param layer vector layer number
+        """
+        return self.layers[layer]['key']
+    
+    def GetTable(self, layer):
+        """!Get table name of given layer
+        
+        @param layer vector layer number
+        """
+        return self.layers[layer]['table']
+    
+    def GetTableDesc(self, table):
+        """!Get table columns
+
+        @param table table name
+        """
+        return self.tables[table]
+
 class LayerSelect(wx.Choice):
     """
     Creates combo box for selecting data layers defined for vector.
@@ -540,16 +561,29 @@
         if vector:
             self.InsertColumns(vector, layer)
                 
-    def InsertColumns(self, vector, layer):
-        """!Insert columns for a vector attribute table into the columns combobox"""
+    def InsertColumns(self, vector, layer, excludeKey = False, type = None):
+        """!Insert columns for a vector attribute table into the columns combobox
+
+        @param vector vector name
+        @param layer vector layer number
+        @param excludeKey exclude key column from the list?
+        @param type only columns of given type (given as list)
+        """
         dbInfo = VectorDBInfo(vector)
         
         try:
-            table = dbInfo.layers[int(layer)]['table']
-            columnchoices = dbInfo.tables[table]
+            table = dbInfo.GetTable(int(layer))
+            columnchoices = dbInfo.GetTableDesc(table)
+            keyColumn = dbInfo.GetKeyColumn(int(layer))
             columns = len(columnchoices.keys()) * ['']
             for key, val in columnchoices.iteritems():
                 columns[val['index']] = key
+            if excludeKey: # exclude key column
+                columns.remove(keyColumn)
+            if type: # only selected column types
+                for key, value in columnchoices.iteritems():
+                    if value['type'] not in type:
+                        columns.remove(key)
         except (KeyError, ValueError):
             columns = []
 

Modified: grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py	2009-06-16 10:25:09 UTC (rev 37907)
@@ -1166,6 +1166,17 @@
             self.polycoords.append(self.Pixel2Cell(event.GetPositionTuple()[:]))
             self.DrawLines(pdc=self.pdcTmp)
     
+    def __geomAttrb(self):
+        """!Trac geometry attributes?"""
+        ret = list()
+        for key, val in UserSettings.Get(group = 'vdigit', key = 'geomAttrb',
+                                         internal = True).iteritems():
+            if not val['enabled'] or not val['column']:
+                continue
+            ret.append((key, val['column']))
+
+        return ret
+    
     def __updateATM(self):
         """!Update open Attribute Table Manager
 
@@ -1953,6 +1964,13 @@
                                                                            cats=cats,
                                                                            pos=posWindow,
                                                                            action="add")
+                        layer = int(UserSettings.Get(group='vdigit', key="layer", subkey='value'))
+                        for attrb, column in self.__geomAttrb():
+                            if attrb is 'length':
+                                val = digitClass.GetLineLength(fid)
+                            addRecordDlg.SetColumnValue(layer, column, val)
+                        addRecordDlg.OnReset()
+                        
                         if addRecordDlg.mapDBInfo and \
                                addRecordDlg.ShowModal() == wx.ID_OK:
                             sqlfile = tempfile.NamedTemporaryFile(mode="w")

Modified: grass/trunk/gui/wxpython/gui_modules/preferences.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/preferences.py	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/gui_modules/preferences.py	2009-06-16 10:25:09 UTC (rev 37907)
@@ -6,6 +6,7 @@
 Sets default display font, etc.
 
 Classes:
+ - Settings
  - PreferencesDialog
  - DefaultFontDialog
  - MapsetAccess
@@ -16,7 +17,7 @@
 for details.
 
 @author Michael Barton (Arizona State University)
-Martin Landa <landa.martin gmail.com>
+ at author Martin Landa <landa.martin gmail.com>
 """
 
 import os
@@ -553,6 +554,11 @@
                                                                        _("histogram"))
         self.internalSettings['vdigit']['bgmap'] = {}
         self.internalSettings['vdigit']['bgmap']['value'] = ''
+        self.internalSettings['vdigit']['geomAttrb'] = dict()
+        self.internalSettings['vdigit']['geomAttrb']['length'] = { 'enabled' : False,
+                                                                   'column' : '' }
+        self.internalSettings['vdigit']['geomAttrb']['area']   = { 'enabled' : False,
+                                                                   'column' : '' }
         
     def ReadSettingsFile(self, settings=None):
         """!Reads settings file (mapset, location, gisdbase)"""
@@ -743,8 +749,8 @@
                 else:
                     return settings[group][key]
             else:
-                if type(subkey) == type([]) or \
-                        type(subkey) == type(()):
+                if type(subkey) == type(tuple()) or \
+                        type(subkey) == type(list()):
                     return settings[group][key][subkey[0]][subkey[1]]
                 else:
                     return settings[group][key][subkey]  
@@ -778,7 +784,8 @@
                 else:
                     settings[group][key] = value
             else:
-                if type(subkey) == type([]):
+                if type(subkey) == type(tuple()) or \
+                        type(subkey) == type(list()):
                     settings[group][key][subkey[0]][subkey[1]] = value
                 else:
                     settings[group][key][subkey] = value

Modified: grass/trunk/gui/wxpython/gui_modules/vdigit.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/vdigit.py	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/gui_modules/vdigit.py	2009-06-16 10:25:09 UTC (rev 37907)
@@ -604,6 +604,16 @@
         """
         return dict(self.digit.GetLineCats(line))
 
+    def GetLineLength(self, line):
+        """!Get line length
+
+        @param line feature id
+
+        @return line length
+        @return 0 for non-linear features
+        """
+        return self.digit.GetLineLength(line)
+        
     def SetLineCats(self, line, layer, cats, add=True):
         """!Set categories for given line and layer
 
@@ -1477,6 +1487,59 @@
         border.Add(item=sizer, proportion=0,
                    flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=5)
 
+        #
+        # geometry attributes (currently only length and area are supported)
+        #
+        box   = wx.StaticBox (parent = panel, id = wx.ID_ANY,
+                              label = " %s " % _("Geometry attributes"))
+        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
+        flexSizer = wx.FlexGridSizer(cols=2, hgap=3, vgap=3)
+        flexSizer.AddGrowableCol(0)
+        self.geomAttrb = { 'length' : { 'label' : _('length') },
+                           'area' : { 'label' : _('area') } }
+
+        digitToolbar = self.parent.toolbars['vdigit']
+        try:
+            vectorName = digitToolbar.GetLayer().GetName()
+        except AttributeError:
+            vectorName = None # no vector selected for editing
+        layer = UserSettings.Get(group='vdigit', key="layer", subkey='value')
+        for attrb in self.geomAttrb.keys():
+            # checkbox
+            check = wx.CheckBox(parent = panel, id = wx.ID_ANY,
+                                label = self.geomAttrb[attrb]['label'])
+            ### self.deleteRecord.SetValue(UserSettings.Get(group='vdigit', key="delRecord", subkey='enabled'))
+            check.Bind(wx.EVT_CHECKBOX, self.OnGeomAttrb)
+            # column (only numeric)
+            column = gselect.ColumnSelect(parent = panel)
+            column.InsertColumns(vector = vectorName,
+                                 layer = layer, excludeKey = True,
+                                 type = ['integer', 'double precision'])
+            
+            # default values
+            check.SetValue(UserSettings.Get(group='vdigit', key='geomAttrb',
+                                            subkey=[attrb, 'enabled'],
+                                            internal=True))
+            
+            if not vectorName:
+                check.Enable(False)
+                column.Enable(False)
+            
+            if not check.IsChecked():
+                column.Enable(False)
+            
+            self.geomAttrb[attrb]['check'] = check.GetId()
+            self.geomAttrb[attrb]['column'] = column.GetId()
+            
+            flexSizer.Add(item = check, proportion = 0,
+                          flag = wx.ALIGN_CENTER_VERTICAL)
+            flexSizer.Add(item = column, proportion = 0)
+
+        sizer.Add(item=flexSizer, proportion=1,
+                  flag=wx.ALL | wx.EXPAND, border=1)
+        border.Add(item=sizer, proportion=0,
+                   flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=5)
+
         # bindings
         self.Bind(wx.EVT_CHECKBOX, self.OnChangeAddRecord, self.addRecord)
         self.Bind(wx.EVT_CHOICE, self.OnChangeCategoryMode, self.categoryMode)
@@ -1511,6 +1574,22 @@
             (_("Area (closed boundary + centroid)"), "area"),
             (_("Direction"), "direction"),)
 
+    def OnGeomAttrb(self, event):
+        """!Register geometry attributes (enable/disable)"""
+        checked = event.IsChecked()
+        id = event.GetId()
+        key = None
+        for attrb, val in self.geomAttrb.iteritems():
+            if val['check'] == id:
+                key = attrb
+                break
+        
+        column = self.FindWindowById(self.geomAttrb[key]['column'])
+        if checked:
+            column.Enable()
+        else:
+            column.Enable(False)
+        
     def OnChangeCategoryMode(self, event):
         """!Change category mode"""
 
@@ -1677,6 +1756,17 @@
         UserSettings.Set(group='vdigit', key="delRecord", subkey='enabled',
                          value=self.deleteRecord.IsChecked())
 
+        # geometry attributes
+        for key, val in self.geomAttrb.iteritems():
+            checked = self.FindWindowById(val['check']).IsChecked()
+            column  = self.FindWindowById(val['column']).GetValue()
+            UserSettings.Set(group = 'vdigit', key = 'geomAttrb',
+                             subkey = [key, 'enabled'], value = checked,
+                             internal = True)
+            UserSettings.Set(group = 'vdigit', key = 'geomAttrb',
+                             subkey = [key, 'column'], value = column,
+                             internal = True)
+        
         # snapping threshold
         self.parent.digit.threshold = self.parent.digit.driver.GetThreshold()
 

Modified: grass/trunk/gui/wxpython/vdigit/digit.h
===================================================================
--- grass/trunk/gui/wxpython/vdigit/digit.h	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/vdigit/digit.h	2009-06-16 10:25:09 UTC (rev 37907)
@@ -82,6 +82,8 @@
 					double, double, double, bool,
 					int, int, double);
 
+    double GetLineLength(int);
+    
     int CopyCats(std::vector<int>, std::vector<int>, bool);
     int GetCategory(int);
     std::map<int, std::vector<int> > GetLineCats(int);

Modified: grass/trunk/gui/wxpython/vdigit/line.cpp
===================================================================
--- grass/trunk/gui/wxpython/vdigit/line.cpp	2009-06-16 07:31:07 UTC (rev 37906)
+++ grass/trunk/gui/wxpython/vdigit/line.cpp	2009-06-16 10:25:09 UTC (rev 37907)
@@ -1037,3 +1037,37 @@
 
     return ret;
 }
+
+/*!
+  \brief Get line length
+  
+  \todo LL locations
+
+  \param line feature id
+
+  \return line length
+  \return 0 for non-linear features
+*/
+double Digit::GetLineLength(int line)
+{
+    int type;
+    double length;
+    
+    struct line_pnts *points;
+    
+    if (!Vect_line_alive(display->mapInfo, line))
+	return 0;
+    
+    points = Vect_new_line_struct();
+    
+    type = Vect_read_line(display->mapInfo, points, NULL, line);
+    
+    if (!(type & GV_LINES))
+	length = 0.0;
+    else
+	length = Vect_line_length(points);
+
+    Vect_destroy_line_struct(points);
+
+    return length;
+}



More information about the grass-commit mailing list