refs #928: 90% of way to genericizing VectorTile

This commit is contained in:
Justin R. Miller
2015-03-02 16:30:22 -08:00
committed by John Firebaugh
parent 7d06e62363
commit 831264d530
5 changed files with 214 additions and 159 deletions

View File

@@ -2,22 +2,10 @@
#include <mbgl/style/filter_expression_private.hpp>
#include <mbgl/util/variant_io.hpp>
#include <algorithm>
#include <iostream>
#include <ostream>
using namespace mbgl;
std::ostream& mbgl::operator<<(std::ostream& os, const FeatureType& type) {
switch (type) {
case FeatureType::Unknown: return os << "Unknown";
case FeatureType::Point: return os << "Point";
case FeatureType::LineString: return os << "LineString";
case FeatureType::Polygon: return os << "Polygon";
default: return os << "Invalid";
}
}
VectorTileFeature::VectorTileFeature(pbf feature, const VectorTileLayer& layer) {
while (feature.next()) {
if (feature.tag == 1) { // id
@@ -44,17 +32,16 @@ VectorTileFeature::VectorTileFeature(pbf feature, const VectorTileLayer& layer)
}
}
} else if (feature.tag == 3) { // type
type = (FeatureType)feature.varint();
type = (GeometryFeatureType)feature.varint();
} else if (feature.tag == 4) { // geometry
geometry = feature.message();
geometries = feature.message();
} else {
feature.skip();
}
}
}
std::ostream& mbgl::operator<<(std::ostream& os, const VectorTileFeature& feature) {
std::ostream& mbgl::operator<<(std::ostream& os, const GeometryTileFeature<pbf>& feature) {
os << "Feature(" << feature.id << "): " << feature.type << std::endl;
for (const auto& prop : feature.properties) {
os << " - " << prop.first << ": " << prop.second << std::endl;
@@ -62,10 +49,6 @@ std::ostream& mbgl::operator<<(std::ostream& os, const VectorTileFeature& featur
return os;
}
VectorTile::VectorTile() {}
VectorTile::VectorTile(pbf tile) {
while (tile.next()) {
if (tile.tag == 3) { // layer
@@ -77,16 +60,7 @@ VectorTile::VectorTile(pbf tile) {
}
}
VectorTile& VectorTile::operator=(VectorTile && other) {
if (this != &other) {
layers.swap(other.layers);
}
return *this;
}
VectorTileLayer::VectorTileLayer(pbf layer) : data(layer) {
std::vector<std::string> stacks;
VectorTileLayer::VectorTileLayer(pbf layer) : layer_pbf(layer) {
while (layer.next()) {
if (layer.tag == 1) { // name
name = layer.string();
@@ -103,61 +77,61 @@ VectorTileLayer::VectorTileLayer(pbf layer) : data(layer) {
}
}
FilteredVectorTileLayer::FilteredVectorTileLayer(const VectorTileLayer& layer_, const FilterExpression &filterExpression_)
: layer(layer_),
filterExpression(filterExpression_) {
}
FilteredVectorTileLayer::FilteredVectorTileLayer(const VectorTileLayer& layer_, const FilterExpression& filterExpression_)
: GeometryFilteredTileLayer<pbf>(layer_, filterExpression_) {}
FilteredVectorTileLayer::iterator FilteredVectorTileLayer::begin() const {
return iterator(*this, layer.data);
const VectorTileLayer *layer_ = static_cast<const VectorTileLayer *>(&layer);
return FilteredVectorTileLayer::iterator(*this, layer_->layer_pbf);
}
FilteredVectorTileLayer::iterator FilteredVectorTileLayer::end() const {
return iterator(*this, pbf(layer.data.end, 0));
const VectorTileLayer *layer_ = static_cast<const VectorTileLayer *>(&layer);
return FilteredVectorTileLayer::iterator(*this, pbf(layer_->layer_pbf.end, 0));
}
FilteredVectorTileLayer::iterator::iterator(const FilteredVectorTileLayer& parent_, const pbf& data_)
: parent(parent_),
feature(pbf()),
data(data_) {
operator++();
FilteredVectorTileLayer::iterator::iterator(const FilteredVectorTileLayer& parent_, const pbf& data_pbf_)
: GeometryFilteredTileLayer<pbf>::iterator(parent_),
feature_pbf(pbf()),
data_pbf(data_pbf_) {}
VectorTileTagExtractor::VectorTileTagExtractor(const VectorTileLayer& layer_)
: GeometryTileTagExtractor(layer_) {}
void VectorTileTagExtractor::setTags(const pbf& pbf) {
tags_pbf = pbf;
}
VectorTileTagExtractor::VectorTileTagExtractor(const VectorTileLayer &layer) : layer_(layer) {}
void VectorTileTagExtractor::setTags(const pbf &pbf) {
tags_ = pbf;
}
mapbox::util::optional<Value> VectorTileTagExtractor::getValue(const std::string &key) const {
mapbox::util::optional<Value> VectorTileTagExtractor::getValue(const std::string& key) const {
if (key == "$type") {
return Value(uint64_t(type_));
return Value(uint64_t(type));
}
mapbox::util::optional<Value> value;
auto field_it = layer_.key_index.find(key);
if (field_it != layer_.key_index.end()) {
auto field_it = layer.key_index.find(key);
if (field_it != layer.key_index.end()) {
const uint32_t filter_key = field_it->second;
// Now loop through all the key/value pair tags.
// tags are packed varints. They should have an even length.
pbf tags_pbf = tags_;
pbf tags_pbf_ = tags_pbf;
uint32_t tag_key, tag_val;
while (tags_pbf) {
tag_key = tags_pbf.varint();
if (!tags_pbf) {
while (tags_pbf_) {
tag_key = tags_pbf_.varint();
if (!tags_pbf_) {
// This should not happen; otherwise the vector tile is invalid.
fprintf(stderr, "[WARNING] uneven number of feature tag ids\n");
return value;
}
// Note: We need to run this command in all cases, even if the keys don't match.
tag_val = tags_pbf.varint();
tag_val = tags_pbf_.varint();
if (tag_key == filter_key) {
if (layer_.values.size() > tag_val) {
value = layer_.values[tag_val];
if (layer.values.size() > tag_val) {
value = layer.values[tag_val];
} else {
fprintf(stderr, "[WARNING] feature references out of range value\n");
break;
@@ -169,10 +143,6 @@ mapbox::util::optional<Value> VectorTileTagExtractor::getValue(const std::string
return value;
}
void VectorTileTagExtractor::setType(FeatureType type) {
type_ = type;
}
template bool mbgl::evaluate(const FilterExpression&, const VectorTileTagExtractor&);
void FilteredVectorTileLayer::iterator::operator++() {
@@ -180,20 +150,20 @@ void FilteredVectorTileLayer::iterator::operator++() {
const FilterExpression &expression = parent.filterExpression;
while (data.next(2)) { // feature
feature = data.message();
pbf feature_pbf = feature;
while (data_pbf.next(2)) { // feature
feature_pbf = data_pbf.message();
pbf feature_pbf_ = feature_pbf;
VectorTileTagExtractor extractor(parent.layer);
GeometryTileTagExtractor<pbf> extractor(parent.layer);
// Retrieve the basic information
while (feature_pbf.next()) {
if (feature_pbf.tag == 2) { // tags
while (feature_pbf_.next()) {
if (feature_pbf_.tag == 2) { // tags
extractor.setTags(feature_pbf.message());
} else if (feature_pbf.tag == 3) { // geometry type
extractor.setType(FeatureType(feature_pbf.varint()));
} else if (feature_pbf_.tag == 3) { // geometry type
extractor.setType(GeometryFeatureType(feature_pbf_.varint()));
} else {
feature_pbf.skip();
feature_pbf_.skip();
}
}
@@ -207,9 +177,9 @@ void FilteredVectorTileLayer::iterator::operator++() {
}
bool FilteredVectorTileLayer::iterator::operator!=(const iterator& other) const {
return !(data.data == other.data.data && data.end == other.data.end && valid == other.valid);
return !(data_pbf.data == other.data_pbf.data && data_pbf.end == other.data_pbf.end && valid == other.valid);
}
const pbf& FilteredVectorTileLayer::iterator:: operator*() const {
return feature;
return feature_pbf;
}