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 |
|
# 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 |
# constants |
35 |
RANGE_MIN = 0 |
RANGE_MIN = 0 |
36 |
RANGE_MAX = 1 |
RANGE_MAX = 1 |
38 |
|
|
39 |
class Classification: |
class Classification: |
40 |
|
|
41 |
|
def __init__(self, layer, field = None): |
|
def __init__(self, field = None): |
|
42 |
"""Initialize a classification. |
"""Initialize a classification. |
43 |
|
|
44 |
|
layer -- the layer object who owns this classification |
45 |
|
|
46 |
field -- the name of the data table field that |
field -- the name of the data table field that |
47 |
is to be used to classify layer properties |
is to be used to classify layer properties |
48 |
""" |
""" |
49 |
|
|
50 |
self.__points = {} |
self.layer = layer |
51 |
self.__ranges = [] |
self.points = {} |
52 |
self.setField(field) |
self.ranges = [] |
53 |
self.setNull(None) |
self.DefaultData = ClassData() |
54 |
|
self.field = field |
55 |
def setField(self, field): |
#self.SetField(field) |
56 |
|
|
57 |
|
def SetField(self, field): |
58 |
"""Set the name of the data table field to use. |
"""Set the name of the data table field to use. |
59 |
|
|
60 |
field -- if None then all values map to NullData |
field -- if None then all values map to the default data |
61 |
""" |
""" |
62 |
|
|
63 |
self.field = field |
self.field = field |
64 |
|
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
65 |
|
|
66 |
|
def GetField(self): |
67 |
|
return self.field |
68 |
|
|
69 |
def setNull(self, data): |
def SetDefaultData(self, data): |
70 |
"""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. |
71 |
|
|
72 |
data -- data that the value maps to. See class description. |
data -- data that the value maps to. See class description. |
73 |
""" |
""" |
74 |
|
|
75 |
self.NullData = data |
self.DefaultData = data |
76 |
|
|
77 |
def addRange(self, min, max, data): |
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. |
"""Add a new range to the classification. |
103 |
|
|
104 |
A range allows a value to be classified if it falls between |
A range allows a value to be classified if it falls between |
112 |
""" |
""" |
113 |
|
|
114 |
if min >= max: |
if min >= max: |
115 |
raise ValueError("Range minimum >= maximum!") |
raise ValueError(_("Range minimum >= maximum!")) |
116 |
self.__ranges.append([min, max, data]) |
self.ranges.append([min, max, data]) |
117 |
|
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
118 |
|
|
119 |
def addPoint(self, value, data): |
def AddPoint(self, value, data): |
120 |
"""Associate a single value with data. |
"""Associate a single value with data. |
121 |
|
|
122 |
When this value is to be classified data will be returned. |
When this value is to be classified data will be returned. |
126 |
data -- data that the value maps to. See class description. |
data -- data that the value maps to. See class description. |
127 |
""" |
""" |
128 |
|
|
129 |
self.__points[value] = data |
self.points[value] = data |
130 |
|
self.layer.changed(LAYER_LEGEND_CHANGED, self.layer) |
131 |
|
|
132 |
def getProperties(self, value): |
def GetProperties(self, value): |
133 |
"""Return the associated data, or the NullData. |
"""Return the associated data, or the default data. |
134 |
|
|
135 |
The following search technique is used: |
The following search technique is used: |
136 |
(1) if the field is None, return NullData |
(1) if the field is None, return the default data |
137 |
(2) check if the value exists as a single value |
(2) check if the value exists as a single value |
138 |
(3) check if the value falls within a range. Ranges |
(3) check if the value falls within a range. Ranges |
139 |
are checked in the order they were added to |
are checked in the order they were added to |
140 |
the classification. |
the classification. |
141 |
|
|
142 |
value -- the value to classify. If there is no mapping |
value -- the value to classify. If there is no mapping, |
143 |
return the NullData (which may be None) |
or value is None, return the default data |
144 |
|
(which may be None) |
145 |
""" |
""" |
146 |
|
|
147 |
if self.field is not None: |
if self.field is not None and value is not None: |
148 |
# |
# |
149 |
# first check the discrete values |
# first check the discrete values |
150 |
# |
# |
151 |
if self.__points.has_key(value): |
if self.points.has_key(value): |
152 |
return self.__points[value] |
return self.points[value] |
153 |
|
|
154 |
# |
# |
155 |
# now check the ranges |
# now check the ranges |
156 |
# |
# |
157 |
for p in self.__ranges: |
for p in self.ranges: |
158 |
if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]): |
if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]): |
159 |
return p[RANGE_DATA] |
return p[RANGE_DATA] |
160 |
|
|
161 |
|
|
162 |
return self.NullData |
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 |
|
|