diff --git a/src/mbgl/map/vector_tile.cpp b/src/mbgl/map/vector_tile.cpp index e0f6c2d73..bf10f8706 100644 --- a/src/mbgl/map/vector_tile.cpp +++ b/src/mbgl/map/vector_tile.cpp @@ -29,7 +29,8 @@ Value parseValue(pbf data) { return false; } -VectorTileFeature::VectorTileFeature(pbf feature_pbf, const VectorTileLayer& layer) { +VectorTileFeature::VectorTileFeature(pbf feature_pbf, const VectorTileLayer& layer_) + : layer(layer_) { while (feature_pbf.next()) { if (feature_pbf.tag == 1) { // id id = feature_pbf.varint(); @@ -43,16 +44,16 @@ VectorTileFeature::VectorTileFeature(pbf feature_pbf, const VectorTileLayer& lay throw std::runtime_error("feature referenced out of range key"); } - if (tags) { - uint32_t tag_val = tags.varint(); - if (layer.values.size() <= tag_val) { - throw std::runtime_error("feature referenced out of range value"); - } - - properties.emplace(layer.keys[tag_key], layer.values[tag_val]); - } else { + if (!tags) { throw std::runtime_error("uneven number of feature tag ids"); } + + uint32_t tag_val = tags.varint(); + if (layer.values.size() <= tag_val) { + throw std::runtime_error("feature referenced out of range value"); + } + + properties.emplace(tag_key, tag_val); } } else if (feature_pbf.tag == 3) { // type type = (FeatureType)feature_pbf.varint(); @@ -65,11 +66,15 @@ VectorTileFeature::VectorTileFeature(pbf feature_pbf, const VectorTileLayer& lay } mapbox::util::optional VectorTileFeature::getValue(const std::string& key) const { - auto it = properties.find(key); - if (it != properties.end()) { - return it->second; + auto keyIter = layer.keys.find(key); + if (keyIter == layer.keys.end()) { + return mapbox::util::optional(); } - return mapbox::util::optional(); + auto propIter = properties.find(keyIter->second); + if (propIter == properties.end()) { + return mapbox::util::optional(); + } + return layer.values[propIter->second]; } GeometryCollection VectorTileFeature::getGeometries() const { @@ -143,7 +148,7 @@ VectorTileLayer::VectorTileLayer(pbf layer_pbf) { } else if (layer_pbf.tag == 2) { // feature features.push_back(layer_pbf.message()); } else if (layer_pbf.tag == 3) { // keys - keys.emplace_back(layer_pbf.string()); + keys.emplace(layer_pbf.string(), keys.size()); } else if (layer_pbf.tag == 4) { // values values.emplace_back(std::move(parseValue(layer_pbf.message()))); } else if (layer_pbf.tag == 5) { // extent diff --git a/src/mbgl/map/vector_tile.hpp b/src/mbgl/map/vector_tile.hpp index ae08b54e2..6685bb613 100644 --- a/src/mbgl/map/vector_tile.hpp +++ b/src/mbgl/map/vector_tile.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace mbgl { @@ -19,9 +19,10 @@ public: GeometryCollection getGeometries() const override; private: + const VectorTileLayer& layer; uint64_t id = 0; FeatureType type = FeatureType::Unknown; - std::map properties; + std::unordered_map properties; pbf geometry_pbf; }; @@ -38,7 +39,7 @@ private: std::string name; uint32_t extent = 4096; - std::vector keys; + std::unordered_map keys; std::vector values; std::vector features; }; @@ -50,7 +51,7 @@ public: util::ptr getLayer(const std::string&) const override; private: - std::map> layers; + std::unordered_map> layers; }; }