Skip to content

Commit

Permalink
Bugfix: Create attributes of the features on the Mapnik context
Browse files Browse the repository at this point in the history
Otherwise Mapnik gets not attributes and any attribute filters return
false.
  • Loading branch information
Nakaner committed Nov 9, 2023
1 parent e024acc commit a06ba77
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/mbtiles_vector_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,26 @@ void mbtiles_vector_datasource::parse_json()
//TODO init bounds
}

mapnik::context_ptr mbtiles_vector_datasource::get_context_with_attributes() const
{
mapnik::context_ptr context = std::make_shared<mapnik::context_type>();
const std::vector<mapnik::attribute_descriptor>& desc_ar = desc_.get_descriptors();
for (auto const& attr_info : desc_ar)
{
context->push(attr_info.get_name()); // TODO only push query attributes
}
return context;
}

mapnik::featureset_ptr mbtiles_vector_datasource::features(mapnik::query const& q) const
{
#ifdef MAPNIK_STATS
mapnik::progress_timer __stats__(std::clog, "mbtiles_vector_datasource::features");
#endif

mapnik::box2d<double> const& box = q.get_bbox();
return mapnik::featureset_ptr(new mbtiles_vector_featureset(dataset_, zoom_, box, layer_));
mapnik::context_ptr context = get_context_with_attributes();
return mapnik::featureset_ptr(new mbtiles_vector_featureset(dataset_, context, zoom_, box, layer_));
}

mapnik::featureset_ptr mbtiles_vector_datasource::features_at_point(mapnik::coord2d const& pt, double tol) const
Expand All @@ -277,6 +289,6 @@ mapnik::featureset_ptr mbtiles_vector_datasource::features_at_point(mapnik::coor
#endif

mapnik::filter_at_point filter(pt, tol);

return mapnik::featureset_ptr(new mbtiles_vector_featureset(dataset_, zoom_, filter.box_, layer_));
mapnik::context_ptr context = get_context_with_attributes();
return mapnik::featureset_ptr(new mbtiles_vector_featureset(dataset_, context, zoom_, filter.box_, layer_));
}
1 change: 1 addition & 0 deletions src/mbtiles_vector_datasource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class mbtiles_vector_datasource : public mapnik::datasource

private:
void init(mapnik::parameters const& params);
mapnik::context_ptr get_context_with_attributes() const;
int zoom_from_string(const char* z);
int zoom_from_string(const std::string& z);
void parse_json();
Expand Down
8 changes: 4 additions & 4 deletions src/mbtiles_vector_featureset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include <math.h>

mbtiles_vector_featureset::mbtiles_vector_featureset(std::shared_ptr<sqlite_connection> database,
const int zoom,
mapnik::context_ptr const& ctx, const int zoom,
mapnik::box2d<double> const& extent, const std::string & layer) :
database_(database),
context_(ctx),
zoom_(zoom),
extent_(extent),
layer_(layer),
Expand Down Expand Up @@ -89,15 +90,14 @@ bool mbtiles_vector_featureset::open_tile()
{
return false;
}
std::cerr << "Opening vector tile " << zoom_ << '/' << x_ << '/' << y_ << '\n';
if (mapnik::vector_tile_impl::is_gzip_compressed(blob, size) ||
mapnik::vector_tile_impl::is_zlib_compressed(blob, size))
{
std::string decompressed;
mapnik::vector_tile_impl::zlib_decompress(blob, size, decompressed);
vector_tile_.reset(new mvt_io(std::move(decompressed), x_, y_, zoom_, layer_));
vector_tile_.reset(new mvt_io(std::move(decompressed), context_, x_, y_, zoom_, layer_));
} else {
vector_tile_.reset(new mvt_io(std::string(blob, size), x_, y_, zoom_, layer_));
vector_tile_.reset(new mvt_io(std::string(blob, size), context_, x_, y_, zoom_, layer_));
}
return true;
}
2 changes: 2 additions & 0 deletions src/mbtiles_vector_featureset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class mbtiles_vector_featureset : public mapnik::Featureset
{
public:
mbtiles_vector_featureset(std::shared_ptr<sqlite_connection> database,
mapnik::context_ptr const& ctx,
const int zoom,
mapnik::box2d<double> const& extent,
const std::string & layer);
Expand All @@ -44,6 +45,7 @@ class mbtiles_vector_featureset : public mapnik::Featureset
private:
bool valid() const;
std::shared_ptr<sqlite_connection> database_;
mapnik::context_ptr context_;
int zoom_;
mapnik::box2d<double> const& extent_;
const std::string& layer_;
Expand Down
14 changes: 8 additions & 6 deletions src/mvt_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#include <stdexcept>


mvt_layer::mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom)
mvt_layer::mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom,
mapnik::context_ptr const& ctx)
: keys_(),
values_(),
tr_("utf-8"),
ctx_(std::make_shared<mapnik::context_type>())
context_(ctx),
tr_("utf-8")
{
resolution_ = mapnik::EARTH_CIRCUMFERENCE / (1 << zoom);
tile_x_ = -0.5 * mapnik::EARTH_CIRCUMFERENCE + x * resolution_;
Expand Down Expand Up @@ -71,7 +72,7 @@ mapnik::feature_ptr mvt_layer::next_feature()
{
const protozero::data_view d (features_.at(feature_index_));
protozero::pbf_reader f (d);
mapnik::feature_ptr feature = mapnik::feature_factory::create(ctx_, feature_index_);
mapnik::feature_ptr feature = mapnik::feature_factory::create(context_, feature_index_);
++feature_index_;
mvt_message::geom_type geometry_type = mvt_message::geom_type::unknown;
bool has_geometry = false;
Expand Down Expand Up @@ -166,7 +167,7 @@ mapnik::feature_ptr mvt_layer::next_feature()

bool mvt_io::read_layer(protozero::pbf_message<mvt_message::layer>& pbf_layer)
{
layer_.reset(new mvt_layer(x_, y_, zoom_));
layer_.reset(new mvt_layer(x_, y_, zoom_, context_));
bool ignore_layer = false;
while (pbf_layer.next())
{
Expand Down Expand Up @@ -264,8 +265,9 @@ mapnik::feature_ptr mvt_io::next()
return layer_->next_feature();
}

mvt_io::mvt_io(std::string&& data, const uint32_t x, const uint32_t y, const uint32_t zoom, std::string layer_name)
mvt_io::mvt_io(std::string&& data, mapnik::context_ptr const& ctx, const uint32_t x, const uint32_t y, const uint32_t zoom, std::string layer_name)
: reader_(data),
context_(ctx),
x_(x),
y_(y),
zoom_(zoom),
Expand Down
7 changes: 4 additions & 3 deletions src/mvt_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ class mvt_layer
size_t feature_index_ = 0;
std::vector<std::string> keys_;
std::vector<pbf_attr_value_type> values_;
mapnik::context_ptr const& context_;
std::size_t num_keys_ = 0;
std::size_t num_values_ = 0;
mapnik::transcoder tr_;
mapnik::context_ptr ctx_;
double tile_x_;
double tile_y_;
double resolution_;
double scale_ = 0;
std::string name_;
uint32_t extent_ = 4096;
public:
explicit mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom);
explicit mvt_layer(const uint32_t x, const uint32_t y, const uint32_t zoom, mapnik::context_ptr const& ctx);
void add_feature(const protozero::data_view& feature);
bool has_features() const;
void add_key(std::string&& key);
Expand All @@ -95,6 +95,7 @@ class mvt_layer
class mvt_io
{
protozero::pbf_reader reader_;
mapnik::context_ptr context_;
const uint32_t x_;
const uint32_t y_;
const uint32_t zoom_;
Expand All @@ -108,7 +109,7 @@ class mvt_io
bool read_layer(protozero::pbf_message<mvt_message::layer>& l);

public:
explicit mvt_io(std::string&& data, const uint32_t x, const uint32_t y, const uint32_t zoom, std::string layer_name);
explicit mvt_io(std::string&& data, mapnik::context_ptr const& ctx, const uint32_t x, const uint32_t y, const uint32_t zoom, std::string layer_name);
mapnik::feature_ptr next();
};

Expand Down

0 comments on commit a06ba77

Please sign in to comment.