/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/classification.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/classification.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 381 by jonathan, Tue Jan 28 18:37:05 2003 UTC revision 388 by jonathan, Mon Feb 10 15:25:05 2003 UTC
# Line 13  to data. This mapping can be specified i Line 13  to data. This mapping can be specified i
13  First, specific values can be associated with data.  First, specific values can be associated with data.
14  Second, ranges can be associated with data such that if  Second, ranges can be associated with data such that if
15  an input value falls with a range that data is returned.  an input value falls with a range that data is returned.
16  If no mapping can be found then a NullData data will  If no mapping can be found then default data will
17  be returned. Input values must be hashable objects  be returned. Input values must be hashable objects
18    
19  See the description of getProperties() for more information  See the description of getProperties() for more information
20  on the mapping algorithm.  on the mapping algorithm.
21  """  """
22        
23    from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
24         LAYER_VISIBILITY_CHANGED
25    
26  from Thuban import _  from Thuban import _
27  from Thuban.Model.color import Color  from Thuban.Model.color import Color
28    
# Line 32  RANGE_DATA = 2 Line 35  RANGE_DATA = 2
35    
36  class Classification:  class Classification:
37    
38        def __init__(self, layer, field = None):
     def __init__(self, field = None):  
39          """Initialize a classification.          """Initialize a classification.
40    
41               layer -- the layer object who owns this classification
42    
43             field -- the name of the data table field that             field -- the name of the data table field that
44                      is to be used to classify layer properties                      is to be used to classify layer properties
45          """          """
46    
47            self.layer = layer
48          self.points = {}          self.points = {}
49          self.ranges = []          self.ranges = []
50          self.setField(field)          self.DefaultData = ClassData()
51          self.setNull(None)          self.field = field
52                    #self.SetField(field)
53      def setField(self, field):  
54        def SetField(self, field):
55          """Set the name of the data table field to use.          """Set the name of the data table field to use.
56                    
57             field -- if None then all values map to NullData             field -- if None then all values map to the default data
58          """          """
59    
60          self.field = field          self.field = field
61            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
62    
63        def GetField(self):
64            return self.field
65    
66      def setNull(self, data):      def SetDefaultData(self, data):
67          """Set the data to be used when a value can't be classified.          """Set the data to be used when a value can't be classified.
68    
69             data -- data that the value maps to. See class description.             data -- data that the value maps to. See class description.
70          """          """
71    
72          self.NullData = data          self.DefaultData = data
73    
74      def addRange(self, min, max, data):      def GetDefaultData(self):
75            return self.DefaultData
76    
77        def SetDefaultFill(self, fill):
78            self.DefaultData.SetFill(fill)
79            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
80            
81        def GetDefaultFill(self):
82            return self.DefaultData.GetFill()
83            
84        def SetDefaultStroke(self, stroke):
85            self.DefaultData.SetStroke(stroke)
86            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
87            
88        def GetDefaultStroke(self):
89            return self.DefaultData.GetStroke()
90            
91        def SetDefaultStrokeWidth(self, strokeWidth):
92            self.DefaultData.SetStrokeWidth(strokeWidth)
93            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
94            
95        def GetDefaultStrokeWidth(self):
96            return self.DefaultData.GetStrokeWidth()
97            
98        def AddRange(self, min, max, data):
99          """Add a new range to the classification.          """Add a new range to the classification.
100    
101             A range allows a value to be classified if it falls between             A range allows a value to be classified if it falls between
# Line 77  class Classification: Line 111  class Classification:
111          if min >= max:          if min >= max:
112              raise ValueError(_("Range minimum >= maximum!"))              raise ValueError(_("Range minimum >= maximum!"))
113          self.ranges.append([min, max, data])          self.ranges.append([min, max, data])
114            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
115    
116      def addPoint(self, value, data):      def AddPoint(self, value, data):
117          """Associate a single value with data.          """Associate a single value with data.
118    
119             When this value is to be classified data will be returned.             When this value is to be classified data will be returned.
# Line 89  class Classification: Line 124  class Classification:
124          """          """
125    
126          self.points[value] = data          self.points[value] = data
127            self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
128    
129      def getProperties(self, value):      def GetProperties(self, value):
130          """Return the associated data, or the NullData.          """Return the associated data, or the default data.
131    
132             The following search technique is used:             The following search technique is used:
133                 (1) if the field is None, return NullData                 (1) if the field is None, return the default data
134                 (2) check if the value exists as a single value                 (2) check if the value exists as a single value
135                 (3) check if the value falls within a range. Ranges                 (3) check if the value falls within a range. Ranges
136                     are checked in the order they were added to                     are checked in the order they were added to
137                     the classification.                     the classification.
138    
139             value -- the value to classify. If there is no mapping             value -- the value to classify. If there is no mapping,
140                      return the NullData (which may be None)                      or value is None, return the default data
141                        (which may be None)
142          """          """
143    
144          if self.field is not None:          if self.field is not None and value is not None:
145              #              #
146              # first check the discrete values              # first check the discrete values
147              #              #
# Line 119  class Classification: Line 156  class Classification:
156                      return p[RANGE_DATA]                      return p[RANGE_DATA]
157    
158    
159          return self.NullData          return self.DefaultData
160    
161      def TreeInfo(self):      def TreeInfo(self):
162          items = []          items = []
163    
164          #          #
165          # shouldn't print anything if there are no classifications          # XXX: shouldn't print anything if there are no classifications
166          #          #
167    
168                    
# Line 134  class Classification: Line 171  class Classification:
171                  return "None"                  return "None"
172              return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)              return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)
173    
174          if self.NullData is not None:          def build_item(data):
175              i = []              i = []
176              for key, value in self.NullData.items():  
177                  if isinstance(value, Color):              v = data.GetStroke()
178                      i.append((_("%s: %s") % (key, color_string(value)), value))              i.append((_("Stroke: %s") % color_string(v), v))
179                  else:              v = data.GetStrokeWidth()
180                      i.append(_("%s: %s") % (key, value))              i.append((_("Stroke Width: %s") % v))
181              items.append((_("'NULL'"), i))              v = data.GetFill()
182                i.append((_("Fill: %s") % color_string(v), v))
183                return i
184    
185            items.append((_("'DEFAULT'"), build_item(self.DefaultData)))
186    
187          for name, data in self.points.items():          for name, data in self.points.items():
188              i = []              items.append((_("%s") % name, build_item(data)))
             for key, value in data.items():  
                 if isinstance(value, Color):  
                     i.append((_("%s: %s") % (key, color_string(value)), value))  
                 else:  
                     i.append(_("%s: %s") % (key, value))  
             items.append((_("%s") % name, i))  
189    
190          for p in self.ranges:          for p in self.ranges:
             i = []  
191              data = p[RANGE_DATA]              data = p[RANGE_DATA]
192              for key, value in data.items():              items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX])),
193                  if isinstance(value, Color):                           build_item(data))
194                      i.append((_("%s: %s") % (key, color_string(value)), value))  
                 else:  
                     i.append(_("%s: %s") % (key, value))  
             items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX], i)))  
         
195          return (_("Classifications"), items)          return (_("Classifications"), items)
196    
197    
198    class ClassData:
199    
200        def __init__(self):
201            self.stroke = None
202            self.stroke_width = 0
203            self.fill = None
204            self.label = ""
205        
206        def GetStroke(self):
207            return self.stroke
208    
209        def SetStroke(self, stroke):
210            self.stroke = stroke
211    
212        def GetStrokeWidth(self):
213            return self.stroke_width
214    
215        def SetStrokeWidth(self, stroke_width):
216            self.stroke_width = stroke_width
217    
218        def GetFill(self):
219            return self.fill
220    
221        def SetFill(self, fill):
222            self.fill = fill
223    
224        def GetLabel(self):
225            return self.label
226    
227        def SetLabel(self, label):
228            self.label = label
229    

Legend:
Removed from v.381  
changed lines
  Added in v.388

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26