mapnik/tests/cpp_tests/svg_renderer_tests/path_element_test.cpp

313 lines
10 KiB
C++
Raw Normal View History

#define BOOST_TEST_MODULE path_element_tests
// mapnik
#include <mapnik/map.hpp>
2014-07-23 21:36:36 +02:00
#include <mapnik/layer.hpp>
2013-01-04 09:06:05 +01:00
#include <mapnik/rule.hpp>
#include <mapnik/feature_type_style.hpp>
#include <mapnik/svg/output/svg_renderer.hpp>
2014-07-14 20:42:23 +02:00
#include <mapnik/text/placements/dummy.hpp>
#include <mapnik/datasource_cache.hpp>
2012-02-12 12:46:07 +01:00
#include <mapnik/expression.hpp>
#include <mapnik/color_factory.hpp>
2013-12-11 16:26:32 +01:00
#include <mapnik/image_util.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/agg_renderer.hpp>
// boost
#include <boost/test/included/unit_test.hpp>
#include <boost/filesystem.hpp>
// stl
#include <fstream>
#include <iterator>
2012-10-31 03:21:34 +01:00
namespace fs = boost::filesystem;
using namespace mapnik;
2013-12-11 16:26:32 +01:00
const std::string srs_lcc="+proj=lcc +ellps=GRS80 +lat_0=49 +lon_0=-95 +lat+1=49 +lat_2=77 \
+datum=NAD83 +units=m +no_defs";
const std::string srs_merc="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 \
+y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over";
2013-12-11 16:26:32 +01:00
void prepare_map(Map & m)
{
// Provinces (polygon)
feature_type_style provpoly_style;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[NAME_EN] = 'Ontario'"));
{
polygon_symbolizer poly_sym;
put(poly_sym, keys::fill, color(250, 190, 183));
r.append(std::move(poly_sym));
}
2014-05-14 03:24:17 +02:00
provpoly_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
{
rule r;
r.set_filter(parse_expression("[NOM_FR] = 'Québec'"));
{
polygon_symbolizer poly_sym;
put(poly_sym, keys::fill, color(217, 235, 203));
r.append(std::move(poly_sym));
}
2014-05-14 03:24:17 +02:00
provpoly_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("provinces",provpoly_style);
// Provinces (polyline)
feature_type_style provlines_style;
2013-12-11 16:26:32 +01:00
{
rule r;
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(0,0,0));
put(line_sym,keys::stroke_width,1.0);
dash_array dash;
dash.emplace_back(8,4);
dash.emplace_back(2,2);
dash.emplace_back(2,2);
put(line_sym,keys::stroke_dasharray,dash);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
provlines_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("provlines",provlines_style);
2012-04-17 07:33:37 +02:00
// Drainage
feature_type_style qcdrain_style;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[HYC] = 8"));
{
polygon_symbolizer poly_sym;
put(poly_sym, keys::fill, color(153, 204, 255));
r.append(std::move(poly_sym));
}
2014-05-14 03:24:17 +02:00
qcdrain_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("drainage",qcdrain_style);
2012-04-17 07:33:37 +02:00
// Roads 3 and 4 (The "grey" roads)
2012-04-17 07:33:37 +02:00
feature_type_style roads34_style;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[CLASS] = 3 or [CLASS] = 4"));
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(171,158,137));
put(line_sym,keys::stroke_width,2.0);
put(line_sym,keys::stroke_linecap,ROUND_CAP);
put(line_sym,keys::stroke_linejoin,ROUND_JOIN);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
roads34_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("smallroads",roads34_style);
2012-04-17 07:33:37 +02:00
// Roads 2 (The thin yellow ones)
feature_type_style roads2_style_1;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[CLASS] = 2"));
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(171,158,137));
put(line_sym,keys::stroke_width,4.0);
put(line_sym,keys::stroke_linecap,ROUND_CAP);
put(line_sym,keys::stroke_linejoin,ROUND_JOIN);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
roads2_style_1.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("road-border", roads2_style_1);
2012-04-17 07:33:37 +02:00
feature_type_style roads2_style_2;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[CLASS] = 2"));
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(255,250,115));
put(line_sym,keys::stroke_width,2.0);
put(line_sym,keys::stroke_linecap,ROUND_CAP);
put(line_sym,keys::stroke_linejoin,ROUND_JOIN);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
roads2_style_2.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("road-fill", roads2_style_2);
2012-04-17 07:33:37 +02:00
// Roads 1 (The big orange ones, the highways)
feature_type_style roads1_style_1;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[CLASS] = 1"));
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(188,149,28));
put(line_sym,keys::stroke_width,7.0);
put(line_sym,keys::stroke_linecap,ROUND_CAP);
put(line_sym,keys::stroke_linejoin,ROUND_JOIN);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
roads1_style_1.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("highway-border", roads1_style_1);
2012-04-17 07:33:37 +02:00
feature_type_style roads1_style_2;
2013-12-11 16:26:32 +01:00
{
rule r;
r.set_filter(parse_expression("[CLASS] = 1"));
{
line_symbolizer line_sym;
put(line_sym,keys::stroke,color(242,191,36));
put(line_sym,keys::stroke_width,5.0);
put(line_sym,keys::stroke_linecap,ROUND_CAP);
put(line_sym,keys::stroke_linejoin,ROUND_JOIN);
r.append(std::move(line_sym));
}
2014-05-14 03:24:17 +02:00
roads1_style_2.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
2012-04-17 07:33:37 +02:00
m.insert_style("highway-fill", roads1_style_2);
2013-12-11 16:26:32 +01:00
// Populated Places
feature_type_style popplaces_style;
{
rule r;
{
text_symbolizer text_sym;
text_placements_ptr placement_finder = std::make_shared<text_placements_dummy>();
2014-07-23 21:36:36 +02:00
placement_finder->defaults.format_defaults.face_name = "DejaVu Sans Book";
placement_finder->defaults.format_defaults.text_size = mapnik::value_integer(10);
placement_finder->defaults.format_defaults.fill = color(0,0,0);
placement_finder->defaults.format_defaults.halo_fill = color(255,255,200);
placement_finder->defaults.format_defaults.halo_radius = mapnik::value_integer(1);
2013-12-11 16:26:32 +01:00
placement_finder->defaults.set_old_style_expression(parse_expression("[GEONAME]"));
put<text_placements_ptr>(text_sym, keys::text_placements_, placement_finder);
r.append(std::move(text_sym));
}
2014-05-14 03:24:17 +02:00
popplaces_style.add_rule(std::move(r));
2013-12-11 16:26:32 +01:00
}
m.insert_style("popplaces",popplaces_style );
// layers
// Provincial polygons
{
2011-05-04 17:53:36 +02:00
parameters p;
p["type"]="shape";
2013-12-11 16:26:32 +01:00
p["file"]="demo/data/boundaries";
p["encoding"]="latin1";
2012-04-17 07:33:37 +02:00
layer lyr("Provinces");
lyr.set_datasource(datasource_cache::instance().create(p));
2013-12-11 16:26:32 +01:00
lyr.set_srs(srs_lcc);
2012-04-17 07:33:37 +02:00
lyr.add_style("provinces");
m.add_layer(lyr);
}
2012-04-17 07:33:37 +02:00
// Drainage
{
2011-05-04 17:53:36 +02:00
parameters p;
p["type"]="shape";
2013-12-11 16:26:32 +01:00
p["file"]="demo/data/qcdrainage";
2011-05-04 17:53:36 +02:00
layer lyr("Quebec Hydrography");
lyr.set_datasource(datasource_cache::instance().create(p));
2013-12-11 16:26:32 +01:00
lyr.set_srs(srs_lcc);
2012-04-17 07:33:37 +02:00
lyr.add_style("drainage");
m.add_layer(lyr);
}
2012-04-17 07:33:37 +02:00
{
2011-05-04 17:53:36 +02:00
parameters p;
p["type"]="shape";
2013-12-11 16:26:32 +01:00
p["file"]="demo/data/ontdrainage";
2012-04-17 07:33:37 +02:00
layer lyr("Ontario Hydrography");
lyr.set_datasource(datasource_cache::instance().create(p));
2013-12-11 16:26:32 +01:00
lyr.set_srs(srs_lcc);
2012-04-17 07:33:37 +02:00
lyr.add_style("drainage");
m.add_layer(lyr);
}
2012-04-17 07:33:37 +02:00
// Provincial boundaries
{
2011-05-04 17:53:36 +02:00
parameters p;
p["type"]="shape";
2013-12-11 16:26:32 +01:00
p["file"]="demo/data/boundaries_l";
2012-04-17 07:33:37 +02:00
layer lyr("Provincial borders");
2013-12-11 16:26:32 +01:00
lyr.set_srs(srs_lcc);
lyr.set_datasource(datasource_cache::instance().create(p));
2012-04-17 07:33:37 +02:00
lyr.add_style("provlines");
m.add_layer(lyr);
}
2012-04-17 07:33:37 +02:00
// Roads
{
2011-05-04 17:53:36 +02:00
parameters p;
p["type"]="shape";
2013-12-11 16:26:32 +01:00
p["file"]="demo/data/roads";
2012-04-17 07:33:37 +02:00
layer lyr("Roads");
2013-12-11 16:26:32 +01:00
lyr.set_srs(srs_lcc);
lyr.set_datasource(datasource_cache::instance().create(p));
2011-05-04 17:53:36 +02:00
lyr.add_style("smallroads");
lyr.add_style("road-border");
lyr.add_style("road-fill");
lyr.add_style("highway-border");
lyr.add_style("highway-fill");
2012-04-17 07:33:37 +02:00
m.add_layer(lyr);
}
2013-12-11 16:26:32 +01:00
// popplaces
{
parameters p;
p["type"]="shape";
p["file"]="demo/data/popplaces";
p["encoding"] = "latin1";
layer lyr("Populated Places");
lyr.set_srs(srs_lcc);
lyr.set_datasource(datasource_cache::instance().create(p));
lyr.add_style("popplaces");
m.add_layer(lyr);
}
}
void render_to_file(Map const& m, const std::string output_filename)
{
std::ofstream output_stream(output_filename.c_str());
if(output_stream)
{
using svg_ren = svg_renderer<std::ostream_iterator<char> >;
2011-05-04 17:53:36 +02:00
std::ostream_iterator<char> output_stream_iterator(output_stream);
svg_ren renderer(m, output_stream_iterator);
renderer.apply();
output_stream.close();
2012-10-31 03:21:34 +01:00
fs::path output_filename_path =
fs::system_complete(fs::path(".")) / fs::path(output_filename);
BOOST_CHECK_MESSAGE(fs::exists(output_filename_path),
2012-04-17 07:33:37 +02:00
"File '"+output_filename_path.string()+"' was created.");
}
else
{
2011-05-04 17:53:36 +02:00
BOOST_FAIL("Could not create create/open file '"+output_filename+"'.");
}
}
BOOST_AUTO_TEST_CASE(path_element_test_case_1)
{
2013-12-11 16:26:32 +01:00
std::cout << " looking for 'shape.input' plugin in ./plugins/input/" << "\n";
datasource_cache::instance().register_datasources("plugins/input/");
Map m(800,600);
2013-12-11 16:26:32 +01:00
m.set_background(parse_color("white"));
m.set_srs(srs_merc);
prepare_map(m);
2013-12-11 16:26:32 +01:00
m.zoom_to_box(box2d<double>(-8024477.28459,5445190.38849,-7381388.20071,5662941.44855));
render_to_file(m, "path_element_test_case_1.svg");
2013-12-11 16:26:32 +01:00
mapnik::image_32 buf(m.width(),m.height());
mapnik::agg_renderer<mapnik::image_32> ren(m,buf);
ren.apply();
mapnik::save_to_file(buf,"path_element_test_case_1.png");
}