20 |
from classification import Classification, ClassGroupSingleton, \ |
from classification import Classification, ClassGroupSingleton, \ |
21 |
ClassGroupRange, ClassGroupProperties |
ClassGroupRange, ClassGroupProperties |
22 |
|
|
23 |
def generate_singletons(_list, ramp): |
def generate_singletons(_list, ramp, fixes=(None, None, None)): |
24 |
"""Generate a new classification consisting solely of singletons. |
"""Generate a new classification consisting solely of singletons. |
25 |
|
|
26 |
The resulting classification will consist of one group for each |
The resulting classification will consist of one group for each |
27 |
item in _list whose properties ramp between 'prop1' and 'prop2'. |
item in _list whose properties ramp between 'prop1' and 'prop2'. |
28 |
|
|
29 |
_list -- any object that implements the iterator interface |
_list -- a list of values for each singleton |
30 |
|
|
31 |
ramp -- an object which implements the CustomRamp interface |
ramp -- an object which implements the CustomRamp interface |
32 |
|
|
33 |
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
34 |
|
if any item is not None, the appropriate property will |
35 |
|
be fixed to that item value. |
36 |
""" |
""" |
37 |
|
|
38 |
clazz = Classification() |
clazz = Classification() |
39 |
|
|
40 |
i = 0 |
i = 0 |
41 |
maxValue = float(len(_list) - 1) |
maxValue = float(len(_list) - 1) |
42 |
|
if maxValue < 1: maxValue = 1 |
43 |
|
|
44 |
for value in _list: |
for value in _list: |
45 |
prop = ramp.GetProperties(i / maxValue) |
prop = ramp.GetProperties(i / maxValue) |
46 |
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
47 |
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
48 |
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
49 |
clazz.AppendGroup(ClassGroupSingleton(value, prop)) |
clazz.AppendGroup(ClassGroupSingleton(value, prop)) |
50 |
i += 1 |
i += 1 |
51 |
|
|
52 |
return clazz |
return clazz |
53 |
|
|
54 |
def generate_uniform_distribution(min, max, numGroups, ramp, intStep = False): |
def generate_uniform_distribution(min, max, numGroups, ramp, intStep = False, |
55 |
|
fixes = (None, None, None)): |
56 |
"""Generate a classification with numGroups range groups |
"""Generate a classification with numGroups range groups |
57 |
each with the same interval. |
each with the same interval. |
58 |
|
|
60 |
Useful if the values are integers but the |
Useful if the values are integers but the |
61 |
number of groups specified doesn't evenly |
number of groups specified doesn't evenly |
62 |
divide (max - min). |
divide (max - min). |
63 |
|
|
64 |
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
65 |
|
if any item is not None, the appropriate property will |
66 |
|
be fixed to that item value. |
67 |
""" |
""" |
68 |
|
|
69 |
clazz = Classification() |
clazz = Classification() |
72 |
|
|
73 |
end = "[" |
end = "[" |
74 |
maxValue = float(numGroups - 1) |
maxValue = float(numGroups - 1) |
75 |
|
if maxValue < 1: maxValue = 1 |
76 |
|
|
77 |
for i in range(1, numGroups + 1): |
for i in range(1, numGroups + 1): |
78 |
|
|
79 |
prop = ramp.GetProperties(float(i-1) / maxValue) |
prop = ramp.GetProperties(float(i-1) / maxValue) |
80 |
|
|
81 |
if intStep: |
if intStep: |
82 |
cur_max = min + int(round((i * (max - min + 1)) / maxValue)) |
cur_max = min + int(round((i * (max - min + 1)) / float(numGroups))) |
83 |
else: |
else: |
84 |
cur_max = min + (i * (max - min)) / maxValue |
cur_max = min + (i * (max - min)) / float(numGroups) |
85 |
|
|
86 |
if i == numGroups: |
if i == numGroups: |
87 |
cur_max = max |
cur_max = max |
92 |
else: |
else: |
93 |
_range = Range(("[", cur_min, cur_max, end)) |
_range = Range(("[", cur_min, cur_max, end)) |
94 |
|
|
95 |
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
96 |
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
97 |
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
98 |
|
|
99 |
clazz.AppendGroup(ClassGroupRange(_range, prop)) |
clazz.AppendGroup(ClassGroupRange(_range, prop)) |
100 |
|
|
101 |
cur_min = cur_max |
cur_min = cur_max |
102 |
|
|
103 |
return clazz |
return clazz |
104 |
|
|
105 |
def generate_quantiles(_list, percents, ramp, _range): |
def generate_quantiles(_list, percents, ramp, _range, fixes=(None, None, None)): |
106 |
"""Generates a Classification which has groups of ranges that |
"""Generates a Classification which has groups of ranges that |
107 |
represent quantiles of _list at the percentages given in percents. |
represent quantiles of _list at the percentages given in percents. |
108 |
Only the values that fall within _range are considered. |
Only the values that fall within _range are considered. |
122 |
|
|
123 |
_range -- a Range object |
_range -- a Range object |
124 |
|
|
125 |
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
126 |
|
if any item is not None, the appropriate property will |
127 |
|
be fixed to that item value. |
128 |
|
|
129 |
Raises a Value Error if 'percents' has fewer than two items, or |
Raises a Value Error if 'percents' has fewer than two items, or |
130 |
does not cover the entire range. |
does not cover the entire range. |
131 |
""" |
""" |
149 |
end = "]" |
end = "]" |
150 |
|
|
151 |
maxValue = float(numGroups - 1) |
maxValue = float(numGroups - 1) |
152 |
|
if maxValue < 1: maxValue = 1 |
153 |
for (q, p) in quantiles[3]: |
for (q, p) in quantiles[3]: |
154 |
|
|
155 |
prop = ramp.GetProperties(float(i-1) / maxValue) |
prop = ramp.GetProperties(float(i-1) / maxValue) |
160 |
else: |
else: |
161 |
max = _list[q] |
max = _list[q] |
162 |
|
|
163 |
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
164 |
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
165 |
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
166 |
|
|
167 |
group = ClassGroupRange(Range((start, min, max, end)), prop) |
group = ClassGroupRange(Range((start, min, max, end)), prop) |
168 |
|
|
169 |
group.SetLabel("%s%% - %s%%" % (round(oldp*100, 2), |
group.SetLabel("%s%% - %s%%" % (round(oldp*100, 2), |
218 |
end = "]" |
end = "]" |
219 |
|
|
220 |
maxValue = float(numGroups - 1) |
maxValue = float(numGroups - 1) |
221 |
|
if maxValue < 1: maxValue = 1 |
222 |
for (q, p) in quantiles[3][1:]: |
for (q, p) in quantiles[3][1:]: |
223 |
prop = ramp.GetProperties(float(i-1) / maxValue) |
prop = ramp.GetProperties(float(i-1) / maxValue) |
224 |
|
|
420 |
|
|
421 |
newProps = ClassGroupProperties() |
newProps = ClassGroupProperties() |
422 |
|
|
423 |
color1 = self.prop1.GetLineColor() |
self.__SetProperty(self.prop1.GetLineColor(), |
424 |
color2 = self.prop2.GetLineColor() |
self.prop2.GetLineColor(), |
425 |
|
index, newProps.SetLineColor) |
426 |
self.__SetProperty(color1, color2, index, newProps.SetLineColor) |
self.__SetProperty(self.prop1.GetFill(), self.prop2.GetFill(), |
427 |
self.__SetProperty(color1, color2, index, newProps.SetFill) |
index, newProps.SetFill) |
428 |
|
|
429 |
w = (self.prop2.GetLineWidth() - self.prop1.GetLineWidth()) \ |
w = (self.prop2.GetLineWidth() - self.prop1.GetLineWidth()) \ |
430 |
* index \ |
* index \ |
431 |
+ self.prop1.GetLineWidth() |
+ self.prop1.GetLineWidth() |
|
|
|
432 |
newProps.SetLineWidth(int(round(w))) |
newProps.SetLineWidth(int(round(w))) |
433 |
|
|
434 |
return newProps |
return newProps |
469 |
RedRamp = MonochromaticRamp(Color(1, 1, 1), Color(.8, 0, 0)) |
RedRamp = MonochromaticRamp(Color(1, 1, 1), Color(.8, 0, 0)) |
470 |
GreenRamp = MonochromaticRamp(Color(1, 1, 1), Color(0, .8, 0)) |
GreenRamp = MonochromaticRamp(Color(1, 1, 1), Color(0, .8, 0)) |
471 |
BlueRamp = MonochromaticRamp(Color(1, 1, 1), Color(0, 0, .8)) |
BlueRamp = MonochromaticRamp(Color(1, 1, 1), Color(0, 0, .8)) |
472 |
GreenToRedRamp = MonochromaticRamp(Color(1, .8, 1), Color(1, 0, 0)) |
GreenToRedRamp = MonochromaticRamp(Color(0, .8, 0), Color(1, 0, 0)) |
473 |
|
|
474 |
class HotToColdRamp: |
class HotToColdRamp: |
475 |
|
|