2010-07-28 08:51:24 +02:00
|
|
|
#define BOOST_TEST_MODULE path_element_tests
|
|
|
|
|
|
|
|
// boost.test
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
|
|
|
|
// boost.filesystem
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/map.hpp>
|
2013-01-04 09:06:05 +01:00
|
|
|
#include <mapnik/rule.hpp>
|
|
|
|
#include <mapnik/feature_type_style.hpp>
|
2012-08-28 02:58:49 +02:00
|
|
|
#include <mapnik/svg/output/svg_renderer.hpp>
|
2010-07-28 08:51:24 +02:00
|
|
|
#include <mapnik/datasource_cache.hpp>
|
2012-02-12 12:46:07 +01:00
|
|
|
#include <mapnik/expression.hpp>
|
2010-07-28 08:51:24 +02:00
|
|
|
#include <mapnik/color_factory.hpp>
|
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
|
2012-10-31 03:21:34 +01:00
|
|
|
namespace fs = boost::filesystem;
|
2010-08-10 10:23:02 +02:00
|
|
|
using namespace mapnik;
|
2010-07-28 08:51:24 +02:00
|
|
|
|
2010-08-10 10:23:02 +02:00
|
|
|
void prepare_map(Map& m)
|
2012-04-17 07:33:37 +02:00
|
|
|
{
|
2011-11-23 12:33:58 +01:00
|
|
|
const std::string mapnik_dir("/usr/local/lib/mapnik/");
|
2011-11-02 01:39:27 +01:00
|
|
|
std::cout << " looking for 'shape.input' plugin in... " << mapnik_dir << "input/" << "\n";
|
2012-09-07 17:23:03 +02:00
|
|
|
datasource_cache::instance().register_datasources(mapnik_dir + "input/");
|
2010-08-10 10:23:02 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// create styles
|
|
|
|
|
|
|
|
// Provinces (polygon)
|
|
|
|
feature_type_style provpoly_style;
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-01-13 22:35:01 +01:00
|
|
|
rule provpoly_rule_on;
|
2010-07-28 08:51:24 +02:00
|
|
|
provpoly_rule_on.set_filter(parse_expression("[NAME_EN] = 'Ontario'"));
|
|
|
|
provpoly_rule_on.append(polygon_symbolizer(color(250, 190, 183)));
|
|
|
|
provpoly_style.add_rule(provpoly_rule_on);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-01-13 22:35:01 +01:00
|
|
|
rule provpoly_rule_qc;
|
2010-07-28 08:51:24 +02:00
|
|
|
provpoly_rule_qc.set_filter(parse_expression("[NOM_FR] = 'Québec'"));
|
|
|
|
provpoly_rule_qc.append(polygon_symbolizer(color(217, 235, 203)));
|
|
|
|
provpoly_style.add_rule(provpoly_rule_qc);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("provinces",provpoly_style);
|
|
|
|
|
|
|
|
// Provinces (polyline)
|
|
|
|
feature_type_style provlines_style;
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
stroke provlines_stk (color(0,0,0),1.0);
|
|
|
|
provlines_stk.add_dash(8, 4);
|
|
|
|
provlines_stk.add_dash(2, 2);
|
|
|
|
provlines_stk.add_dash(2, 2);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-01-13 22:35:01 +01:00
|
|
|
rule provlines_rule;
|
2010-07-28 08:51:24 +02:00
|
|
|
provlines_rule.append(line_symbolizer(provlines_stk));
|
|
|
|
provlines_style.add_rule(provlines_rule);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("provlines",provlines_style);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
|
|
|
// Drainage
|
2010-07-28 08:51:24 +02:00
|
|
|
feature_type_style qcdrain_style;
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-01-13 22:35:01 +01:00
|
|
|
rule qcdrain_rule;
|
2010-07-28 08:51:24 +02:00
|
|
|
qcdrain_rule.set_filter(parse_expression("[HYC] = 8"));
|
|
|
|
qcdrain_rule.append(polygon_symbolizer(color(153, 204, 255)));
|
|
|
|
qcdrain_style.add_rule(qcdrain_rule);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("drainage",qcdrain_style);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Roads 3 and 4 (The "grey" roads)
|
2012-04-17 07:33:37 +02:00
|
|
|
feature_type_style roads34_style;
|
2011-01-13 22:35:01 +01:00
|
|
|
rule roads34_rule;
|
2010-07-28 08:51:24 +02:00
|
|
|
roads34_rule.set_filter(parse_expression("[CLASS] = 3 or [CLASS] = 4"));
|
|
|
|
stroke roads34_rule_stk(color(171,158,137),2.0);
|
|
|
|
roads34_rule_stk.set_line_cap(ROUND_CAP);
|
|
|
|
roads34_rule_stk.set_line_join(ROUND_JOIN);
|
|
|
|
roads34_rule.append(line_symbolizer(roads34_rule_stk));
|
|
|
|
roads34_style.add_rule(roads34_rule);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("smallroads",roads34_style);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Roads 2 (The thin yellow ones)
|
|
|
|
feature_type_style roads2_style_1;
|
2011-01-13 22:35:01 +01:00
|
|
|
rule roads2_rule_1;
|
2010-07-28 08:51:24 +02:00
|
|
|
roads2_rule_1.set_filter(parse_expression("[CLASS] = 2"));
|
|
|
|
stroke roads2_rule_stk_1(color(171,158,137),4.0);
|
|
|
|
roads2_rule_stk_1.set_line_cap(ROUND_CAP);
|
|
|
|
roads2_rule_stk_1.set_line_join(ROUND_JOIN);
|
|
|
|
roads2_rule_1.append(line_symbolizer(roads2_rule_stk_1));
|
|
|
|
roads2_style_1.add_rule(roads2_rule_1);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("road-border", roads2_style_1);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
feature_type_style roads2_style_2;
|
2011-01-13 22:35:01 +01:00
|
|
|
rule roads2_rule_2;
|
2010-07-28 08:51:24 +02:00
|
|
|
roads2_rule_2.set_filter(parse_expression("[CLASS] = 2"));
|
|
|
|
stroke roads2_rule_stk_2(color(255,250,115),2.0);
|
|
|
|
roads2_rule_stk_2.set_line_cap(ROUND_CAP);
|
|
|
|
roads2_rule_stk_2.set_line_join(ROUND_JOIN);
|
|
|
|
roads2_rule_2.append(line_symbolizer(roads2_rule_stk_2));
|
|
|
|
roads2_style_2.add_rule(roads2_rule_2);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
m.insert_style("road-fill", roads2_style_2);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Roads 1 (The big orange ones, the highways)
|
|
|
|
feature_type_style roads1_style_1;
|
2011-01-13 22:35:01 +01:00
|
|
|
rule roads1_rule_1;
|
2010-07-28 08:51:24 +02:00
|
|
|
roads1_rule_1.set_filter(parse_expression("[CLASS] = 1"));
|
|
|
|
stroke roads1_rule_stk_1(color(188,149,28),7.0);
|
|
|
|
roads1_rule_stk_1.set_line_cap(ROUND_CAP);
|
|
|
|
roads1_rule_stk_1.set_line_join(ROUND_JOIN);
|
|
|
|
roads1_rule_1.append(line_symbolizer(roads1_rule_stk_1));
|
|
|
|
roads1_style_1.add_rule(roads1_rule_1);
|
|
|
|
m.insert_style("highway-border", roads1_style_1);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
feature_type_style roads1_style_2;
|
2011-01-13 22:35:01 +01:00
|
|
|
rule roads1_rule_2;
|
2010-07-28 08:51:24 +02:00
|
|
|
roads1_rule_2.set_filter(parse_expression("[CLASS] = 1"));
|
|
|
|
stroke roads1_rule_stk_2(color(242,191,36),5.0);
|
|
|
|
roads1_rule_stk_2.set_line_cap(ROUND_CAP);
|
|
|
|
roads1_rule_stk_2.set_line_join(ROUND_JOIN);
|
|
|
|
roads1_rule_2.append(line_symbolizer(roads1_rule_stk_2));
|
|
|
|
roads1_style_2.add_rule(roads1_rule_2);
|
2012-04-17 07:33:37 +02:00
|
|
|
m.insert_style("highway-fill", roads1_style_2);
|
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// layers
|
|
|
|
// Provincial polygons
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
parameters p;
|
|
|
|
p["type"]="shape";
|
2011-11-02 01:39:27 +01:00
|
|
|
p["file"]="../../../demo/data/boundaries";
|
2012-04-17 07:33:37 +02:00
|
|
|
|
|
|
|
layer lyr("Provinces");
|
2012-09-07 17:23:03 +02:00
|
|
|
lyr.set_datasource(datasource_cache::instance().create(p));
|
2012-04-17 07:33:37 +02:00
|
|
|
lyr.add_style("provinces");
|
2011-05-04 17:53:36 +02:00
|
|
|
m.addLayer(lyr);
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Drainage
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
parameters p;
|
|
|
|
p["type"]="shape";
|
2011-11-02 01:39:27 +01:00
|
|
|
p["file"]="../../../demo/data/qcdrainage";
|
2011-05-04 17:53:36 +02:00
|
|
|
layer lyr("Quebec Hydrography");
|
2012-09-07 17:23:03 +02:00
|
|
|
lyr.set_datasource(datasource_cache::instance().create(p));
|
2012-04-17 07:33:37 +02:00
|
|
|
lyr.add_style("drainage");
|
2011-05-04 17:53:36 +02:00
|
|
|
m.addLayer(lyr);
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
parameters p;
|
|
|
|
p["type"]="shape";
|
2011-11-02 01:39:27 +01:00
|
|
|
p["file"]="../../../demo/data/ontdrainage";
|
2012-04-17 07:33:37 +02:00
|
|
|
|
|
|
|
layer lyr("Ontario Hydrography");
|
2012-09-07 17:23:03 +02:00
|
|
|
lyr.set_datasource(datasource_cache::instance().create(p));
|
2012-04-17 07:33:37 +02:00
|
|
|
lyr.add_style("drainage");
|
2011-05-04 17:53:36 +02:00
|
|
|
m.addLayer(lyr);
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Provincial boundaries
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
parameters p;
|
|
|
|
p["type"]="shape";
|
2011-11-02 01:39:27 +01:00
|
|
|
p["file"]="../../../demo/data/boundaries_l";
|
2012-04-17 07:33:37 +02:00
|
|
|
layer lyr("Provincial borders");
|
2012-09-07 17:23:03 +02:00
|
|
|
lyr.set_datasource(datasource_cache::instance().create(p));
|
2012-04-17 07:33:37 +02:00
|
|
|
lyr.add_style("provlines");
|
2011-05-04 17:53:36 +02:00
|
|
|
m.addLayer(lyr);
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2010-07-28 08:51:24 +02:00
|
|
|
// Roads
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
parameters p;
|
|
|
|
p["type"]="shape";
|
2012-04-17 07:33:37 +02:00
|
|
|
p["file"]="../../../demo/data/roads";
|
|
|
|
layer lyr("Roads");
|
2012-09-07 17:23:03 +02:00
|
|
|
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.addLayer(lyr);
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
2010-08-10 10:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void render_to_file(Map const& m, const std::string output_filename)
|
|
|
|
{
|
2010-07-28 08:51:24 +02:00
|
|
|
std::ofstream output_stream(output_filename.c_str());
|
|
|
|
|
|
|
|
if(output_stream)
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
typedef svg_renderer<std::ostream_iterator<char> > svg_ren;
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-05-04 17:53:36 +02:00
|
|
|
std::ostream_iterator<char> output_stream_iterator(output_stream);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-05-04 17:53:36 +02:00
|
|
|
svg_ren renderer(m, output_stream_iterator);
|
|
|
|
renderer.apply();
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-05-04 17:53:36 +02:00
|
|
|
output_stream.close();
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2012-10-31 03:21:34 +01:00
|
|
|
fs::path output_filename_path =
|
|
|
|
fs::system_complete(fs::path(".")) / fs::path(output_filename);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2012-10-31 03:21:34 +01:00
|
|
|
BOOST_CHECK_MESSAGE(fs::exists(output_filename_path),
|
2012-04-17 07:33:37 +02:00
|
|
|
"File '"+output_filename_path.string()+"' was created.");
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-04 17:53:36 +02:00
|
|
|
BOOST_FAIL("Could not create create/open file '"+output_filename+"'.");
|
2010-07-28 08:51:24 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 10:23:02 +02:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(path_element_test_case_1)
|
|
|
|
{
|
|
|
|
Map m(800,600);
|
2012-08-29 19:44:04 +02:00
|
|
|
m.set_background(parse_color("steelblue"));
|
2010-08-10 10:23:02 +02:00
|
|
|
|
|
|
|
prepare_map(m);
|
2012-04-17 07:33:37 +02:00
|
|
|
|
2011-05-04 17:53:36 +02:00
|
|
|
//m.zoom_to_box(box2d<double>(1405120.04127408, -247003.813399447,
|
2012-04-17 07:33:37 +02:00
|
|
|
//1706357.31328276, -25098.593149577));
|
|
|
|
m.zoom_all();
|
2010-08-10 10:23:02 +02:00
|
|
|
render_to_file(m, "path_element_test_case_1.svg");
|
|
|
|
}
|