diff --git a/Makefile b/Makefile index a14e7fd4..3a746508 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,7 @@ tippecanoe-enumerate: enumerate.o tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o jsonpull/jsonpull.o dirtiles.o pmtiles_file.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o +tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o attribute.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o diff --git a/clip.cpp b/clip.cpp index 26ea66c0..aa4b7339 100644 --- a/clip.cpp +++ b/clip.cpp @@ -785,28 +785,61 @@ struct tile_feature { size_t seq = 0; }; -static void feature_out(tile_feature const &feature, mvt_layer &outlayer, std::set const &keep) { +static void feature_out(std::vector const &features, mvt_layer &outlayer, std::set const &keep, std::map const &attribute_accum) { // Add geometry to output feature mvt_feature outfeature; - outfeature.type = feature.t; - for (auto const &g : feature.geom) { + outfeature.type = features[0].t; + for (auto const &g : features[0].geom) { outfeature.geometry.emplace_back(g.op, g.x, g.y); } // ID and attributes, if it didn't get clipped away if (outfeature.geometry.size() > 0) { - if (feature.has_id) { + if (features[0].has_id) { outfeature.has_id = true; - outfeature.id = feature.id; + outfeature.id = features[0].id; } - outfeature.seq = feature.seq; + outfeature.seq = features[0].seq; - for (size_t i = 0; i + 1 < feature.tags.size(); i += 2) { - if (keep.size() == 0 || keep.find(feature.layer->keys[feature.tags[i]]) != keep.end()) { - outlayer.tag(outfeature, feature.layer->keys[feature.tags[i]], feature.layer->values[feature.tags[i + 1]]); + if (attribute_accum.size() > 0) { + // accumulate whatever attributes are specified to be accumulated + // onto the feature that will survive into the output, from the + // features that will not + + std::map attribute_accum_state; + std::vector full_keys; + std::vector full_values; + + for (size_t i = 0; i + 1 < features[0].tags.size(); i += 2) { + full_keys.push_back(features[0].layer->keys[features[0].tags[i]]); + full_values.push_back(mvt_value_to_serial_val(features[0].layer->values[features[0].tags[i + 1]])); + } + + for (size_t i = 1; i < features.size(); i++) { + for (size_t j = 0; j + 1 < features[i].tags.size(); j += 2) { + std::string key = features[i].layer->keys[features[i].tags[j]]; + + auto f = attribute_accum.find(key); + if (f != attribute_accum.end()) { + serial_val val = mvt_value_to_serial_val(features[i].layer->values[features[i].tags[j + 1]]); + preserve_attribute(f->second, key, val, full_keys, full_values, attribute_accum_state); + } + } + } + + for (size_t i = 0; i < full_keys.size(); i++) { + if (keep.size() == 0 || keep.find(full_keys[i]) != keep.end()) { + outlayer.tag(outfeature, full_keys[i], stringified_to_mvt_value(full_values[i].type, full_values[i].s.c_str())); + } + } + } else { + for (size_t i = 0; i + 1 < features[0].tags.size(); i += 2) { + if (keep.size() == 0 || keep.find(features[0].layer->keys[features[0].tags[i]]) != keep.end()) { + outlayer.tag(outfeature, features[0].layer->keys[features[0].tags[i]], features[0].layer->values[features[0].tags[i + 1]]); + } } } @@ -864,7 +897,7 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int if (flush_multiplier_cluster) { if (pending_tile_features.size() > 0) { - feature_out(pending_tile_features[0], outlayer, keep); + feature_out(pending_tile_features, outlayer, keep, attribute_accum); pending_tile_features.clear(); } } @@ -959,7 +992,7 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int } if (pending_tile_features.size() > 0) { - feature_out(pending_tile_features[0], outlayer, keep); + feature_out(pending_tile_features, outlayer, keep, attribute_accum); pending_tile_features.clear(); }