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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 388 - (hide annotations)
Mon Feb 10 15:25:05 2003 UTC (22 years ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/classification.py
File MIME type: text/x-python
File size: 6530 byte(s)
* Thuban/Model/classification.py (Classificaton): Added set and
        get methods for the default data. The class also takes a layer
        reference so that modification messages can be sent. Fixed the
        methods to use the new ClassData class.
        (ClassData): New class to encapsulate the classification data

1 jonathan 371 # Copyright (c) 2001 by Intevation GmbH
2     # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     __version__ = "$Revision$"
9    
10     """
11     A Classification provides a mapping from an input value
12     to data. This mapping can be specified in two ways.
13     First, specific values can be associated with data.
14     Second, ranges can be associated with data such that if
15     an input value falls with a range that data is returned.
16 jonathan 388 If no mapping can be found then default data will
17 jonathan 371 be returned. Input values must be hashable objects
18    
19     See the description of getProperties() for more information
20     on the mapping algorithm.
21     """
22    
23 jonathan 388 from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
24     LAYER_VISIBILITY_CHANGED
25    
26 jan 374 from Thuban import _
27 jonathan 381 from Thuban.Model.color import Color
28 jan 374
29 jonathan 381 from wxPython.wx import *
30    
31 jonathan 371 # constants
32     RANGE_MIN = 0
33     RANGE_MAX = 1
34     RANGE_DATA = 2
35    
36     class Classification:
37    
38 jonathan 388 def __init__(self, layer, field = None):
39 jonathan 371 """Initialize a classification.
40    
41 jonathan 388 layer -- the layer object who owns this classification
42    
43 jonathan 371 field -- the name of the data table field that
44     is to be used to classify layer properties
45     """
46    
47 jonathan 388 self.layer = layer
48 jonathan 378 self.points = {}
49     self.ranges = []
50 jonathan 388 self.DefaultData = ClassData()
51     self.field = field
52     #self.SetField(field)
53    
54     def SetField(self, field):
55 jonathan 371 """Set the name of the data table field to use.
56    
57 jonathan 388 field -- if None then all values map to the default data
58 jonathan 371 """
59    
60     self.field = field
61 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
62 jonathan 371
63 jonathan 388 def GetField(self):
64     return self.field
65    
66     def SetDefaultData(self, data):
67 jonathan 371 """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.
70     """
71    
72 jonathan 388 self.DefaultData = data
73 jonathan 371
74 jonathan 388 def GetDefaultData(self):
75     return self.DefaultData
76 jonathan 385
77 jonathan 388 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 jonathan 371 """Add a new range to the classification.
100    
101     A range allows a value to be classified if it falls between
102     min and max. Specifically, min <= value < max
103    
104     min -- the lower bound.
105    
106     max -- the upper bound.
107    
108     data -- data that the value maps to. See class description.
109     """
110    
111     if min >= max:
112 jan 374 raise ValueError(_("Range minimum >= maximum!"))
113 jonathan 378 self.ranges.append([min, max, data])
114 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
115 jonathan 371
116 jonathan 388 def AddPoint(self, value, data):
117 jonathan 371 """Associate a single value with data.
118    
119     When this value is to be classified data will be returned.
120    
121     value -- classification value.
122    
123     data -- data that the value maps to. See class description.
124     """
125    
126 jonathan 378 self.points[value] = data
127 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
128 jonathan 371
129 jonathan 388 def GetProperties(self, value):
130     """Return the associated data, or the default data.
131 jonathan 371
132     The following search technique is used:
133 jonathan 388 (1) if the field is None, return the default data
134 jonathan 371 (2) check if the value exists as a single value
135     (3) check if the value falls within a range. Ranges
136     are checked in the order they were added to
137     the classification.
138    
139 jonathan 388 value -- the value to classify. If there is no mapping,
140     or value is None, return the default data
141     (which may be None)
142 jonathan 371 """
143    
144 jonathan 388 if self.field is not None and value is not None:
145 jonathan 371 #
146     # first check the discrete values
147     #
148 jonathan 378 if self.points.has_key(value):
149     return self.points[value]
150 jonathan 371
151     #
152     # now check the ranges
153     #
154 jonathan 378 for p in self.ranges:
155 jonathan 371 if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):
156     return p[RANGE_DATA]
157    
158    
159 jonathan 388 return self.DefaultData
160 jonathan 371
161 jonathan 381 def TreeInfo(self):
162     items = []
163 jonathan 378
164 jonathan 381 #
165 jonathan 388 # XXX: shouldn't print anything if there are no classifications
166 jonathan 381 #
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 jonathan 388 def build_item(data):
175 jonathan 381 i = []
176    
177 jonathan 388 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 jonathan 381 for name, data in self.points.items():
188 jonathan 388 items.append((_("%s") % name, build_item(data)))
189 jonathan 381
190     for p in self.ranges:
191     data = p[RANGE_DATA]
192 jonathan 388 items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX])),
193     build_item(data))
194    
195 jonathan 381 return (_("Classifications"), items)
196    
197 jonathan 388
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    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26