/[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 371 by jonathan, Mon Jan 27 13:49:39 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 _
27    from Thuban.Model.color import Color
28    
29    from wxPython.wx import *
30    
31  # constants  # constants
32  RANGE_MIN  = 0  RANGE_MIN  = 0
33  RANGE_MAX  = 1  RANGE_MAX  = 1
# Line 27  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.__points = {}          self.layer = layer
48          self.__ranges = []          self.points = {}
49          self.setField(field)          self.ranges = []
50          self.setNull(None)          self.DefaultData = ClassData()
51                    self.field = field
52      def setField(self, field):          #self.SetField(field)
53    
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 setNull(self, data):      def GetField(self):
64            return self.field
65    
66        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 GetDefaultData(self):
75            return self.DefaultData
76    
77      def addRange(self, min, max, data):      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 70  class Classification: Line 109  class Classification:
109          """          """
110    
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 83  class Classification: Line 123  class Classification:
123             data  -- data that the value maps to. See class description.             data  -- data that the value maps to. See class description.
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              #              #
148              if self.__points.has_key(value):              if self.points.has_key(value):
149                  return self.__points[value]                  return self.points[value]
150    
151              #              #
152              # now check the ranges              # now check the ranges
153              #              #
154              for p in self.__ranges:              for p in self.ranges:
155                  if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):                  if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):
156                      return p[RANGE_DATA]                      return p[RANGE_DATA]
157    
158    
159          return self.NullData          return self.DefaultData
160    
161        def TreeInfo(self):
162            items = []
163    
164            #
165            # XXX: shouldn't print anything if there are no classifications
166            #
167    
168            
169            def color_string(color):
170                if color is None:
171                    return "None"
172                return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)
173    
174            def build_item(data):
175                i = []
176    
177                v = data.GetStroke()
178                i.append((_("Stroke: %s") % color_string(v), v))
179                v = data.GetStrokeWidth()
180                i.append((_("Stroke Width: %s") % v))
181                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():
188                items.append((_("%s") % name, build_item(data)))
189    
190            for p in self.ranges:
191                data = p[RANGE_DATA]
192                items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX])),
193                             build_item(data))
194    
195            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.371  
changed lines
  Added in v.388

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26