Skip to content

Commit

Permalink
Access MBTiles file with correct Y axis
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakaner committed Nov 9, 2023
1 parent 1d2270f commit 55eac73
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/mbtiles_vector_featureset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ mapnik::feature_ptr mbtiles_vector_featureset::next()
return mapnik::feature_ptr();
}

int mbtiles_vector_featureset::convert_y(const int y) const
{
return (1 << zoom_) - 1 - y;
}

bool mbtiles_vector_featureset::next_tile()
{
++x_;
Expand All @@ -56,7 +61,7 @@ bool mbtiles_vector_featureset::next_tile()

bool mbtiles_vector_featureset::open_tile()
{
std::string sql = (boost::format("SELECT tile_data FROM tiles WHERE zoom_level = %1% AND tile_column = %2% AND tile_row = %3%") % zoom_ % x_ % y_).str();
std::string sql = (boost::format("SELECT tile_data FROM tiles WHERE zoom_level = %1% AND tile_column = %2% AND tile_row = %3%") % zoom_ % x_ % convert_y(y_)).str();
std::shared_ptr<sqlite_resultset> result (database_->execute_query(sql));
int size = 0;
char const* blob = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/mbtiles_vector_featureset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class mbtiles_vector_featureset : public mapnik::Featureset
int x_ = 0;
/// y index of the currently accessed tile
int y_ = 0;
/**
* Transform Y index (tile_row) from z/x/y to TMS schema.
*/
int convert_y(const int y) const;

bool next_tile();
bool open_tile();
Expand Down
24 changes: 9 additions & 15 deletions src/mvt_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mapnik::feature_ptr mvt_layer::next_feature()
return mapnik::feature_ptr();
}

void mvt_io::read_layer(protozero::pbf_reader& pbf_layer)
bool mvt_io::read_layer(protozero::pbf_reader& pbf_layer)
//void mvt_io::read_layer(protozero::pbf_message<mvt_message::layer>& pbf_layer)
{
layer_.reset(new mvt_layer(x_, y_, zoom_));
Expand Down Expand Up @@ -252,6 +252,8 @@ void mvt_io::read_layer(protozero::pbf_reader& pbf_layer)
}
}
layer_->finish_reading();
std::cerr << '\n';
return !ignore_layer;
}

mapnik::feature_ptr mvt_io::next()
Expand All @@ -270,23 +272,15 @@ mvt_io::mvt_io(std::string&& data, const uint32_t x, const uint32_t y, const uin
zoom_(zoom),
layer_name_(layer_name)
{
while (reader_.next())
{
switch (reader_.tag_and_type()) {
case tag_and_type(static_cast<uint32_t>(mvt_message::tile::layer), protozero::pbf_wire_type::length_delimited): {
std::cerr << "got layer\n";
const auto data_view(reader_.get_view());
while (reader_.next(static_cast<uint32_t>(mvt_message::tile::layer))) {
std::cerr << "got layer\n";
const auto data_view(reader_.get_view());
// protozero::pbf_message<mvt_message::layer> 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<uint32_t>(reader_.wire_type()) << '\n';
reader_.skip();
protozero::pbf_reader msg_layer(data_view);
if (read_layer(msg_layer)) {
break;
}
// read_layers();
}
}

5 changes: 4 additions & 1 deletion src/mvt_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ class mvt_io
const int tile_extent_ = -1;
std::unique_ptr<mvt_layer> layer_;

void read_layer(protozero::pbf_reader& l);
/**
* Read a layer from PBF. Returns true if requested layer was parsed.
*/
bool read_layer(protozero::pbf_reader& l);
// void read_layer(protozero::pbf_message<mvt_message::layer>& l);
// void read_layers();

Expand Down

0 comments on commit 55eac73

Please sign in to comment.