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 |
|
25 |
# constants |
26 |
RANGE_MIN = 0 |
27 |
RANGE_MAX = 1 |
28 |
RANGE_DATA = 2 |
29 |
|
30 |
class Classification: |
31 |
|
32 |
|
33 |
def __init__(self, field = None): |
34 |
"""Initialize a classification. |
35 |
|
36 |
field -- the name of the data table field that |
37 |
is to be used to classify layer properties |
38 |
""" |
39 |
|
40 |
self.points = {} |
41 |
self.ranges = [] |
42 |
self.setField(field) |
43 |
self.setNull(None) |
44 |
|
45 |
def setField(self, field): |
46 |
"""Set the name of the data table field to use. |
47 |
|
48 |
field -- if None then all values map to NullData |
49 |
""" |
50 |
|
51 |
self.field = field |
52 |
|
53 |
def setNull(self, data): |
54 |
"""Set the data to be used when a value can't be classified. |
55 |
|
56 |
data -- data that the value maps to. See class description. |
57 |
""" |
58 |
|
59 |
self.NullData = data |
60 |
|
61 |
def addRange(self, min, max, data): |
62 |
"""Add a new range to the classification. |
63 |
|
64 |
A range allows a value to be classified if it falls between |
65 |
min and max. Specifically, min <= value < max |
66 |
|
67 |
min -- the lower bound. |
68 |
|
69 |
max -- the upper bound. |
70 |
|
71 |
data -- data that the value maps to. See class description. |
72 |
""" |
73 |
|
74 |
if min >= max: |
75 |
raise ValueError(_("Range minimum >= maximum!")) |
76 |
self.ranges.append([min, max, data]) |
77 |
|
78 |
def addPoint(self, value, data): |
79 |
"""Associate a single value with data. |
80 |
|
81 |
When this value is to be classified data will be returned. |
82 |
|
83 |
value -- classification value. |
84 |
|
85 |
data -- data that the value maps to. See class description. |
86 |
""" |
87 |
|
88 |
self.points[value] = data |
89 |
|
90 |
def getProperties(self, value): |
91 |
"""Return the associated data, or the NullData. |
92 |
|
93 |
The following search technique is used: |
94 |
(1) if the field is None, return NullData |
95 |
(2) check if the value exists as a single value |
96 |
(3) check if the value falls within a range. Ranges |
97 |
are checked in the order they were added to |
98 |
the classification. |
99 |
|
100 |
value -- the value to classify. If there is no mapping |
101 |
return the NullData (which may be None) |
102 |
""" |
103 |
|
104 |
if self.field is not None: |
105 |
# |
106 |
# first check the discrete values |
107 |
# |
108 |
if self.points.has_key(value): |
109 |
return self.points[value] |
110 |
|
111 |
# |
112 |
# now check the ranges |
113 |
# |
114 |
for p in self.ranges: |
115 |
if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]): |
116 |
return p[RANGE_DATA] |
117 |
|
118 |
|
119 |
return self.NullData |
120 |
|
121 |
|