From 91d4621caa0b2fb9c3025550d24c419270666357 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 28 Sep 2016 17:12:59 -0700 Subject: [PATCH] Also handle nested feature attributes in tile-join --- tile-join.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/tile-join.cpp b/tile-join.cpp index 29bd001..10ff78c 100644 --- a/tile-join.cpp +++ b/tile-join.cpp @@ -29,6 +29,70 @@ struct stats { double minlat, minlon, maxlat, maxlon; }; +size_t tag_key(mvt_layer &layer, std::string const &key) { + size_t ko; + + std::map::iterator ki = layer.key_map.find(key); + + if (ki == layer.key_map.end()) { + ko = layer.keys.size(); + layer.keys.push_back(key); + layer.key_map.insert(std::pair(key, ko)); + } else { + ko = ki->second; + } + + return ko; +} + +size_t tag_value(mvt_layer &layer, mvt_value &value) { + size_t vo; + + std::map::iterator vi = layer.value_map.find(value); + + if (vi == layer.value_map.end()) { + vo = layer.values.size(); + layer.values.push_back(value); + layer.value_map.insert(std::pair(value, vo)); + } else { + vo = vi->second; + } + + return vo; +} + +size_t tag_object(mvt_layer const &layer, mvt_value const &val, mvt_layer &outlayer) { + mvt_value tv; + + if (val.type == mvt_hash) { + tv.type = mvt_hash; + tv.list_value = std::vector(); + + for (size_t i = 0; i + 1 < val.list_value.size(); i += 2) { + tv.list_value.push_back(tag_key(outlayer, layer.keys[val.list_value[i]])); + tv.list_value.push_back(tag_object(layer, layer.values[val.list_value[i + 1]], outlayer)); + } + } else if (val.type == mvt_list) { + tv.type = mvt_list; + tv.list_value = std::vector(); + + for (size_t i = 0; i < val.list_value.size(); i++) { + tv.list_value.push_back(tag_object(layer, layer.values[val.list_value[i]], outlayer)); + } + } else { + tv = val; + } + + return tag_value(outlayer, tv); +} + +void copy_nested(mvt_layer &layer, mvt_feature &feature, std::string key, mvt_value &val, mvt_layer &outlayer, mvt_feature &outfeature) { + size_t ko = tag_key(outlayer, key); + size_t vo = tag_object(layer, val, outlayer); + outfeature.tags.push_back(ko); + outfeature.tags.push_back(vo); +} + void handle(std::string message, int z, unsigned x, unsigned y, std::map &layermap, std::vector &header, std::map> &mapping, std::set &exclude, int ifmatched, mvt_tile &outtile) { mvt_tile tile; int features_added = 0; @@ -116,6 +180,8 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::mapsecond.file_keys.insert(tas); - outlayer.tag(outfeature, layer.keys[feat.tags[t]], val); + + if (type == VT_OBJECT) { + copy_nested(layer, feat, layer.keys[feat.tags[t]], val, outlayer, outfeature); + } else { + outlayer.tag(outfeature, layer.keys[feat.tags[t]], val); + } } if (header.size() > 0 && strcmp(key, header[0].c_str()) == 0) {