diff --git a/src/mvt_io.cpp b/src/mvt_io.cpp index a0843a0..2690fb8 100644 --- a/src/mvt_io.cpp +++ b/src/mvt_io.cpp @@ -9,6 +9,7 @@ #include "vector_tile_geometry_decoder.hpp" #include #include +#include mvt_layer::mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom) @@ -22,7 +23,7 @@ mvt_layer::mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom) tile_y_ = 0.5 * mapnik::EARTH_CIRCUMFERENCE - y * resolution_; } -void mvt_layer::add_feature(protozero::pbf_message&& feature) +void mvt_layer::add_feature(protozero::pbf_message feature) { features_.push_back(feature); } @@ -158,33 +159,45 @@ mapnik::feature_ptr mvt_layer::next_feature() return mapnik::feature_ptr(); } -void mvt_io::read_layer(protozero::pbf_message& pbf_layer) +void mvt_io::read_layer(protozero::pbf_reader& pbf_layer) +//void mvt_io::read_layer(protozero::pbf_message& pbf_layer) { layer_.reset(new mvt_layer(x_, y_, zoom_)); + std::cerr << "Message layer\n"; + bool ignore_layer = false; while (pbf_layer.next()) { + if (ignore_layer) { + pbf_layer.skip(); + continue; + } switch (pbf_layer.tag()) { - case mvt_message::layer::name: + case static_cast(mvt_message::layer::name): { - std::cerr << "Message layer\n"; std::string name = pbf_layer.get_string(); + std::cerr << " name '" << name << "' expected '" << layer_name_ << "'\n"; if (name != layer_name_) { - return; + std::cerr << " skipping\n"; + ignore_layer = true; } layer_->set_name(std::move(name)); break; } - case mvt_message::layer::extent: + case static_cast(mvt_message::layer::extent): + std::cerr << " extent\n"; layer_->set_extent(pbf_layer.get_uint32()); break; - case mvt_message::layer::keys: + case static_cast(mvt_message::layer::keys): + std::cerr << " keys\n"; layer_->add_key(pbf_layer.get_string()); break; - case mvt_message::layer::values: + case static_cast(mvt_message::layer::values): { - protozero::pbf_message val_msg = pbf_layer.get_message(); + std::cerr << " values\n"; + const auto data_view(pbf_layer.get_view()); + protozero::pbf_message val_msg (data_view); while (val_msg.next()) { switch(val_msg.tag()) { @@ -210,44 +223,35 @@ void mvt_io::read_layer(protozero::pbf_message& pbf_layer) layer_->add_value(val_msg.get_bool()); break; default: + val_msg.skip(); throw std::runtime_error("unknown Value type " + std::to_string(static_cast(val_msg.tag())) + " in layer->values"); } } break; } - case mvt_message::layer::features: + case static_cast(mvt_message::layer::features): { - protozero::pbf_message f_msg(pbf_layer.get_view()); - layer_->add_feature(std::move(f_msg)); + std::cerr << " features\n"; + const auto data_view(pbf_layer.get_view()); + protozero::pbf_message f_msg (data_view); + layer_->add_feature(f_msg); break; } - default: - pbf_layer.skip(); - } - } - layer_->finish_reading(); -} - -void mvt_io::read_layers() -{ - protozero::pbf_message pbf_layer = message_.get_message(); -// protozero::pbf_reader pbf_layer = message_.get_message(); - - while (pbf_layer.next()) - { - switch (pbf_layer.tag()) - { - case mvt_message::tile::layer: + case static_cast(mvt_message::layer::version): { - protozero::pbf_message msg_layer(pbf_layer.get_view()); - read_layer(msg_layer); + uint32_t version = pbf_layer.get_uint32(); + std::cerr << " version " << version << '\n'; + if (version != 2) { + throw std::runtime_error("Vector tile does not have major version 2."); + } break; } default: - throw std::runtime_error{std::string("Unsupported message ") + std::to_string(static_cast(pbf_layer.tag())) + " in vector tile"}; - break; + std::cerr << " other (tag " << static_cast(pbf_layer.tag()) << " wire_type " << static_cast(pbf_layer.wire_type()) << ")\n"; + pbf_layer.skip(); } } + layer_->finish_reading(); } mapnik::feature_ptr mvt_io::next() @@ -260,17 +264,29 @@ mapnik::feature_ptr mvt_io::next() } mvt_io::mvt_io(std::string&& data, const uint32_t x, const uint32_t y, const uint32_t zoom, std::string layer_name) - : message_(data), + : reader_(data), x_(x), y_(y), zoom_(zoom), layer_name_(layer_name) { - while (message_.next(mvt_message::tile::layer, protozero::pbf_wire_type::length_delimited)) + while (reader_.next()) { - protozero::pbf_message msg_layer(message_.get_message()); - read_layer(msg_layer); -// read_layers(); + switch (reader_.tag_and_type()) { + case tag_and_type(static_cast(mvt_message::tile::layer), protozero::pbf_wire_type::length_delimited): { + std::cerr << "got layer\n"; + const auto data_view(reader_.get_view()); +// protozero::pbf_message msg_layer(reader_.get_message()); + protozero::pbf_reader msg_layer(data_view); + read_layer(msg_layer); +// read_layers(); + break; + } + default: + std::cerr << "got tag " << reader_.tag() << " type " << static_cast(reader_.wire_type()) << '\n'; + reader_.skip(); + break; + } } } diff --git a/src/mvt_io.hpp b/src/mvt_io.hpp index dafca1f..6f3d663 100644 --- a/src/mvt_io.hpp +++ b/src/mvt_io.hpp @@ -97,7 +97,7 @@ class mvt_layer uint32_t extent_ = 4096; public: explicit mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom); - void add_feature(protozero::pbf_message&& feature); + void add_feature(protozero::pbf_message feature); bool has_features() const; void add_key(std::string&& key); void add_value(pbf_attr_value_type value); @@ -113,7 +113,7 @@ class mvt_layer class mvt_io { - protozero::pbf_message message_; + protozero::pbf_reader reader_; const uint32_t x_; const uint32_t y_; const uint32_t zoom_; @@ -121,8 +121,9 @@ class mvt_io const int tile_extent_ = -1; std::unique_ptr layer_; - void read_layer(protozero::pbf_message& l); - void read_layers(); + void read_layer(protozero::pbf_reader& l); +// void read_layer(protozero::pbf_message& l); +// void read_layers(); // /// Transform pixel coordinates to Mercator coordinates, requires coordinate of top left corner of the tile. // double pixel_x_to_mercator(const double x);