mirror of
https://github.com/mapnik/mapnik.git
synced 2025-12-08 20:13:09 +00:00
added a max_value attribute to color_band to handle the case of the last band more gracefully
This commit is contained in:
parent
58b0a3e5ff
commit
38e1570b8b
@ -48,6 +48,14 @@ namespace {
|
||||
{
|
||||
rc->append_band(v, c, m);
|
||||
}
|
||||
void append_band5(raster_colorizer_ptr & rc, float v, float vm, color c, unsigned m)
|
||||
{
|
||||
rc->append_band(v, vm, c, m);
|
||||
}
|
||||
void append_band6(raster_colorizer_ptr & rc, float v, float vm, color c)
|
||||
{
|
||||
rc->append_band(v, vm, c);
|
||||
}
|
||||
color_bands const& get_color_bands(raster_colorizer_ptr & rc)
|
||||
{
|
||||
return rc->get_color_bands();
|
||||
@ -67,6 +75,8 @@ void export_raster_colorizer()
|
||||
.def("append_band", append_band2, "TODO: Write docs")
|
||||
.def("append_band", append_band3, "TODO: Write docs")
|
||||
.def("append_band", append_band4, "TODO: Write docs")
|
||||
.def("append_band", append_band5, "TODO: Write docs")
|
||||
.def("append_band", append_band6, "TODO: Write docs")
|
||||
.def("get_color", &raster_colorizer::get_color, "TODO: Write docs")
|
||||
;
|
||||
|
||||
@ -83,6 +93,7 @@ void export_raster_colorizer()
|
||||
(&color_band::get_color,
|
||||
return_value_policy<reference_existing_object>()))
|
||||
.add_property("value", &color_band::get_value)
|
||||
.add_property("max_value", &color_band::get_max_value)
|
||||
.def(self == self)
|
||||
.def("__str__",&color_band::to_string)
|
||||
;
|
||||
|
||||
@ -37,11 +37,19 @@ namespace mapnik
|
||||
struct MAPNIK_DECL color_band
|
||||
{
|
||||
float value_;
|
||||
float max_value_;
|
||||
color color_;
|
||||
unsigned midpoints_;
|
||||
bool is_interpolated_;
|
||||
color_band(float value, color c)
|
||||
: value_(value),
|
||||
max_value_(value),
|
||||
color_(c),
|
||||
midpoints_(0),
|
||||
is_interpolated_(false) {}
|
||||
color_band(float value, float max_value, color c)
|
||||
: value_(value),
|
||||
max_value_(max_value),
|
||||
color_(c),
|
||||
midpoints_(0),
|
||||
is_interpolated_(false) {}
|
||||
@ -57,18 +65,22 @@ struct MAPNIK_DECL color_band
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
const float get_max_value() const
|
||||
{
|
||||
return max_value_;
|
||||
}
|
||||
const color& get_color() const
|
||||
{
|
||||
return color_;
|
||||
}
|
||||
bool operator==(color_band const& other) const
|
||||
{
|
||||
return value_ == other.value_ && color_ == other.color_;
|
||||
return value_ == other.value_ && color_ == other.color_ && max_value_ == other.max_value_;
|
||||
}
|
||||
std::string to_string() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << color_.to_string() << " " << value_;
|
||||
ss << color_.to_string() << " " << value_ << " " << max_value_;
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
@ -101,6 +113,8 @@ struct MAPNIK_DECL raster_colorizer
|
||||
);
|
||||
}
|
||||
colors_.push_back(band);
|
||||
if (colors_.size() > 0 && colors_.back().value_ == colors_.back().max_value_)
|
||||
colors_.back().max_value_ = band.value_;
|
||||
}
|
||||
void append_band (color_band band, unsigned midpoints)
|
||||
{
|
||||
@ -141,11 +155,22 @@ struct MAPNIK_DECL raster_colorizer
|
||||
append_band(color_band(value, c));
|
||||
}
|
||||
|
||||
void append_band (float value, float max_value, color c)
|
||||
{
|
||||
append_band(color_band(value, max_value, c));
|
||||
}
|
||||
|
||||
void append_band (float value, color c, unsigned midpoints)
|
||||
{
|
||||
append_band(color_band(value, c), midpoints);
|
||||
}
|
||||
|
||||
void append_band (float value, float max_value, color c, unsigned midpoints)
|
||||
{
|
||||
append_band(color_band(value, max_value, c), midpoints);
|
||||
}
|
||||
|
||||
|
||||
/* rgba =
|
||||
* if cs[pos].value <= value < cs[pos+1].value: cs[pos].color
|
||||
* otherwise: transparent
|
||||
@ -168,7 +193,7 @@ struct MAPNIK_DECL raster_colorizer
|
||||
}
|
||||
lo--;
|
||||
if ((0 <= lo && lo < last) ||
|
||||
(lo==last && colors_[last].value_==value))
|
||||
(lo==last && (colors_[last].value_==value || value<colors_[last].max_value_)))
|
||||
return colors_[lo].color_;
|
||||
else
|
||||
return color(0,0,0,0);
|
||||
|
||||
@ -1605,7 +1605,12 @@ void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc,
|
||||
throw config_error("missing color");
|
||||
}
|
||||
unsigned midpoints = get_attr(cb, "midpoints", 0);
|
||||
rc->append_band(value, *c, midpoints);
|
||||
optional<float> max_value = get_opt_attr<float>(cb, "max_value");
|
||||
if (max_value) {
|
||||
rc->append_band(value, *max_value, *c, midpoints);
|
||||
} else {
|
||||
rc->append_band(value, *c, midpoints);
|
||||
}
|
||||
}
|
||||
else if (cb_tag.first != "<xmlcomment>" &&
|
||||
cb_tag.first != "<xmlattr>" )
|
||||
|
||||
@ -59,6 +59,8 @@ void serialize_raster_colorizer(ptree & sym_node,
|
||||
ptree::value_type("ColorBand", ptree())
|
||||
)->second;
|
||||
set_attr(band_node, "value", cb[i].get_value());
|
||||
if (cb[i].get_value() != cb[i].get_max_value())
|
||||
set_attr(band_node, "max_value", cb[i].get_max_value());
|
||||
set_attr(band_node, "midpoints", cb[i].get_midpoints());
|
||||
optional<color> c = cb[i].get_color();
|
||||
if (c) set_attr(band_node, "color", * c);
|
||||
|
||||
@ -30,3 +30,17 @@ def test_get_color():
|
||||
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"))
|
||||
|
||||
def test_get_color_with_max_value():
|
||||
colorizer = mapnik2.RasterColorizer()
|
||||
c1 = mapnik2.Color("#0044cc")
|
||||
colorizer.append_band(0, c1)
|
||||
c2 = mapnik2.Color("#0055dd")
|
||||
colorizer.append_band(1, 2, c2)
|
||||
|
||||
eq_(colorizer.get_color(-1), mapnik2.Color("transparent"))
|
||||
eq_(colorizer.get_color(0), c1)
|
||||
eq_(colorizer.get_color(0.5), c1)
|
||||
eq_(colorizer.get_color(1), c2)
|
||||
eq_(colorizer.get_color(1.5), c2)
|
||||
eq_(colorizer.get_color(2), mapnik2.Color("transparent"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user