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:
Alberto Valverde 2010-03-12 13:34:32 +00:00
parent 2f1d60b666
commit a7ea07ee0d
2 changed files with 12 additions and 7 deletions

View file

@ -153,9 +153,11 @@ namespace mapnik
* if cs[pos].value <= value < cs[pos+1].value: cs[pos].color
* otherwise: transparent
* 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 {
int pos=-1, lo=0, hi=colors_.size()-1;
int pos=-1, last=(int)colors_.size()-1, lo=0, hi=last;
while (lo<=hi) {
pos = (lo+hi)/2;
if (colors_[pos].value_<value) {
@ -168,9 +170,11 @@ namespace mapnik
}
}
lo--;
return (0 <= lo && lo < (int)colors_.size()-1)?
colors_[lo].color_:
color(0,0,0,0);
if ((0 <= lo && lo < last) ||
(lo==last && colors_[last].value_==value))
return colors_[lo].color_;
else
return color(0,0,0,0);
}
void colorize(mapnik::raster_ptr const& raster) const {

View file

@ -18,8 +18,7 @@ class test_raster_colorizer():
( 70, "#cc00cc"),
( 80, "#990099"),
( 90, "#660066"),
( 200, "#00000"), # last band denotes upper limit, values above it will
# not return the color
( 200, "#ffffff"),
]]
for value, color in bands:
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(5), bands[0][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"))