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 a NullData 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 Thuban import _ |
24 |
from Thuban.Model.color import Color |
25 |
|
26 |
from wxPython.wx import * |
27 |
|
28 |
# constants |
29 |
RANGE_MIN = 0 |
30 |
RANGE_MAX = 1 |
31 |
RANGE_DATA = 2 |
32 |
|
33 |
class Classification: |
34 |
|
35 |
|
36 |
def __init__(self, field = None): |
37 |
"""Initialize a classification. |
38 |
|
39 |
field -- the name of the data table field that |
40 |
is to be used to classify layer properties |
41 |
""" |
42 |
|
43 |
self.points = {} |
44 |
self.ranges = [] |
45 |
self.setField(field) |
46 |
self.setNull(None) |
47 |
|
48 |
def setField(self, field): |
49 |
"""Set the name of the data table field to use. |
50 |
|
51 |
field -- if None then all values map to NullData |
52 |
""" |
53 |
|
54 |
self.field = field |
55 |
|
56 |
def setNull(self, data): |
57 |
"""Set the data to be used when a value can't be classified. |
58 |
|
59 |
data -- data that the value maps to. See class description. |
60 |
""" |
61 |
|
62 |
self.NullData = data |
63 |
|
64 |
def addRange(self, min, max, data): |
65 |
"""Add a new range to the classification. |
66 |
|
67 |
A range allows a value to be classified if it falls between |
68 |
min and max. Specifically, min <= value < max |
69 |
|
70 |
min -- the lower bound. |
71 |
|
72 |
max -- the upper bound. |
73 |
|
74 |
data -- data that the value maps to. See class description. |
75 |
""" |
76 |
|
77 |
if min >= max: |
78 |
raise ValueError(_("Range minimum >= maximum!")) |
79 |
self.ranges.append([min, max, data]) |
80 |
|
81 |
def addPoint(self, value, data): |
82 |
"""Associate a single value with data. |
83 |
|
84 |
When this value is to be classified data will be returned. |
85 |
|
86 |
value -- classification value. |
87 |
|
88 |
data -- data that the value maps to. See class description. |
89 |
""" |
90 |
|
91 |
self.points[value] = data |
92 |
|
93 |
def getProperties(self, value): |
94 |
"""Return the associated data, or the NullData. |
95 |
|
96 |
The following search technique is used: |
97 |
(1) if the field is None, return NullData |
98 |
(2) check if the value exists as a single value |
99 |
(3) check if the value falls within a range. Ranges |
100 |
are checked in the order they were added to |
101 |
the classification. |
102 |
|
103 |
value -- the value to classify. If there is no mapping |
104 |
return the NullData (which may be None) |
105 |
""" |
106 |
|
107 |
if self.field is not None: |
108 |
# |
109 |
# first check the discrete values |
110 |
# |
111 |
if self.points.has_key(value): |
112 |
return self.points[value] |
113 |
|
114 |
# |
115 |
# now check the ranges |
116 |
# |
117 |
for p in self.ranges: |
118 |
if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]): |
119 |
return p[RANGE_DATA] |
120 |
|
121 |
|
122 |
return self.NullData |
123 |
|
124 |
def TreeInfo(self): |
125 |
items = [] |
126 |
|
127 |
# |
128 |
# shouldn't print anything if there are no classifications |
129 |
# |
130 |
|
131 |
|
132 |
def color_string(color): |
133 |
if color is None: |
134 |
return "None" |
135 |
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
136 |
|
137 |
if self.NullData is not None: |
138 |
i = [] |
139 |
for key, value in self.NullData.items(): |
140 |
if isinstance(value, Color): |
141 |
i.append((_("%s: %s") % (key, color_string(value)), value)) |
142 |
else: |
143 |
i.append(_("%s: %s") % (key, value)) |
144 |
items.append((_("'NULL'"), i)) |
145 |
|
146 |
for name, data in self.points.items(): |
147 |
i = [] |
148 |
for key, value in data.items(): |
149 |
if isinstance(value, Color): |
150 |
i.append((_("%s: %s") % (key, color_string(value)), value)) |
151 |
else: |
152 |
i.append(_("%s: %s") % (key, value)) |
153 |
items.append((_("%s") % name, i)) |
154 |
|
155 |
for p in self.ranges: |
156 |
i = [] |
157 |
data = p[RANGE_DATA] |
158 |
for key, value in data.items(): |
159 |
if isinstance(value, Color): |
160 |
i.append((_("%s: %s") % (key, color_string(value)), value)) |
161 |
else: |
162 |
i.append(_("%s: %s") % (key, value)) |
163 |
items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX], i))) |
164 |
|
165 |
return (_("Classifications"), items) |
166 |
|