modified raster_colorizer so the color of the last band is used if the value matches its value exactly. This is to make declaring legends for rasters with discrete values easier (ie: no need to define a dummy band for N+1)
This commit is contained in:
parent
2f1d60b666
commit
a7ea07ee0d
2 changed files with 12 additions and 7 deletions
|
@ -153,9 +153,11 @@ namespace mapnik
|
||||||
* if cs[pos].value <= value < cs[pos+1].value: cs[pos].color
|
* if cs[pos].value <= value < cs[pos+1].value: cs[pos].color
|
||||||
* otherwise: transparent
|
* otherwise: transparent
|
||||||
* where 0 <= pos < length(bands)-1
|
* where 0 <= pos < length(bands)-1
|
||||||
|
* Last band is special, its value represents the upper bound and its
|
||||||
|
* color will only be used if the value matches its value exactly.
|
||||||
*/
|
*/
|
||||||
color get_color(float value) const {
|
color get_color(float value) const {
|
||||||
int pos=-1, lo=0, hi=colors_.size()-1;
|
int pos=-1, last=(int)colors_.size()-1, lo=0, hi=last;
|
||||||
while (lo<=hi) {
|
while (lo<=hi) {
|
||||||
pos = (lo+hi)/2;
|
pos = (lo+hi)/2;
|
||||||
if (colors_[pos].value_<value) {
|
if (colors_[pos].value_<value) {
|
||||||
|
@ -168,9 +170,11 @@ namespace mapnik
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lo--;
|
lo--;
|
||||||
return (0 <= lo && lo < (int)colors_.size()-1)?
|
if ((0 <= lo && lo < last) ||
|
||||||
colors_[lo].color_:
|
(lo==last && colors_[last].value_==value))
|
||||||
color(0,0,0,0);
|
return colors_[lo].color_;
|
||||||
|
else
|
||||||
|
return color(0,0,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void colorize(mapnik::raster_ptr const& raster) const {
|
void colorize(mapnik::raster_ptr const& raster) const {
|
||||||
|
|
|
@ -18,8 +18,7 @@ class test_raster_colorizer():
|
||||||
( 70, "#cc00cc"),
|
( 70, "#cc00cc"),
|
||||||
( 80, "#990099"),
|
( 80, "#990099"),
|
||||||
( 90, "#660066"),
|
( 90, "#660066"),
|
||||||
( 200, "#00000"), # last band denotes upper limit, values above it will
|
( 200, "#ffffff"),
|
||||||
# not return the color
|
|
||||||
]]
|
]]
|
||||||
for value, color in bands:
|
for value, color in bands:
|
||||||
colorizer.append_band(value, color)
|
colorizer.append_band(value, color)
|
||||||
|
@ -28,5 +27,7 @@ class test_raster_colorizer():
|
||||||
eq_(colorizer.get_color(0), bands[0][1])
|
eq_(colorizer.get_color(0), bands[0][1])
|
||||||
eq_(colorizer.get_color(5), bands[0][1])
|
eq_(colorizer.get_color(5), bands[0][1])
|
||||||
eq_(colorizer.get_color(10), bands[1][1])
|
eq_(colorizer.get_color(10), bands[1][1])
|
||||||
eq_(colorizer.get_color(200), mapnik2.Color("transparent"))
|
# last value is used if it matches exactly
|
||||||
|
eq_(colorizer.get_color(200), bands[-1][1])
|
||||||
|
# values greater than the last value are mapped to "transparent"
|
||||||
eq_(colorizer.get_color(201), mapnik2.Color("transparent"))
|
eq_(colorizer.get_color(201), mapnik2.Color("transparent"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue