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