Skip to content

Commit 0d281d3

Browse files
committed
Add an all-mvt_value attribute accumulation path
1 parent 28efc40 commit 0d281d3

File tree

4 files changed

+143
-29
lines changed

4 files changed

+143
-29
lines changed

attribute.cpp

+108
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,111 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
207207
full_keys.push_back(key);
208208
full_values.push_back(sv);
209209
}
210+
211+
void preserve_attribute(attribute_op const &op, std::string const &key, mvt_value const &val, std::vector<std::string> &full_keys, std::vector<mvt_value> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
212+
for (size_t i = 0; i < full_keys.size(); i++) {
213+
if (key == full_keys[i]) {
214+
switch (op) {
215+
case op_sum:
216+
full_values[i] = mvt_value(full_values[i].to_double() + val.to_double());
217+
return;
218+
219+
case op_product:
220+
full_values[i] = mvt_value_to_double(full_values[i].to_double() * val.to_double());
221+
return;
222+
223+
case op_max: {
224+
double existing = full_values[i].to_double();
225+
double maybe = val.to_double();
226+
if (maybe > existing) {
227+
full_values[i] = val;
228+
}
229+
return;
230+
}
231+
232+
case op_min: {
233+
double existing = full_values[i].to_double();
234+
double maybe = val.to_double();
235+
if (maybe < existing) {
236+
full_values[i] = val;
237+
}
238+
return;
239+
}
240+
241+
case op_mean: {
242+
auto state = attribute_accum_state.find(key);
243+
if (state == attribute_accum_state.end()) {
244+
accum_state s;
245+
s.sum = full_values[i].to_double() + val.to_double();
246+
s.count = 2;
247+
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
248+
249+
full_values[i] = mvt_value(s.sum / s.count);
250+
} else {
251+
state->second.sum += val.to_double();
252+
state->second.count += 1;
253+
254+
full_values[i] = mvt_value(state->second.sum / state->second.count);
255+
}
256+
return;
257+
}
258+
259+
case op_concat:
260+
full_values[i].set_string_value(full_values[i].toString() + val.toString());
261+
return;
262+
263+
case op_comma:
264+
full_values[i].set_string_value(full_values[i].toString() + "," + val.toString());
265+
return;
266+
267+
case op_count: {
268+
auto state = attribute_accum_state.find(key);
269+
if (state == attribute_accum_state.end()) { // not already present
270+
accum_state s;
271+
s.count = 2;
272+
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
273+
274+
full_values[i] = mvt_value(s.count);
275+
} else { // already present, incrementing
276+
state->second.count += 1;
277+
full_values[i] = mvt_value(state->second.count);
278+
}
279+
return;
280+
}
281+
}
282+
}
283+
}
284+
285+
// not found, so we are making a new value
286+
287+
mvt_value v;
288+
switch (op) {
289+
case op_sum:
290+
case op_max:
291+
case op_min:
292+
v = val;
293+
break;
294+
295+
case op_count: {
296+
auto state = attribute_accum_state.find(key);
297+
if (state == attribute_accum_state.end()) { // not already present
298+
accum_state s;
299+
s.count = 1;
300+
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
301+
302+
v = mvt_value(s.count);
303+
} else { // already present, incrementing
304+
fprintf(stderr, "preserve_attribute: can't happen (count)\n");
305+
exit(EXIT_IMPOSSIBLE);
306+
}
307+
break;
308+
}
309+
310+
default:
311+
fprintf(stderr, "can't happen: operation that isn't used by --accumulate-numeric-attributes\n");
312+
exit(EXIT_IMPOSSIBLE);
313+
}
314+
315+
full_keys.push_back(key);
316+
full_values.push_back(v);
317+
}

attribute.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <unordered_map>
66
#include <map>
7+
#include "mvt.hpp"
78

89
enum attribute_op {
910
op_sum,
@@ -26,6 +27,7 @@ struct serial_val;
2627
void set_attribute_accum(std::unordered_map<std::string, attribute_op> &attribute_accum, std::string name, std::string type);
2728
void set_attribute_accum(std::unordered_map<std::string, attribute_op> &attribute_accum, const char *arg, char **argv);
2829
void preserve_attribute(attribute_op const &op, const std::string &key, serial_val const &val, std::vector<std::string> &full_keys, std::vector<serial_val> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state);
30+
void preserve_attribute(attribute_op const &op, std::string const &key, mvt_value const &val, std::vector<std::string> &full_keys, std::vector<mvt_value> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state);
2931

3032
extern std::map<std::string, attribute_op> numeric_operations;
3133

clip.cpp

+21-28
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ static void add_mean(mvt_feature &feature, mvt_layer &layer, std::string const &
11651165
// accumulate :sum:, :min:, :max:, and :count: versions of the specified attribute
11661166
static void preserve_numeric(const std::string &key, const mvt_value &val, // numeric attribute being accumulated
11671167
std::vector<std::string> &full_keys, // keys of feature being accumulated onto
1168-
std::vector<serial_val> &full_values, // values of features being accumulated onto
1168+
std::vector<mvt_value> &full_values, // values of features being accumulated onto
11691169
const std::string &accumulate_numeric, // prefix of accumulations
11701170
std::set<std::string> &keys, // key presence in the source feature
11711171
std::map<std::string, size_t> &numeric_out_field, // key index in the output feature
@@ -1218,49 +1218,45 @@ static void preserve_numeric(const std::string &key, const mvt_value &val, /
12181218
if (op.second == op_count) {
12191219
if (starting_from_accumulation) {
12201220
// copy our count
1221-
full_values.push_back(mvt_value_to_serial_val(val));
1221+
full_values.push_back(val);
12221222
} else {
12231223
// new count of 1
1224-
serial_val sv;
1225-
sv.type = mvt_double;
1226-
sv.s = "1";
1227-
full_values.push_back(sv);
1224+
full_values.push_back(mvt_value(1));
12281225
}
12291226
} else {
1230-
full_values.push_back(mvt_value_to_serial_val(val));
1227+
full_values.push_back(val);
12311228
}
12321229
} else {
12331230
// exists unprefixed, so copy it, and then accumulate on our value
12341231
numeric_out_field.emplace(prefixed, full_keys.size());
12351232
full_keys.push_back(prefixed);
12361233

12371234
if (op.second == op_count) {
1238-
serial_val sv;
1239-
sv.type = mvt_double;
1235+
mvt_value v;
12401236
if (starting_from_accumulation) {
12411237
// sum our count onto the existing 1
1242-
sv.s = std::to_string(1 + mvt_value_to_long_long(val));
1238+
v = mvt_value(1 + mvt_value_to_long_long(val));
12431239
} else {
12441240
// sum our 1 onto the existing 1
1245-
sv.s = "2";
1241+
v = mvt_value(2);
12461242
}
1247-
full_values.push_back(sv);
1243+
full_values.push_back(v);
12481244
} else {
12491245
full_values.push_back(full_values[out_attr->second]);
1250-
preserve_attribute(op.second, prefixed, mvt_value_to_serial_val(val), full_keys, full_values, attribute_accum_state);
1246+
preserve_attribute(op.second, prefixed, val, full_keys, full_values, attribute_accum_state);
12511247
}
12521248
}
12531249
} else {
12541250
// exists, so accumulate on our value
12551251
if (op.second == op_count) {
12561252
if (starting_from_accumulation) {
12571253
// sum our count onto the existing count
1258-
full_values[prefixed_attr->second].s = std::to_string(atoll(full_values[prefixed_attr->second].s.c_str()) + mvt_value_to_long_long(val));
1254+
full_values[prefixed_attr->second] = mvt_value(mvt_value_to_long_long(full_values[prefixed_attr->second]) + mvt_value_to_long_long(val));
12591255
} else {
1260-
full_values[prefixed_attr->second].s = std::to_string(atoll(full_values[prefixed_attr->second].s.c_str()) + 1);
1256+
full_values[prefixed_attr->second] = mvt_value(mvt_value_to_long_long(full_values[prefixed_attr->second]) + 1);
12611257
}
12621258
} else {
1263-
preserve_attribute(op.second, prefixed, mvt_value_to_serial_val(val), full_keys, full_values, attribute_accum_state);
1259+
preserve_attribute(op.second, prefixed, val, full_keys, full_values, attribute_accum_state);
12641260
}
12651261
}
12661262
}
@@ -1293,7 +1289,6 @@ static void feature_out(std::vector<tile_feature> const &features, mvt_layer &ou
12931289
std::set<std::string> const &exclude,
12941290
std::vector<std::string> const &exclude_prefix,
12951291
std::unordered_map<std::string, attribute_op> const &attribute_accum,
1296-
std::shared_ptr<std::string> const &tile_stringpool,
12971292
std::string const &accumulate_numeric) {
12981293
// Add geometry to output feature
12991294

@@ -1315,13 +1310,13 @@ static void feature_out(std::vector<tile_feature> const &features, mvt_layer &ou
13151310

13161311
if (attribute_accum.size() > 0 || accumulate_numeric.size() > 0) {
13171312
// convert the attributes of the output feature
1318-
// from mvt_value to serial_val so they can have
1313+
// from layer references to a vector so they can have
13191314
// attributes from the other features of the
13201315
// multiplier cluster accumulated onto them
13211316

13221317
std::unordered_map<std::string, accum_state> attribute_accum_state;
13231318
std::vector<std::string> full_keys;
1324-
std::vector<serial_val> full_values;
1319+
std::vector<mvt_value> full_values;
13251320
std::map<std::string, size_t> numeric_out_field;
13261321

13271322
for (size_t i = 0; i + 1 < features[0].tags.size(); i += 2) {
@@ -1330,12 +1325,12 @@ static void feature_out(std::vector<tile_feature> const &features, mvt_layer &ou
13301325
if (f != attribute_accum.end()) {
13311326
// this attribute has an accumulator, so convert it
13321327
full_keys.push_back(features[0].layer->keys[features[0].tags[i]]);
1333-
full_values.push_back(mvt_value_to_serial_val(features[0].layer->values[features[0].tags[i + 1]]));
1328+
full_values.push_back(features[0].layer->values[features[0].tags[i + 1]]);
13341329
} else if (accumulate_numeric.size() > 0 && features[0].layer->values[features[0].tags[i + 1]].is_numeric()) {
13351330
// convert numeric for accumulation
13361331
numeric_out_field.emplace(key, full_keys.size());
13371332
full_keys.push_back(key);
1338-
full_values.push_back(mvt_value_to_serial_val(features[0].layer->values[features[0].tags[i + 1]]));
1333+
full_values.push_back(features[0].layer->values[features[0].tags[i + 1]]);
13391334
} else {
13401335
// otherwise just tag it directly onto the output feature
13411336
if (should_keep(features[0].layer->keys[features[0].tags[i]], keep, exclude, exclude_prefix)) {
@@ -1361,7 +1356,7 @@ static void feature_out(std::vector<tile_feature> const &features, mvt_layer &ou
13611356

13621357
auto f = attribute_accum.find(key);
13631358
if (f != attribute_accum.end()) {
1364-
serial_val val = mvt_value_to_serial_val(features[i].layer->values[features[i].tags[j + 1]]);
1359+
mvt_value val = features[i].layer->values[features[i].tags[j + 1]];
13651360
preserve_attribute(f->second, key, val, full_keys, full_values, attribute_accum_state);
13661361
} else if (accumulate_numeric.size() > 0) {
13671362
const mvt_value &val = features[i].layer->values[features[i].tags[j + 1]];
@@ -1379,7 +1374,7 @@ static void feature_out(std::vector<tile_feature> const &features, mvt_layer &ou
13791374

13801375
for (size_t i = 0; i < full_keys.size(); i++) {
13811376
if (should_keep(full_keys[i], keep, exclude, exclude_prefix)) {
1382-
outlayer.tag(outfeature, full_keys[i], stringified_to_mvt_value(full_values[i].type, full_values[i].s.c_str(), tile_stringpool));
1377+
outlayer.tag(outfeature, full_keys[i], full_values[i]);
13831378
}
13841379
}
13851380

@@ -1570,7 +1565,6 @@ mvt_tile assign_to_bins(mvt_tile &features,
15701565
outlayer.name = features.layers[0].name;
15711566

15721567
std::vector<std::vector<tile_feature>> outfeatures;
1573-
std::shared_ptr<std::string> tile_stringpool = std::make_shared<std::string>();
15741568

15751569
for (auto &e : events) {
15761570
if (e.kind == index_event::ENTER) {
@@ -1680,7 +1674,7 @@ mvt_tile assign_to_bins(mvt_tile &features,
16801674
if (outfeatures[i].size() > 1) {
16811675
feature_out(outfeatures[i], outlayer,
16821676
keep, exclude, exclude_prefix, attribute_accum,
1683-
tile_stringpool, accumulate_numeric);
1677+
accumulate_numeric);
16841678
mvt_feature &nfeature = outlayer.features.back();
16851679
mvt_value val;
16861680
val.type = mvt_uint;
@@ -1715,7 +1709,6 @@ std::string overzoom(std::vector<source_tile> const &tiles, int nz, int nx, int
17151709
std::vector<mvt_layer> const &bins, std::string const &bin_by_id_list,
17161710
std::string const &accumulate_numeric) {
17171711
mvt_tile outtile;
1718-
std::shared_ptr<std::string> tile_stringpool = std::make_shared<std::string>();
17191712

17201713
for (auto const &tile : tiles) {
17211714
for (auto const &layer : tile.tile.layers) {
@@ -1840,7 +1833,7 @@ std::string overzoom(std::vector<source_tile> const &tiles, int nz, int nx, int
18401833

18411834
if (flush_multiplier_cluster) {
18421835
if (pending_tile_features.size() > 0) {
1843-
feature_out(pending_tile_features, *outlayer, keep, exclude, exclude_prefix, attribute_accum, tile_stringpool, accumulate_numeric);
1836+
feature_out(pending_tile_features, *outlayer, keep, exclude, exclude_prefix, attribute_accum, accumulate_numeric);
18441837
pending_tile_features.clear();
18451838
}
18461839
}
@@ -1897,7 +1890,7 @@ std::string overzoom(std::vector<source_tile> const &tiles, int nz, int nx, int
18971890
}
18981891

18991892
if (pending_tile_features.size() > 0) {
1900-
feature_out(pending_tile_features, *outlayer, keep, exclude, exclude_prefix, attribute_accum, tile_stringpool, accumulate_numeric);
1893+
feature_out(pending_tile_features, *outlayer, keep, exclude, exclude_prefix, attribute_accum, accumulate_numeric);
19011894
pending_tile_features.clear();
19021895
}
19031896

mvt.hpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ enum mvt_value_type {
7676
mvt_no_such_key,
7777
};
7878

79+
struct mvt_value;
80+
double mvt_value_to_double(mvt_value const &v);
81+
7982
struct mvt_value {
8083
mvt_value_type type;
8184
std::shared_ptr<std::string> s;
@@ -126,6 +129,10 @@ struct mvt_value {
126129
type == mvt_sint;
127130
}
128131

132+
double to_double() const {
133+
return mvt_value_to_double(*this);
134+
}
135+
129136
bool operator<(const mvt_value &o) const;
130137
bool operator==(const mvt_value &o) const;
131138
std::string toString() const;
@@ -134,6 +141,11 @@ struct mvt_value {
134141
this->type = mvt_double;
135142
this->numeric_value.double_value = 0;
136143
}
144+
145+
mvt_value(double v) {
146+
this->type = mvt_double;
147+
this->numeric_value.double_value = v;
148+
}
137149
};
138150

139151
template <>
@@ -201,7 +213,6 @@ int dezig(unsigned n);
201213

202214
mvt_value stringified_to_mvt_value(int type, const char *s, std::shared_ptr<std::string> const &tile_stringpool);
203215
long long mvt_value_to_long_long(mvt_value const &v);
204-
double mvt_value_to_double(mvt_value const &v);
205216

206217
bool is_integer(const char *s, long long *v);
207218
bool is_unsigned_integer(const char *s, unsigned long long *v);

0 commit comments

Comments
 (0)