27 |
from messages import \ |
from messages import \ |
28 |
LAYER_PROJECTION_CHANGED, \ |
LAYER_PROJECTION_CHANGED, \ |
29 |
LAYER_LEGEND_CHANGED, \ |
LAYER_LEGEND_CHANGED, \ |
30 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED,\ |
31 |
|
CLASS_CHANGED |
32 |
|
|
33 |
from Thuban.Model.color import Color, Transparent, Black |
from Thuban.Model.color import Color, Transparent, Black |
34 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
35 |
|
|
36 |
import Thuban.Model.layer |
import Thuban.Model.layer |
37 |
|
|
38 |
class Classification: |
from Thuban.Lib.connector import Publisher |
39 |
|
|
40 |
|
class Classification(Publisher): |
41 |
"""Encapsulates the classification of layer. |
"""Encapsulates the classification of layer. |
42 |
|
|
43 |
The Classification divides some kind of data into Groups which |
The Classification divides some kind of data into Groups which |
45 |
retrieved by matching data values to the appropriate group. |
retrieved by matching data values to the appropriate group. |
46 |
""" |
""" |
47 |
|
|
48 |
def __init__(self, layer = None, field = None): |
def __init__(self): |
49 |
"""Initialize a classification. |
"""Initialize a classification.""" |
|
|
|
|
layer -- the Layer object who owns this classification |
|
|
|
|
|
field -- the name of the data table field that |
|
|
is to be used to classify layer properties |
|
|
""" |
|
50 |
|
|
|
self.layer = None |
|
|
self.field = None |
|
|
self.fieldType = None |
|
51 |
self.__groups = [] |
self.__groups = [] |
52 |
|
|
53 |
self.SetDefaultGroup(ClassGroupDefault()) |
self.SetDefaultGroup(ClassGroupDefault()) |
54 |
|
|
|
self.SetFieldInfo(field, None) |
|
|
|
|
|
self._set_layer(layer) |
|
|
|
|
55 |
def __iter__(self): |
def __iter__(self): |
56 |
return ClassIterator(self.__groups) |
return ClassIterator(self.__groups) |
57 |
|
|
58 |
def __deepcopy__(self, memo): |
def __deepcopy__(self, memo): |
59 |
clazz = Classification() |
clazz = Classification() |
60 |
|
|
|
# note: the only thing that isn't copied is the layer reference |
|
|
clazz.field = self.field |
|
|
clazz.fieldType = self.fieldType |
|
61 |
clazz.__groups[0] = copy.deepcopy(self.__groups[0]) |
clazz.__groups[0] = copy.deepcopy(self.__groups[0]) |
62 |
|
|
63 |
for i in range(1, len(self.__groups)): |
for i in range(1, len(self.__groups)): |
67 |
|
|
68 |
def __SendNotification(self): |
def __SendNotification(self): |
69 |
"""Notify the layer that this class has changed.""" |
"""Notify the layer that this class has changed.""" |
70 |
if self.layer is not None: |
self.issue(CLASS_CHANGED) |
|
self.layer.ClassChanged() |
|
|
|
|
|
def GetField(self): |
|
|
"""Return the name of the field.""" |
|
|
return self.field |
|
|
|
|
|
def GetFieldType(self): |
|
|
"""Return the field type.""" |
|
|
return self.fieldType |
|
|
|
|
|
def SetFieldInfo(self, name, type): |
|
|
"""Set the classified field name to 'name' and it's field |
|
|
type to 'type' |
|
|
|
|
|
If this classification has an owning layer a ValueError |
|
|
exception will be thrown either the field or field type |
|
|
mismatch the information in the layer's table. |
|
|
|
|
|
If the field info is successful set and the class has |
|
|
an owning layer, the layer will be informed that the |
|
|
classification has changed. |
|
|
""" |
|
|
|
|
|
if name == "": |
|
|
name = None |
|
|
|
|
|
if self.layer is None: |
|
|
self.field = name |
|
|
self.fieldType = type |
|
|
elif name is None: |
|
|
self.field = None |
|
|
self.fieldType = None |
|
|
else: |
|
|
# |
|
|
# verify that the field exists in the layer and that |
|
|
# the type is correct. |
|
|
# |
|
|
fieldType = self.layer.GetFieldType(name) |
|
|
if fieldType is None: |
|
|
raise ValueError("'%s' was not found in the layer's table." |
|
|
% self.field) |
|
|
elif type is not None and fieldType != type: |
|
|
raise ValueError("type doesn't match layer's field type for %s" |
|
|
% self.field) |
|
71 |
|
|
72 |
self.field = name |
def __getattr__(self, attr): |
73 |
self.fieldType = fieldType |
"""Generate the compiled classification on demand""" |
74 |
|
if attr == "_compiled_classification": |
75 |
|
self._compile_classification() |
76 |
|
return self._compiled_classification |
77 |
|
raise AttributeError(attr) |
78 |
|
|
79 |
|
def _compile_classification(self): |
80 |
|
"""Generate the compiled classification |
81 |
|
|
82 |
|
The compiled classification is a more compact representation of |
83 |
|
the classification groups that is also more efficient for |
84 |
|
performing the classification. |
85 |
|
|
86 |
|
The compiled classification is a list of tuples. The first |
87 |
|
element of the tuple is a string which describes the rest of the |
88 |
|
tuple. There are two kinds of tuples: |
89 |
|
|
90 |
|
'singletons' |
91 |
|
|
92 |
|
The second element of the tuple is a dictionary which |
93 |
|
combines several consecutive ClassGroupSingleton instances. |
94 |
|
The dictionary maps the values of the singletons (as |
95 |
|
returned by the GetValue() method) to the corresponding |
96 |
|
group. |
97 |
|
|
98 |
|
'range' |
99 |
|
|
100 |
|
The tuple describes a ClassGroupRange instance. The tuples |
101 |
|
second element is a tuple fo the form (lfunc, min, max, |
102 |
|
rfunc, group) where group is the original group object, |
103 |
|
lfunc and rfuct are comparison functions and min and max are |
104 |
|
lower and upper bounds of the range. Given a value and such |
105 |
|
a tuple the group matches if and only if |
106 |
|
|
107 |
|
lfunc(min, value) and rfunc(max, value) |
108 |
|
|
109 |
|
is true. |
110 |
|
|
111 |
|
The compiled classification is bound to |
112 |
|
self._compile_classification. |
113 |
|
""" |
114 |
|
compiled = [] |
115 |
|
for group in self.__groups[1:]: |
116 |
|
if isinstance(group, ClassGroupSingleton): |
117 |
|
if not compiled or compiled[-1][0] != "singletons": |
118 |
|
compiled.append(("singletons", {})) |
119 |
|
compiled[-1][1].setdefault(group.GetValue(), group) |
120 |
|
elif isinstance(group, ClassGroupRange): |
121 |
|
left, min, max, right = group.GetRangeTuple() |
122 |
|
if left == "[": |
123 |
|
lfunc = operator.le |
124 |
|
elif left == "]": |
125 |
|
lfunc = operator.lt |
126 |
|
if right == "[": |
127 |
|
rfunc = operator.gt |
128 |
|
elif right == "]": |
129 |
|
rfunc = operator.ge |
130 |
|
compiled.append(("range", (lfunc, min, max, rfunc, group))) |
131 |
|
else: |
132 |
|
raise TypeError("Unknown group type %s", group) |
133 |
|
self._compiled_classification = compiled |
134 |
|
|
135 |
self.__SendNotification() |
def _clear_compiled_classification(self): |
136 |
|
"""Reset the compiled classification. |
137 |
|
|
138 |
def _set_layer(self, layer): |
If will be created on demand when self._compiled_classification |
139 |
"""Internal: Set the owning Layer of this classification. |
is accessed again. |
|
|
|
|
A ValueError exception will be thrown either the field or |
|
|
field type mismatch the information in the layer's table. |
|
140 |
|
|
141 |
If the layer is successful set, the layer will be informed |
Call this method whenever self.__groups is modified. |
|
that the classification has changed. |
|
142 |
""" |
""" |
143 |
|
try: |
144 |
if layer is None: |
del self._compiled_classification |
145 |
self.layer = None |
except: |
146 |
else: |
pass |
|
old_layer = self.layer |
|
|
self.layer = layer |
|
|
|
|
|
try: |
|
|
# this sync's the fieldType |
|
|
# and sends a notification to the layer |
|
|
self.SetFieldInfo(self.GetField(), None) |
|
|
except ValueError: |
|
|
self.layer = old_layer |
|
|
raise ValueError |
|
|
|
|
|
def GetLayer(self): |
|
|
"""Return the parent layer.""" |
|
|
return self.layer |
|
147 |
|
|
148 |
# |
# |
149 |
# these SetDefault* methods are really only provided for |
# these SetDefault* methods are really only provided for |
202 |
|
|
203 |
group -- group that the value maps to. |
group -- group that the value maps to. |
204 |
""" |
""" |
|
|
|
205 |
assert isinstance(group, ClassGroupDefault) |
assert isinstance(group, ClassGroupDefault) |
206 |
if len(self.__groups) > 0: |
if len(self.__groups) > 0: |
207 |
self.__groups[0] = group |
self.__groups[0] = group |
208 |
else: |
else: |
209 |
self.__groups.append(group) |
self.__groups.append(group) |
210 |
|
self.__SendNotification() |
211 |
|
|
212 |
def GetDefaultGroup(self): |
def GetDefaultGroup(self): |
213 |
"""Return the default group.""" |
"""Return the default group.""" |
222 |
self.InsertGroup(self.GetNumGroups(), item) |
self.InsertGroup(self.GetNumGroups(), item) |
223 |
|
|
224 |
def InsertGroup(self, index, group): |
def InsertGroup(self, index, group): |
|
|
|
225 |
assert isinstance(group, ClassGroup) |
assert isinstance(group, ClassGroup) |
|
|
|
226 |
self.__groups.insert(index + 1, group) |
self.__groups.insert(index + 1, group) |
227 |
|
self._clear_compiled_classification() |
228 |
self.__SendNotification() |
self.__SendNotification() |
229 |
|
|
230 |
def RemoveGroup(self, index): |
def RemoveGroup(self, index): |
231 |
return self.__groups.pop(index + 1) |
"""Remove the classification group with the given index""" |
232 |
|
self.__groups.pop(index + 1) |
233 |
|
self._clear_compiled_classification() |
234 |
|
self.__SendNotification() |
235 |
|
|
236 |
def ReplaceGroup(self, index, group): |
def ReplaceGroup(self, index, group): |
237 |
assert isinstance(group, ClassGroup) |
assert isinstance(group, ClassGroup) |
|
|
|
238 |
self.__groups[index + 1] = group |
self.__groups[index + 1] = group |
239 |
|
self._clear_compiled_classification() |
240 |
self.__SendNotification() |
self.__SendNotification() |
241 |
|
|
242 |
def GetGroup(self, index): |
def GetGroup(self, index): |
246 |
"""Return the number of non-default groups in the classification.""" |
"""Return the number of non-default groups in the classification.""" |
247 |
return len(self.__groups) - 1 |
return len(self.__groups) - 1 |
248 |
|
|
|
|
|
249 |
def FindGroup(self, value): |
def FindGroup(self, value): |
250 |
"""Return the associated group, or the default group. |
"""Return the group that matches the value. |
251 |
|
|
252 |
Groups are checked in the order the were added to the |
Groups are effectively checked in the order the were added to |
253 |
Classification. |
the Classification. |
254 |
|
|
255 |
value -- the value to classify. If there is no mapping, |
value -- the value to classify. If there is no mapping or value |
256 |
the field is None or value is None, |
is None, return the default properties |
|
return the default properties |
|
257 |
""" |
""" |
258 |
|
|
259 |
if self.GetField() is not None and value is not None: |
if value is not None: |
260 |
|
for typ, params in self._compiled_classification: |
261 |
for i in range(1, len(self.__groups)): |
if typ == "singletons": |
262 |
group = self.__groups[i] |
group = params.get(value) |
263 |
if group.Matches(value): |
if group is not None: |
264 |
return group |
return group |
265 |
|
elif typ == "range": |
266 |
|
lfunc, min, max, rfunc, g = params |
267 |
|
if lfunc(min, value) and rfunc(max, value): |
268 |
|
return g |
269 |
|
|
270 |
return self.GetDefaultGroup() |
return self.GetDefaultGroup() |
271 |
|
|
314 |
for p in self: |
for p in self: |
315 |
items.append(build_item(p, p.GetDisplayText())) |
items.append(build_item(p, p.GetDisplayText())) |
316 |
|
|
|
# if isinstance(p, ClassGroupDefault): |
|
|
# items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'"))) |
|
|
# elif isinstance(p, ClassGroupSingleton): |
|
|
# items.append(build_item(p, str(p.GetValue()))) |
|
|
# elif isinstance(p, ClassGroupRange): |
|
|
# items.append(build_item(p, "%s - %s" % |
|
|
# (p.GetMin(), p.GetMax()))) |
|
|
|
|
317 |
return (_("Classification"), items) |
return (_("Classification"), items) |
318 |
|
|
319 |
class ClassIterator: |
class ClassIterator: |
320 |
"""Allows the Groups in a Classifcation to be interated over. |
"""Allows the Groups in a Classifcation to be interated over. |
321 |
|
|
327 |
"""Constructor. |
"""Constructor. |
328 |
|
|
329 |
default -- the default group |
default -- the default group |
330 |
|
|
331 |
points -- a list of singleton groups |
points -- a list of singleton groups |
332 |
|
|
333 |
ranges -- a list of range groups |
ranges -- a list of range groups |
334 |
|
|
335 |
maps -- a list of map groups |
maps -- a list of map groups |
336 |
""" |
""" |
337 |
|
|
338 |
self.data = data #[default, points, ranges, maps] |
self.data = data |
339 |
self.data_index = 0 |
self.data_index = 0 |
|
#self.data_iter = iter(self.data) |
|
|
#self.iter = None |
|
340 |
|
|
341 |
def __iter__(self): |
def __iter__(self): |
342 |
return self |
return self |
350 |
d = self.data[self.data_index] |
d = self.data[self.data_index] |
351 |
self.data_index += 1 |
self.data_index += 1 |
352 |
return d |
return d |
353 |
|
|
|
# if self.iter is None: |
|
|
# try: |
|
|
# self.data_item = self.data_iter.next() |
|
|
# self.iter = iter(self.data_item) |
|
|
# except TypeError: |
|
|
# return self.data_item |
|
|
|
|
|
# try: |
|
|
# return self.iter.next() |
|
|
# except StopIteration: |
|
|
# self.iter = None |
|
|
# return self.next() |
|
|
|
|
354 |
class ClassGroupProperties: |
class ClassGroupProperties: |
355 |
"""Represents the properties of a single Classification Group. |
"""Represents the properties of a single Classification Group. |
356 |
|
|
357 |
These are used when rendering a layer.""" |
These are used when rendering a layer.""" |
358 |
|
|
359 |
|
# TODO: Actually, size is only relevant for point objects. |
360 |
|
# Eventually it should be spearated, e.g. when introducing symbols. |
361 |
|
|
362 |
def __init__(self, props = None): |
def __init__(self, props = None): |
363 |
"""Constructor. |
"""Constructor. |
364 |
|
|
365 |
props -- a ClassGroupProperties object. The class is copied if |
props -- a ClassGroupProperties object. The class is copied if |
366 |
prop is not None. Otherwise, a default set of properties |
prop is not None. Otherwise, a default set of properties |
367 |
is created such that: line color = Black, line width = 1, |
is created such that: line color = Black, line width = 1, |
368 |
and fill color = Transparent |
size = 5 and fill color = Transparent |
369 |
""" |
""" |
370 |
|
|
|
#self.stroke = None |
|
|
#self.strokeWidth = 0 |
|
|
#self.fill = None |
|
|
|
|
371 |
if props is not None: |
if props is not None: |
372 |
self.SetProperties(props) |
self.SetProperties(props) |
373 |
else: |
else: |
374 |
self.SetLineColor(Black) |
self.SetLineColor(Black) |
375 |
self.SetLineWidth(1) |
self.SetLineWidth(1) |
376 |
|
self.SetSize(5) |
377 |
self.SetFill(Transparent) |
self.SetFill(Transparent) |
378 |
|
|
379 |
def SetProperties(self, props): |
def SetProperties(self, props): |
382 |
assert isinstance(props, ClassGroupProperties) |
assert isinstance(props, ClassGroupProperties) |
383 |
self.SetLineColor(props.GetLineColor()) |
self.SetLineColor(props.GetLineColor()) |
384 |
self.SetLineWidth(props.GetLineWidth()) |
self.SetLineWidth(props.GetLineWidth()) |
385 |
|
self.SetSize(props.GetSize()) |
386 |
self.SetFill(props.GetFill()) |
self.SetFill(props.GetFill()) |
387 |
|
|
388 |
def GetLineColor(self): |
def GetLineColor(self): |
389 |
"""Return the line color as a Color object.""" |
"""Return the line color as a Color object.""" |
390 |
return self.__stroke |
return self.__stroke |
412 |
|
|
413 |
self.__strokeWidth = lineWidth |
self.__strokeWidth = lineWidth |
414 |
|
|
415 |
|
def GetSize(self): |
416 |
|
"""Return the size.""" |
417 |
|
return self.__size |
418 |
|
|
419 |
|
def SetSize(self, size): |
420 |
|
"""Set the size. |
421 |
|
|
422 |
|
size -- the new size. This must be > 0. |
423 |
|
""" |
424 |
|
assert isinstance(size, types.IntType) |
425 |
|
if (size < 1): |
426 |
|
raise ValueError(_("size < 1")) |
427 |
|
|
428 |
|
self.__size = size |
429 |
|
|
430 |
def GetFill(self): |
def GetFill(self): |
431 |
"""Return the fill color as a Color object.""" |
"""Return the fill color as a Color object.""" |
432 |
return self.__fill |
return self.__fill |
433 |
|
|
434 |
def SetFill(self, fill): |
def SetFill(self, fill): |
435 |
"""Set the fill color. |
"""Set the fill color. |
436 |
|
|
451 |
self.__stroke == other.__stroke) \ |
self.__stroke == other.__stroke) \ |
452 |
and (self.__fill is other.__fill or \ |
and (self.__fill is other.__fill or \ |
453 |
self.__fill == other.__fill) \ |
self.__fill == other.__fill) \ |
454 |
and self.__strokeWidth == other.__strokeWidth |
and self.__strokeWidth == other.__strokeWidth\ |
455 |
|
and self.__size == other.__size |
456 |
|
|
457 |
def __ne__(self, other): |
def __ne__(self, other): |
458 |
return not self.__eq__(other) |
return not self.__eq__(other) |
464 |
return ClassGroupProperties(self) |
return ClassGroupProperties(self) |
465 |
|
|
466 |
def __repr__(self): |
def __repr__(self): |
467 |
return repr((self.__stroke, self.__strokeWidth, self.__fill)) |
return repr((self.__stroke, self.__strokeWidth, self.__size, |
468 |
|
self.__fill)) |
469 |
|
|
470 |
class ClassGroup: |
class ClassGroup: |
471 |
"""A base class for all Groups within a Classification""" |
"""A base class for all Groups within a Classification""" |
488 |
def GetLabel(self): |
def GetLabel(self): |
489 |
"""Return the Group's label.""" |
"""Return the Group's label.""" |
490 |
return self.label |
return self.label |
491 |
|
|
492 |
def SetLabel(self, label): |
def SetLabel(self, label): |
493 |
"""Set the Group's label. |
"""Set the Group's label. |
494 |
|
|
657 |
""" |
""" |
658 |
|
|
659 |
ClassGroup.__init__(self, label, props, group) |
ClassGroup.__init__(self, label, props, group) |
|
|
|
|
#self.__min = self.__max = 0 |
|
|
#self.__range = Range("[" + repr(float(min)) + ";" + |
|
|
#repr(float(max)) + "[") |
|
660 |
self.SetRange(_range) |
self.SetRange(_range) |
661 |
|
|
662 |
def __copy__(self): |
def __copy__(self): |
711 |
|
|
712 |
def GetRange(self): |
def GetRange(self): |
713 |
"""Return the range as a string""" |
"""Return the range as a string""" |
|
#return (self.__min, self.__max) |
|
714 |
return self.__range.string(self.__range.GetRange()) |
return self.__range.string(self.__range.GetRange()) |
715 |
|
|
716 |
|
def GetRangeTuple(self): |
717 |
|
return self.__range.GetRange() |
718 |
|
|
719 |
def Matches(self, value): |
def Matches(self, value): |
720 |
"""Determine if the given value lies with the current range. |
"""Determine if the given value lies with the current range. |
721 |
|
|
723 |
""" |
""" |
724 |
|
|
725 |
return operator.contains(self.__range, value) |
return operator.contains(self.__range, value) |
|
#return self.__min <= value < self.__max |
|
726 |
|
|
727 |
def GetDisplayText(self): |
def GetDisplayText(self): |
728 |
label = self.GetLabel() |
label = self.GetLabel() |
729 |
|
|
730 |
if label != "": return label |
if label != "": return label |
731 |
|
|
|
#return _("%s - %s") % (self.GetMin(), self.GetMax()) |
|
|
#return repr(self.__range) |
|
732 |
return self.__range.string(self.__range.GetRange()) |
return self.__range.string(self.__range.GetRange()) |
733 |
|
|
734 |
def __eq__(self, other): |
def __eq__(self, other): |
735 |
return ClassGroup.__eq__(self, other) \ |
return ClassGroup.__eq__(self, other) \ |
736 |
and isinstance(other, ClassGroupRange) \ |
and isinstance(other, ClassGroupRange) \ |
737 |
and self.__range == other.__range |
and self.__range == other.__range |
|
#and self.__min == other.__min \ |
|
|
#and self.__max == other.__max |
|
738 |
|
|
739 |
def __repr__(self): |
def __repr__(self): |
740 |
return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")" |
return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")" |
|
#return "(" + repr(self.__min) + ", " + repr(self.__max) + ", " + \ |
|
|
#ClassGroup.__repr__(self) + ")" |
|
741 |
|
|
742 |
class ClassGroupMap(ClassGroup): |
class ClassGroupMap(ClassGroup): |
743 |
"""Currently, this class is not used.""" |
"""Currently, this class is not used.""" |