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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 388 - (show 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 # 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 If no mapping can be found then default data will
17 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 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
32 RANGE_MIN = 0
33 RANGE_MAX = 1
34 RANGE_DATA = 2
35
36 class Classification:
37
38 def __init__(self, layer, field = None):
39 """Initialize a classification.
40
41 layer -- the layer object who owns this classification
42
43 field -- the name of the data table field that
44 is to be used to classify layer properties
45 """
46
47 self.layer = layer
48 self.points = {}
49 self.ranges = []
50 self.DefaultData = ClassData()
51 self.field = field
52 #self.SetField(field)
53
54 def SetField(self, field):
55 """Set the name of the data table field to use.
56
57 field -- if None then all values map to the default data
58 """
59
60 self.field = field
61 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
62
63 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.
68
69 data -- data that the value maps to. See class description.
70 """
71
72 self.DefaultData = data
73
74 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.
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 raise ValueError(_("Range minimum >= maximum!"))
113 self.ranges.append([min, max, data])
114 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
115
116 def AddPoint(self, value, data):
117 """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 self.points[value] = data
127 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
128
129 def GetProperties(self, value):
130 """Return the associated data, or the default data.
131
132 The following search technique is used:
133 (1) if the field is None, return the default data
134 (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 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 """
143
144 if self.field is not None and value is not None:
145 #
146 # first check the discrete values
147 #
148 if self.points.has_key(value):
149 return self.points[value]
150
151 #
152 # now check the ranges
153 #
154 for p in self.ranges:
155 if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):
156 return p[RANGE_DATA]
157
158
159 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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26