From d66d05213f2a13e36651a12ee4a3b6d913348be4 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Tue, 27 Feb 2018 16:12:29 -0800 Subject: [PATCH] Add the mean as an option for accumulating attributes --- tile.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tile.cpp b/tile.cpp index 4d79e75..d0cbf0c 100644 --- a/tile.cpp +++ b/tile.cpp @@ -1463,7 +1463,7 @@ struct accum_state { double count = 0; }; -void preserve_attribute(attribute_op op, std::map &attribute_accum_state, serial_feature &sf, char *stringpool, std::string &key, serial_val &val, partial &p) { +void preserve_attribute(attribute_op op, std::map &attribute_accum_state, char *stringpool, std::string &key, serial_val &val, partial &p) { if (p.need_tilestats.count(key) == 0) { p.need_tilestats.insert(key); } @@ -1523,6 +1523,25 @@ void preserve_attribute(attribute_op op, std::map &att } break; + case op_mean: + { + auto state = attribute_accum_state.find(key); + if (state == attribute_accum_state.end()) { + accum_state s; + s.sum = atof(p.full_values[i].s.c_str()) + atof(val.s.c_str()); + s.count = 2; + attribute_accum_state.insert(std::pair(key, s)); + + p.full_values[i].s = milo::dtoa_milo(s.sum / s.count); + } else { + state->second.sum += atof(val.s.c_str()); + state->second.count += 1; + + p.full_values[i].s = milo::dtoa_milo(state->second.sum / state->second.count); + } + } + break; + case op_concat: p.full_values[i].s += val.s; p.full_values[i].type = mvt_string; @@ -1547,7 +1566,7 @@ void preserve_attributes(std::map const *attribute_ac auto f = attribute_accum->find(key); if (f != attribute_accum->end()) { - preserve_attribute(f->second, attribute_accum_state, sf, stringpool, key, sv, p); + preserve_attribute(f->second, attribute_accum_state, stringpool, key, sv, p); } } for (size_t i = 0; i < sf.full_keys.size(); i++) { @@ -1556,7 +1575,7 @@ void preserve_attributes(std::map const *attribute_ac auto f = attribute_accum->find(key); if (f != attribute_accum->end()) { - preserve_attribute(f->second, attribute_accum_state, sf, stringpool, key, sv, p); + preserve_attribute(f->second, attribute_accum_state, stringpool, key, sv, p); } } }