Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass variables to replace tokens in query #248

Merged
merged 5 commits into from
Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/vector_tile_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class tile_layer
double scale_factor,
double scale_denom,
int offset_x,
int offset_y)
int offset_y,
mapnik::attributes const& vars)
: valid_(true),
ds_(lay.datasource()),
target_proj_(map.srs(), true),
Expand All @@ -106,7 +107,7 @@ class tile_layer
layer_extent_(calc_extent(tile_size)),
target_buffered_extent_(calc_target_buffered_extent(tile_extent_bbox, buffer_size, lay, map)),
source_buffered_extent_(calc_source_buffered_extent()),
query_(calc_query(scale_factor, scale_denom, tile_extent_bbox, lay)),
query_(calc_query(scale_factor, scale_denom, tile_extent_bbox, lay, vars)),
view_trans_(layer_extent_, layer_extent_, tile_extent_bbox, offset_x, offset_y),
empty_(true),
painted_(false)
Expand Down Expand Up @@ -221,7 +222,8 @@ class tile_layer
mapnik::query calc_query(double scale_factor,
double scale_denom,
mapnik::box2d<double> const& tile_extent_bbox,
mapnik::layer const& lay)
mapnik::layer const& lay,
mapnik::attributes const& vars)
{
// Adjust the scale denominator if required
if (scale_denom <= 0.0)
Expand Down Expand Up @@ -293,6 +295,7 @@ class tile_layer
q.add_property_name(desc.get_name());
}
}
q.set_variables(vars);
return q;
}

Expand All @@ -306,6 +309,11 @@ class tile_layer
return ds_->features(query_);
}

mapnik::query const& get_query() const
{
return query_;
}

mapnik::view_transform const& get_view_transform() const
{
return view_trans_;
Expand Down
12 changes: 10 additions & 2 deletions src/vector_tile_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mapnik/image_scaling.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/map.hpp>
#include <mapnik/attribute.hpp>
#include <mapnik/request.hpp>
#include <mapnik/util/noncopyable.hpp>

Expand Down Expand Up @@ -46,9 +47,10 @@ class processor : private mapnik::util::noncopyable
bool multi_polygon_union_;
bool process_all_rings_;
std::launch threading_mode_;
mapnik::attributes vars_;

public:
processor(mapnik::Map const& map)
processor(mapnik::Map const& map, mapnik::attributes const& vars = mapnik::attributes())
: m_(map),
image_format_("webp"),
scale_factor_(1.0),
Expand All @@ -59,7 +61,8 @@ class processor : private mapnik::util::noncopyable
strictly_simple_(true),
multi_polygon_union_(false),
process_all_rings_(false),
threading_mode_(std::launch::deferred) {}
threading_mode_(std::launch::deferred),
vars_(vars) {}

MAPNIK_VECTOR_INLINE void update_tile(tile & t,
double scale_denom = 0.0,
Expand Down Expand Up @@ -182,6 +185,11 @@ class processor : private mapnik::util::noncopyable
return image_format_;
}

mapnik::attributes const& get_variables() const
{
return vars_;
}

void set_threading_mode(std::launch mode)
{
threading_mode_ = mode;
Expand Down
4 changes: 3 additions & 1 deletion src/vector_tile_processor.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mapnik/layer.hpp>
#include <mapnik/map.hpp>
#include <mapnik/version.hpp>
#include <mapnik/attribute.hpp>

#if MAPNIK_VERSION >= 300100
#include <mapnik/geometry/transform.hpp>
Expand Down Expand Up @@ -258,7 +259,8 @@ MAPNIK_VECTOR_INLINE void processor::update_tile(tile & t,
scale_factor_,
scale_denom,
offset_x,
offset_y);
offset_y,
vars_);
if (!tile_layers.back().is_valid())
{
t.add_empty_layer(lay.name());
Expand Down
36 changes: 36 additions & 0 deletions test/system/processor_and_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ TEST_CASE("vector tile output -- simple two points")
CHECK(190 == buffer.size());
}

TEST_CASE("processor -- can deal with (optional) variables")
{
// Build Map
mapnik::Map map(256, 256, "+init=epsg:3857");
mapnik::layer lyr("layer",map.srs());
lyr.set_datasource(testing::build_ds(0,0,true));
map.add_layer(lyr);

// Create processor
const mapnik::attributes vars { {"zoom_level", 20} };
mapnik::vector_tile_impl::processor ren(map, vars);

// Request Tile
mapnik::vector_tile_impl::merc_tile out_tile = ren.create_tile(0,0,0);
CHECK(out_tile.is_painted() == true);
CHECK(out_tile.is_empty() == false);

// Now check that the tile is correct.
vector_tile::Tile tile;
tile.ParseFromString(out_tile.get_buffer());
REQUIRE(1 == tile.layers_size());
vector_tile::Tile_Layer const& layer = tile.layers(0);
CHECK(std::string("layer") == layer.name());
REQUIRE(2 == layer.features_size());
vector_tile::Tile_Feature const& f = layer.features(0);
CHECK(static_cast<mapnik::value_integer>(1) == static_cast<mapnik::value_integer>(f.id()));
REQUIRE(3 == f.geometry_size());
CHECK(9 == f.geometry(0));
CHECK(4096 == f.geometry(1));
CHECK(4096 == f.geometry(2));
CHECK(190 == tile.ByteSize());
std::string buffer;
CHECK(tile.SerializeToString(&buffer));
CHECK(190 == buffer.size());
}

TEST_CASE("vector tile output -- empty tile")
{
// test adding empty layers should result in empty tile
Expand Down
67 changes: 67 additions & 0 deletions test/unit/layer_impl/layer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "catch.hpp"

// mapnik
#include <mapnik/map.hpp>
#include <mapnik/attribute.hpp>
#include <mapnik/memory_datasource.hpp>

// mapnik vector tile layer class
#include "vector_tile_layer.hpp"

TEST_CASE("Vector tile layer class")
{
SECTION("The constructor can produce a valid tile_layer with empty vars")
{
mapnik::Map map(256, 256);

// Create memory datasource
mapnik::parameters params;
params["type"] = "memory";
auto ds = std::make_shared<mapnik::memory_datasource>(params);

mapnik::layer layer("layer", "+init=epsg:3857");
layer.set_datasource(ds);
mapnik::box2d<double> extent(-20037508.342789,-20037508.342789,20037508.342789,20037508.342789);
const mapnik::attributes empty_vars;

mapnik::vector_tile_impl::tile_layer some_layer(map,
layer,
extent,
256, // tile_size
10, // buffer_size
1.0, // scale_factor
0, // scale_denom
0, // offset_x
0, // offset_y
empty_vars);
CHECK(some_layer.is_valid());
}

SECTION("The query has the variables passed to the constructor")
{
mapnik::Map map(256, 256);

// Create memory datasource
mapnik::parameters params;
params["type"] = "memory";
auto ds = std::make_shared<mapnik::memory_datasource>(params);

mapnik::layer layer("layer", "+init=epsg:3857");
layer.set_datasource(ds);
mapnik::box2d<double> extent(-20037508.342789,-20037508.342789,20037508.342789,20037508.342789);
const mapnik::attributes vars { {"zoom_level", 20} };

mapnik::vector_tile_impl::tile_layer some_layer(map,
layer,
extent,
256, // tile_size
10, // buffer_size
1.0, // scale_factor
0, // scale_denom
0, // offset_x
0, // offset_y
vars);
CHECK(some_layer.is_valid());
CHECK( ( vars == some_layer.get_query().variables() ) );
}
}
33 changes: 33 additions & 0 deletions test/unit/processor/variables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "catch.hpp"

// mapnik
#include <mapnik/map.hpp>
#include <mapnik/attribute.hpp>
#include <mapnik/memory_datasource.hpp>
#include <mapnik/feature_factory.hpp>

// mapnik-vector-tile
#include "vector_tile_processor.hpp"
#include "vector_tile_tile.hpp"


TEST_CASE("feature processor - can pass variables down to layers")
{
SECTION("variables are optional")
{
mapnik::Map map(256, 256);
mapnik::attributes empty_vars;

mapnik::vector_tile_impl::processor some_processor(map);
CHECK( ( empty_vars == some_processor.get_variables() ) );
}

SECTION("variables can be passed down to the processor")
{
mapnik::Map map(256, 256);
const mapnik::attributes vars { {"zoom_level", 20} };

mapnik::vector_tile_impl::processor some_processor(map, vars);
CHECK( ( vars == some_processor.get_variables() ) );
}
}