20 |
from classification import Classification, ClassGroupSingleton, \ |
from classification import Classification, ClassGroupSingleton, \ |
21 |
ClassGroupRange, ClassGroupProperties |
ClassGroupRange, ClassGroupProperties |
22 |
|
|
23 |
def generate_singletons(_list, ramp, fixes=(None, None, None)): |
def generate_singletons(_list, ramp): |
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 |
29 |
_list -- a list of values for each singleton |
_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 |
|
|
|
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
|
|
if any item is not None, the appropriate property will |
|
|
be fixed to that item value. |
|
32 |
""" |
""" |
33 |
|
|
34 |
clazz = Classification() |
clazz = Classification() |
39 |
|
|
40 |
for value in _list: |
for value in _list: |
41 |
prop = ramp.GetProperties(i / maxValue) |
prop = ramp.GetProperties(i / maxValue) |
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
|
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
|
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
|
42 |
clazz.AppendGroup(ClassGroupSingleton(value, prop)) |
clazz.AppendGroup(ClassGroupSingleton(value, prop)) |
43 |
i += 1 |
i += 1 |
44 |
|
|
45 |
return clazz |
return clazz |
46 |
|
|
47 |
def generate_uniform_distribution(min, max, numGroups, ramp, intStep = False, |
def generate_uniform_distribution(min, max, numGroups, ramp, intStep = False): |
|
fixes = (None, None, None)): |
|
48 |
"""Generate a classification with numGroups range groups |
"""Generate a classification with numGroups range groups |
49 |
each with the same interval. |
each with the same interval. |
50 |
|
|
52 |
Useful if the values are integers but the |
Useful if the values are integers but the |
53 |
number of groups specified doesn't evenly |
number of groups specified doesn't evenly |
54 |
divide (max - min). |
divide (max - min). |
|
|
|
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
|
|
if any item is not None, the appropriate property will |
|
|
be fixed to that item value. |
|
55 |
""" |
""" |
56 |
|
|
57 |
clazz = Classification() |
clazz = Classification() |
80 |
else: |
else: |
81 |
_range = Range(("[", cur_min, cur_max, end)) |
_range = Range(("[", cur_min, cur_max, end)) |
82 |
|
|
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
|
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
|
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
|
|
|
|
83 |
clazz.AppendGroup(ClassGroupRange(_range, prop)) |
clazz.AppendGroup(ClassGroupRange(_range, prop)) |
84 |
|
|
85 |
cur_min = cur_max |
cur_min = cur_max |
86 |
|
|
87 |
return clazz |
return clazz |
88 |
|
|
89 |
def generate_quantiles(_list, percents, ramp, _range, fixes=(None, None, None)): |
def generate_quantiles(_list, percents, ramp, _range): |
90 |
"""Generates a Classification which has groups of ranges that |
"""Generates a Classification which has groups of ranges that |
91 |
represent quantiles of _list at the percentages given in percents. |
represent quantiles of _list at the percentages given in percents. |
92 |
Only the values that fall within _range are considered. |
Only the values that fall within _range are considered. |
106 |
|
|
107 |
_range -- a Range object |
_range -- a Range object |
108 |
|
|
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
|
|
if any item is not None, the appropriate property will |
|
|
be fixed to that item value. |
|
|
|
|
109 |
Raises a Value Error if 'percents' has fewer than two items, or |
Raises a Value Error if 'percents' has fewer than two items, or |
110 |
does not cover the entire range. |
does not cover the entire range. |
111 |
""" |
""" |
140 |
else: |
else: |
141 |
max = _list[q] |
max = _list[q] |
142 |
|
|
|
if fixes[0] is not None: prop.SetLineColor(fixes[0]) |
|
|
if fixes[1] is not None: prop.SetLineWidth(fixes[1]) |
|
|
if fixes[2] is not None: prop.SetFill(fixes[2]) |
|
|
|
|
143 |
group = ClassGroupRange(Range((start, min, max, end)), prop) |
group = ClassGroupRange(Range((start, min, max, end)), prop) |
144 |
|
|
145 |
group.SetLabel("%s%% - %s%%" % (round(oldp*100, 2), |
group.SetLabel("%s%% - %s%%" % (round(oldp*100, 2), |
480 |
|
|
481 |
return prop |
return prop |
482 |
|
|
483 |
|
class FixedRamp: |
484 |
|
"""FixedRamp allows particular properties of a ramp to be |
485 |
|
held constant over the ramp. |
486 |
|
""" |
487 |
|
|
488 |
|
def __init__(self, ramp, fixes): |
489 |
|
""" |
490 |
|
ramp -- a source ramp to get the default properties |
491 |
|
|
492 |
|
fixes -- a tuple (lineColor, lineWidth, fillColor) such that |
493 |
|
if any item is not None, the appropriate property will |
494 |
|
be fixed to that item value. |
495 |
|
""" |
496 |
|
|
497 |
|
self.fixes = fixes |
498 |
|
self.ramp = ramp |
499 |
|
|
500 |
|
def GetRamp(self): |
501 |
|
return self |
502 |
|
|
503 |
|
def GetProperties(self, index): |
504 |
|
props = self.ramp.GetProperties(index) |
505 |
|
if self.fixes[0] is not None: props.SetLineColor(self.fixes[0]) |
506 |
|
if self.fixes[1] is not None: props.SetLineWidth(self.fixes[1]) |
507 |
|
if self.fixes[2] is not None: props.SetFill(self.fixes[2]) |
508 |
|
|
509 |
|
return props |