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 |
# fix for people using python2.1 |
24 |
from __future__ import nested_scopes |
25 |
|
26 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
27 |
LAYER_VISIBILITY_CHANGED |
28 |
|
29 |
from Thuban import _ |
30 |
from Thuban.Model.color import Color |
31 |
|
32 |
from wxPython.wx import * |
33 |
|
34 |
# constants |
35 |
RANGE_MIN = 0 |
36 |
RANGE_MAX = 1 |
37 |
RANGE_DATA = 2 |
38 |
|
39 |
class Classification: |
40 |
|
41 |
def __init__(self, layer, field = None): |
42 |
"""Initialize a classification. |
43 |
|
44 |
layer -- the layer object who owns this classification |
45 |
|
46 |
field -- the name of the data table field that |
47 |
is to be used to classify layer properties |
48 |
""" |
49 |
|
50 |
self.layer = layer |
51 |
self.points = {} |
52 |
self.ranges = [] |
53 |
self.DefaultData = ClassData() |
54 |
self.field = field |
55 |
#self.SetField(field) |
56 |
|
57 |
def SetField(self, field): |
58 |
"""Set the name of the data table field to use. |
59 |
|
60 |
field -- if None then all values map to the default data |
61 |
""" |
62 |
|
63 |
self.field = field |
64 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
65 |
|
66 |
def GetField(self): |
67 |
return self.field |
68 |
|
69 |
def SetDefaultData(self, data): |
70 |
"""Set the data to be used when a value can't be classified. |
71 |
|
72 |
data -- data that the value maps to. See class description. |
73 |
""" |
74 |
|
75 |
self.DefaultData = data |
76 |
|
77 |
def GetDefaultData(self): |
78 |
return self.DefaultData |
79 |
|
80 |
def SetDefaultFill(self, fill): |
81 |
self.DefaultData.SetFill(fill) |
82 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
83 |
|
84 |
def GetDefaultFill(self): |
85 |
return self.DefaultData.GetFill() |
86 |
|
87 |
def SetDefaultStroke(self, stroke): |
88 |
self.DefaultData.SetStroke(stroke) |
89 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
90 |
|
91 |
def GetDefaultStroke(self): |
92 |
return self.DefaultData.GetStroke() |
93 |
|
94 |
def SetDefaultStrokeWidth(self, strokeWidth): |
95 |
self.DefaultData.SetStrokeWidth(strokeWidth) |
96 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
97 |
|
98 |
def GetDefaultStrokeWidth(self): |
99 |
return self.DefaultData.GetStrokeWidth() |
100 |
|
101 |
def AddRange(self, min, max, data): |
102 |
"""Add a new range to the classification. |
103 |
|
104 |
A range allows a value to be classified if it falls between |
105 |
min and max. Specifically, min <= value < max |
106 |
|
107 |
min -- the lower bound. |
108 |
|
109 |
max -- the upper bound. |
110 |
|
111 |
data -- data that the value maps to. See class description. |
112 |
""" |
113 |
|
114 |
if min >= max: |
115 |
raise ValueError(_("Range minimum >= maximum!")) |
116 |
self.ranges.append([min, max, data]) |
117 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
118 |
|
119 |
def AddPoint(self, value, data): |
120 |
"""Associate a single value with data. |
121 |
|
122 |
When this value is to be classified data will be returned. |
123 |
|
124 |
value -- classification value. |
125 |
|
126 |
data -- data that the value maps to. See class description. |
127 |
""" |
128 |
|
129 |
self.points[value] = data |
130 |
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
131 |
|
132 |
def GetProperties(self, value): |
133 |
"""Return the associated data, or the default data. |
134 |
|
135 |
The following search technique is used: |
136 |
(1) if the field is None, return the default data |
137 |
(2) check if the value exists as a single value |
138 |
(3) check if the value falls within a range. Ranges |
139 |
are checked in the order they were added to |
140 |
the classification. |
141 |
|
142 |
value -- the value to classify. If there is no mapping, |
143 |
or value is None, return the default data |
144 |
(which may be None) |
145 |
""" |
146 |
|
147 |
if self.field is not None and value is not None: |
148 |
# |
149 |
# first check the discrete values |
150 |
# |
151 |
if self.points.has_key(value): |
152 |
return self.points[value] |
153 |
|
154 |
# |
155 |
# now check the ranges |
156 |
# |
157 |
for p in self.ranges: |
158 |
if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]): |
159 |
return p[RANGE_DATA] |
160 |
|
161 |
|
162 |
return self.DefaultData |
163 |
|
164 |
def TreeInfo(self): |
165 |
items = [] |
166 |
|
167 |
# |
168 |
# XXX: shouldn't print anything if there are no classifications |
169 |
# |
170 |
|
171 |
|
172 |
def color_string(color): |
173 |
if color is None: |
174 |
return "None" |
175 |
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
176 |
|
177 |
def build_item(data): |
178 |
i = [] |
179 |
|
180 |
v = data.GetStroke() |
181 |
i.append((_("Stroke: %s") % color_string(v), v)) |
182 |
v = data.GetStrokeWidth() |
183 |
i.append((_("Stroke Width: %s") % v)) |
184 |
v = data.GetFill() |
185 |
i.append((_("Fill: %s") % color_string(v), v)) |
186 |
return i |
187 |
|
188 |
items.append((_("'DEFAULT'"), build_item(self.DefaultData))) |
189 |
|
190 |
for name, data in self.points.items(): |
191 |
items.append((_("%s") % name, build_item(data))) |
192 |
|
193 |
for p in self.ranges: |
194 |
data = p[RANGE_DATA] |
195 |
items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX])), |
196 |
build_item(data)) |
197 |
|
198 |
return (_("Classifications"), items) |
199 |
|
200 |
|
201 |
class ClassData: |
202 |
|
203 |
def __init__(self): |
204 |
self.stroke = None |
205 |
self.stroke_width = 0 |
206 |
self.fill = None |
207 |
self.label = "" |
208 |
|
209 |
def GetStroke(self): |
210 |
return self.stroke |
211 |
|
212 |
def SetStroke(self, stroke): |
213 |
self.stroke = stroke |
214 |
|
215 |
def GetStrokeWidth(self): |
216 |
return self.stroke_width |
217 |
|
218 |
def SetStrokeWidth(self, stroke_width): |
219 |
self.stroke_width = stroke_width |
220 |
|
221 |
def GetFill(self): |
222 |
return self.fill |
223 |
|
224 |
def SetFill(self, fill): |
225 |
self.fill = fill |
226 |
|
227 |
def GetLabel(self): |
228 |
return self.label |
229 |
|
230 |
def SetLabel(self, label): |
231 |
self.label = label |
232 |
|