/[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 427 by jonathan, Wed Feb 19 16:51:12 2003 UTC revision 428 by jonathan, Mon Feb 24 18:46:35 2003 UTC
# Line 16  an input value falls with a range that d Line 16  an input value falls with a range that d
16  If no mapping can be found then default 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 GetClassData() for more information
20  on the mapping algorithm.  on the mapping algorithm.
21  """  """
22        
# Line 55  class Classification: Line 55  class Classification:
55          self.field = field          self.field = field
56          #self.SetField(field)          #self.SetField(field)
57    
58      def SendMessage(self, message):      def __iter__(self):
59            return ClassIterator(self.DefaultData,
60                                 self.points.values(),
61                                 self.ranges,
62                                 self.maps)
63    
64        def __SendMessage(self, message):
65          if self.layer is not None:          if self.layer is not None:
66              self.layer.changed(message, self.layer)              self.layer.changed(message, self.layer)
67            
# Line 66  class Classification: Line 72  class Classification:
72          """          """
73    
74          self.field = field          self.field = field
75          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
76    
77      def GetField(self):      def GetField(self):
78          return self.field          return self.field
79    
80      def SetLayer(self, layer):      def SetLayer(self, layer):
81          self.layer = layer          self.layer = layer
82          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
83    
84      def GetLayer(self):      def GetLayer(self):
85          return layer.self          return layer.self
# Line 84  class Classification: Line 90  class Classification:
90             data -- data that the value maps to. See class description.             data -- data that the value maps to. See class description.
91          """          """
92    
93            assert(data.GetType() == ClassData.DEFAULT)
94          self.DefaultData = data          self.DefaultData = data
95    
96      def GetDefaultData(self):      def GetDefaultData(self):
97          return self.DefaultData          return self.DefaultData
98    
99        #
100        # these SetDefault* methods are really only provided for
101        # some backward compatibility. they should be considered
102        # for removal once all the classification code is finished.
103        #
104    
105      def SetDefaultFill(self, fill):      def SetDefaultFill(self, fill):
106          self.DefaultData.SetFill(fill)          self.DefaultData.SetFill(fill)
107          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
108                    
109      def GetDefaultFill(self):      def GetDefaultFill(self):
110          return self.DefaultData.GetFill()          return self.DefaultData.GetFill()
111                    
112      def SetDefaultStroke(self, stroke):      def SetDefaultStroke(self, stroke):
113          self.DefaultData.SetStroke(stroke)          self.DefaultData.SetStroke(stroke)
114          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
115                    
116      def GetDefaultStroke(self):      def GetDefaultStroke(self):
117          return self.DefaultData.GetStroke()          return self.DefaultData.GetStroke()
118                    
119      def SetDefaultStrokeWidth(self, strokeWidth):      def SetDefaultStrokeWidth(self, strokeWidth):
120          self.DefaultData.SetStrokeWidth(strokeWidth)          self.DefaultData.SetStrokeWidth(strokeWidth)
121          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
122                    
123      def GetDefaultStrokeWidth(self):      def GetDefaultStrokeWidth(self):
124          return self.DefaultData.GetStrokeWidth()          return self.DefaultData.GetStrokeWidth()
# Line 124  class Classification: Line 137  class Classification:
137          else:          else:
138              raise ValueError(_("Unrecognized ClassData type %s") % type)              raise ValueError(_("Unrecognized ClassData type %s") % type)
139    
140          self.SendMessage(LAYER_LEGEND_CHANGED)          self.__SendMessage(LAYER_LEGEND_CHANGED)
141    
142      def GetProperties(self, value):      def GetClassData(self, value):
143          """Return the associated data, or the default data.          """Return the associated data, or the default data.
144    
145             The following search technique is used:             The following search technique is used:
# Line 137  class Classification: Line 150  class Classification:
150                     the classification.                     the classification.
151    
152             value -- the value to classify. If there is no mapping,             value -- the value to classify. If there is no mapping,
153                      or value is None, return the default data                      or value is None, return the default properties
                     (which may be None)  
154          """          """
155    
156          if self.field is not None and value is not None:          if self.field is not None and value is not None:
# Line 192  class Classification: Line 204  class Classification:
204              i.append(build_color_item(_("Fill"), v))              i.append(build_color_item(_("Fill"), v))
205              return (label, i)              return (label, i)
206    
207          items.append(build_item(self.DefaultData, _("'DEFAULT'")))          for p in self:
208                type = p.GetType()
209                if type == ClassData.DEFAULT:
210                    items.append(build_item(self.DefaultData, _("'DEFAULT'")))
211                elif type == ClassData.POINT:
212                    items.append(build_item(p, str(p.GetValue())))
213                elif type == ClassData.RANGE:
214                    items.append(build_item(p, "%s - %s" %
215                                               (p.GetMin(), p.GetMax())))
216    
217          for p in self.points.values():  #       for p in self.points.values():
218              items.append(build_item(p, str(p.GetValue())))  #           items.append(build_item(p, str(p.GetValue())))
219    
220          for p in self.ranges:  #       for p in self.ranges:
221              items.append(build_item(p, "%s - %s" % (p.GetMin(), p.GetMax())))  #           items.append(build_item(p, "%s - %s" % (p.GetMin(), p.GetMax())))
222    
223          return (_("Classifications"), items)          return (_("Classifications"), items)
224    
225    class ClassIterator:
226    
227        def __init__(self, default, points, ranges, maps):
228            self.data = [default, points, ranges, maps]
229            self.data_iter = iter(self.data)
230            self.iter = None
231    
232        def __iter__(self):
233            return self
234    
235        def next(self):
236            if self.iter is None:
237                try:
238                    self.data_item = self.data_iter.next()
239                    self.iter = iter(self.data_item)
240                except TypeError:
241                    return self.data_item
242    
243            try:
244                return self.iter.next()
245            except StopIteration:
246                self.iter = None
247                return self.next()
248          
249  class ClassData:  class ClassData:
250    
251      INVALID = -1      INVALID = -1
# Line 211  class ClassData: Line 254  class ClassData:
254      RANGE = 2      RANGE = 2
255      MAP   = 3      MAP   = 3
256    
257      def __init__(self, type = INVALID, classData = None):      def __init__(self, classData = None, type = INVALID):
258    
259          if classData is not None:          if classData is not None:
260              self.SetStroke(classData.GetStroke())              self.SetStroke(classData.GetStroke())
# Line 259  class ClassData: Line 302  class ClassData:
302    
303  class ClassDataDefault(ClassData):  class ClassDataDefault(ClassData):
304      def __init__(self, classData = None):      def __init__(self, classData = None):
305          ClassData.__init__(self, ClassData.DEFAULT, classData)          ClassData.__init__(self, classData, ClassData.DEFAULT)
306            
307  class ClassDataPoint(ClassData):  class ClassDataPoint(ClassData):
308    
309      def __init__(self, value = 0, classData = None):      def __init__(self, value = 0, classData = None):
310          ClassData.__init__(self, ClassData.POINT, classData)          ClassData.__init__(self, classData, ClassData.POINT)
311    
312          self.value = value          self.value = value
313    
# Line 277  class ClassDataPoint(ClassData): Line 320  class ClassDataPoint(ClassData):
320  class ClassDataRange(ClassData):  class ClassDataRange(ClassData):
321    
322      def __init__(self, min = 0, max = 1, classData = None):      def __init__(self, min = 0, max = 1, classData = None):
323          ClassData.__init__(self, ClassData.RANGE, classData)          ClassData.__init__(self, classData, ClassData.RANGE)
324    
325          if min >= max:          if min >= max:
326              raise ValueError(_("ClassDataRange: %i(min) >= %i(max)!") %              raise ValueError(_("ClassDataRange: %i(min) >= %i(max)!") %
# Line 315  class ClassDataMap(ClassData): Line 358  class ClassDataMap(ClassData):
358      FUNC_ID = "id"      FUNC_ID = "id"
359    
360      def __init__(self, map_type = FUNC_ID, func = None, classData = None):      def __init__(self, map_type = FUNC_ID, func = None, classData = None):
361          ClassData.__init__(self, ClassData.MAP, classData)          ClassData.__init__(self, classData, ClassData.MAP)
362    
363          self.map_type = map_type          self.map_type = map_type
364          self.func = func          self.func = func

Legend:
Removed from v.427  
changed lines
  Added in v.428

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26