Add --accumulate-attribute to tippecanoe-overzoom (#189)

* Starting to factor out attribute accumulation into its own file

* Continuing to factor out attribute accumulation

* Reduce duplicate code

* Plumbing the accumulate-attribute option around

* Call the attribute accumulator

* Test that accumulation works

* Add missing #includes

* Don't sort within individual multiplier clusters

Doing so throws off the spatial distribution of the low zooms

* Docs and changelog

* Add comments
This commit is contained in:
Erica Fischer
2024-01-23 15:41:36 -08:00
committed by GitHub
parent f957f30f90
commit cbf222754b
17 changed files with 493 additions and 431 deletions
+6 -78
View File
@@ -43,6 +43,7 @@
#include "errors.hpp"
#include "compression.hpp"
#include "protozero/varint.hpp"
#include "attribute.hpp"
extern "C" {
#include "jsonpull/jsonpull.h"
@@ -507,11 +508,6 @@ void rewrite(drawvec &geom, int z, int nextzoom, int maxzoom, long long *bbox, u
}
}
struct accum_state {
double sum = 0;
double count = 0;
};
struct partial {
std::vector<drawvec> geoms = std::vector<drawvec>();
std::vector<long long> keys = std::vector<long long>();
@@ -1825,7 +1821,7 @@ void add_tilestats(std::string const &layername, int z, std::vector<std::map<std
add_to_tilestats(ts->second.tilestats, key, val);
}
void preserve_attribute(attribute_op op, serial_feature &, char *stringpool, long long *pool_off, std::string &key, serial_val &val, partial &p) {
void promote_attribute(std::string const &key, partial &p, char *stringpool, long long *pool_off) {
if (p.need_tilestats.count(key) == 0) {
p.need_tilestats.insert(key);
}
@@ -1848,70 +1844,6 @@ void preserve_attribute(attribute_op op, serial_feature &, char *stringpool, lon
break;
}
}
for (size_t i = 0; i < p.full_keys.size(); i++) {
if (key == p.full_keys[i]) {
switch (op) {
case op_sum:
p.full_values[i].s = milo::dtoa_milo(atof(p.full_values[i].s.c_str()) + atof(val.s.c_str()));
p.full_values[i].type = mvt_double;
break;
case op_product:
p.full_values[i].s = milo::dtoa_milo(atof(p.full_values[i].s.c_str()) * atof(val.s.c_str()));
p.full_values[i].type = mvt_double;
break;
case op_max: {
double existing = atof(p.full_values[i].s.c_str());
double maybe = atof(val.s.c_str());
if (maybe > existing) {
p.full_values[i].s = val.s.c_str();
p.full_values[i].type = mvt_double;
}
break;
}
case op_min: {
double existing = atof(p.full_values[i].s.c_str());
double maybe = atof(val.s.c_str());
if (maybe < existing) {
p.full_values[i].s = val.s.c_str();
p.full_values[i].type = mvt_double;
}
break;
}
case op_mean: {
auto state = p.attribute_accum_state.find(key);
if (state == p.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;
p.attribute_accum_state.insert(std::pair<std::string, accum_state>(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;
break;
case op_comma:
p.full_values[i].s += std::string(",") + val.s;
p.full_values[i].type = mvt_string;
break;
}
}
}
}
void preserve_attributes(std::map<std::string, attribute_op> const *attribute_accum, serial_feature &sf, char *stringpool, long long *pool_off, partial &p) {
@@ -1924,7 +1856,8 @@ void preserve_attributes(std::map<std::string, attribute_op> const *attribute_ac
auto f = attribute_accum->find(key);
if (f != attribute_accum->end()) {
preserve_attribute(f->second, sf, stringpool, pool_off, key, sv, p);
promote_attribute(key, p, stringpool, pool_off);
preserve_attribute(f->second, key, sv, p.full_keys, p.full_values, p.attribute_accum_state);
}
}
for (size_t i = 0; i < sf.full_keys.size(); i++) {
@@ -1933,7 +1866,8 @@ void preserve_attributes(std::map<std::string, attribute_op> const *attribute_ac
auto f = attribute_accum->find(key);
if (f != attribute_accum->end()) {
preserve_attribute(f->second, sf, stringpool, pool_off, key, sv, p);
promote_attribute(key, p, stringpool, pool_off);
preserve_attribute(f->second, key, sv, p.full_keys, p.full_values, p.attribute_accum_state);
}
}
}
@@ -2721,18 +2655,12 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
if (prevent[P_INPUT_ORDER]) {
auto clustered = assemble_multiplier_clusters(layer_features);
for (auto &c : clustered) {
std::sort(c.begin(), c.end());
}
std::sort(clustered.begin(), clustered.end(), preservecmp);
layer_features = disassemble_multiplier_clusters(clustered);
}
if (order_by.size() != 0) {
auto clustered = assemble_multiplier_clusters(layer_features);
for (auto &c : clustered) {
std::sort(c.begin(), c.end());
}
std::sort(clustered.begin(), clustered.end(), ordercmp);
layer_features = disassemble_multiplier_clusters(clustered);
}