File: path_element_test.cpp

package info (click to toggle)
mapnik 2.0.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,496 kB
  • sloc: cpp: 91,793; python: 6,051; xml: 3,528; sh: 848; makefile: 70; lisp: 10
file content (231 lines) | stat: -rw-r--r-- 7,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#define BOOST_TEST_MODULE path_element_tests

// boost.test
#include <boost/test/included/unit_test.hpp>

// boost.spirit
#include <boost/spirit/include/karma.hpp>

// boost.filesystem
#include <boost/filesystem.hpp>

// mapnik
#include <mapnik/map.hpp>
#include <mapnik/svg_renderer.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/font_engine_freetype.hpp>
#include <mapnik/filter_factory.hpp>
#include <mapnik/color_factory.hpp>

// stl
#include <fstream>
#include <iterator>

namespace filesystem = boost::filesystem;
using namespace mapnik;

void prepare_map(Map& m)
{      
    const std::string mapnik_dir("../../..");
    std::cout << " looking for 'shape.input' plugin in... " << mapnik_dir << "/plugins/input/" << "\n";
    datasource_cache::instance()->register_datasources(mapnik_dir + "/plugins/input/"); 

    // create styles

    // Provinces (polygon)
    feature_type_style provpoly_style;
       
    rule provpoly_rule_on;
    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);
        
    rule provpoly_rule_qc;
    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);
        
    m.insert_style("provinces",provpoly_style);

    // Provinces (polyline)
    feature_type_style provlines_style;
        
    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);
        
    rule provlines_rule;
    provlines_rule.append(line_symbolizer(provlines_stk));
    provlines_style.add_rule(provlines_rule);
        
    m.insert_style("provlines",provlines_style);
        
    // Drainage 
    feature_type_style qcdrain_style;
        
    rule qcdrain_rule;
    qcdrain_rule.set_filter(parse_expression("[HYC] = 8"));
    qcdrain_rule.append(polygon_symbolizer(color(153, 204, 255)));
    qcdrain_style.add_rule(qcdrain_rule);
        
    m.insert_style("drainage",qcdrain_style);
        
    // Roads 3 and 4 (The "grey" roads)
    feature_type_style roads34_style;    
    rule roads34_rule;
    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);
        
    m.insert_style("smallroads",roads34_style);
        
    // Roads 2 (The thin yellow ones)
    feature_type_style roads2_style_1;
    rule roads2_rule_1;
    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);
        
    m.insert_style("road-border", roads2_style_1);
        
    feature_type_style roads2_style_2;
    rule roads2_rule_2;
    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);
        
    m.insert_style("road-fill", roads2_style_2);
        
    // Roads 1 (The big orange ones, the highways)
    feature_type_style roads1_style_1;
    rule roads1_rule_1;
    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);
        
    feature_type_style roads1_style_2;
    rule roads1_rule_2;
    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);
    m.insert_style("highway-fill", roads1_style_2);        
       
    // layers
    // Provincial  polygons
    {
        parameters p;
        p["type"]="shape";
        p["file"]=mapnik_dir+"/demo/data/boundaries";
                
        layer lyr("Provinces"); 
        lyr.set_datasource(datasource_cache::instance()->create(p));
        lyr.add_style("provinces");    
        m.addLayer(lyr);
    }
        
    // Drainage
    {
        parameters p;
        p["type"]="shape";
        p["file"]=mapnik_dir+"/demo/data/qcdrainage";
        layer lyr("Quebec Hydrography");
        lyr.set_datasource(datasource_cache::instance()->create(p));
        lyr.add_style("drainage");    
        m.addLayer(lyr);
    }
        
    {
        parameters p;
        p["type"]="shape";
        p["file"]=mapnik_dir+"/demo/data/ontdrainage";
                
        layer lyr("Ontario Hydrography"); 
        lyr.set_datasource(datasource_cache::instance()->create(p));
        lyr.add_style("drainage");    
        m.addLayer(lyr);
    }
        
    // Provincial boundaries
    {
        parameters p;
        p["type"]="shape";
        p["file"]=mapnik_dir+"/demo/data/boundaries_l";
        layer lyr("Provincial borders"); 
        lyr.set_datasource(datasource_cache::instance()->create(p));
        lyr.add_style("provlines");    
        m.addLayer(lyr);
    }
    
    // Roads
    {
        parameters p;
        p["type"]="shape";
        p["file"]=mapnik_dir+"/demo/data/roads";        
        layer lyr("Roads"); 
        lyr.set_datasource(datasource_cache::instance()->create(p));
        lyr.add_style("smallroads");
        lyr.add_style("road-border");
        lyr.add_style("road-fill");
        lyr.add_style("highway-border");
        lyr.add_style("highway-fill");
    
        m.addLayer(lyr);        
    }
}

void render_to_file(Map const& m, const std::string output_filename)
{
    std::ofstream output_stream(output_filename.c_str());

    if(output_stream)
    {
        typedef svg_renderer<std::ostream_iterator<char> > svg_ren;
    
        std::ostream_iterator<char> output_stream_iterator(output_stream);
    
        svg_ren renderer(m, output_stream_iterator);
        renderer.apply();
    
        output_stream.close();
    
        filesystem::path output_filename_path = 
            filesystem::system_complete(filesystem::path(".")) / filesystem::path(output_filename);
    
        BOOST_CHECK_MESSAGE(filesystem::exists(output_filename_path), 
           "File '"+output_filename_path.string()+"' was created.");
    }
    else
    {
        BOOST_FAIL("Could not create create/open file '"+output_filename+"'.");
    }
}

BOOST_AUTO_TEST_CASE(path_element_test_case_1)
{
    Map m(800,600);
    m.set_background(color_factory::from_string("steelblue"));      

    prepare_map(m);
        
    //m.zoom_to_box(box2d<double>(1405120.04127408, -247003.813399447,
                //1706357.31328276, -25098.593149577));
    m.zoom_all();    
    render_to_file(m, "path_element_test_case_1.svg");
}