-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathvector_tile_processor.hpp
437 lines (420 loc) · 18.1 KB
/
vector_tile_processor.hpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#ifndef __MAPNIK_VECTOR_PROCESSOR_H__
#define __MAPNIK_VECTOR_PROCESSOR_H__
#include <mapnik/map.hpp>
#include <mapnik/request.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/query.hpp>
#include <mapnik/ctrans.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/proj_transform.hpp>
#include <mapnik/scale_denominator.hpp>
#include <mapnik/attribute_descriptor.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/config.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/version.hpp>
#include <mapnik/noncopyable.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/raster.hpp>
#include <mapnik/warp.hpp>
#include <mapnik/version.hpp>
#include <mapnik/image_scaling.hpp>
#include <mapnik/image_compositing.hpp>
// agg
#ifdef CONV_CLIPPER
#include "agg_path_storage.h"
#include "agg_conv_clipper.h"
#else
#include "agg_conv_clip_polygon.h"
#endif
#include "agg_conv_clip_polyline.h"
#include "agg_rendering_buffer.h"
#include "agg_pixfmt_rgba.h"
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
#include <string>
#include <stdexcept>
#include "mapnik3x_compatibility.hpp"
#include MAPNIK_MAKE_SHARED_INCLUDE
#include MAPNIK_SHARED_INCLUDE
namespace mapnik { namespace vector {
/*
This processor combines concepts from mapnik's
feature_style_processor and agg_renderer. It
differs in that here we only process layers in
isolation of their styles, and because of this we need
options for clipping and simplification, for example,
that would normally come from a style's symbolizers
*/
template <typename T>
class processor : private mapnik::noncopyable
{
public:
typedef T backend_type;
private:
backend_type & backend_;
mapnik::Map const& m_;
mapnik::request const& m_req_;
double scale_factor_;
mapnik::CoordTransform t_;
unsigned tolerance_;
std::string image_format_;
scaling_method_e scaling_method_;
bool painted_;
public:
processor(T & backend,
mapnik::Map const& map,
mapnik::request const& m_req,
double scale_factor=1.0,
unsigned offset_x=0,
unsigned offset_y=0,
unsigned tolerance=1,
std::string const& image_format="jpeg",
scaling_method_e scaling_method=SCALING_NEAR
)
: backend_(backend),
m_(map),
m_req_(m_req),
scale_factor_(scale_factor),
t_(m_req.width(),m_req.height(),m_req.extent(),offset_x,offset_y),
tolerance_(tolerance),
image_format_(image_format),
scaling_method_(scaling_method),
painted_(false) {}
void apply(double scale_denom=0.0)
{
mapnik::projection proj(m_.srs(),true);
if (scale_denom <= 0.0)
{
scale_denom = mapnik::scale_denominator(m_req_.scale(),proj.is_geographic());
}
scale_denom *= scale_factor_;
BOOST_FOREACH ( mapnik::layer const& lay, m_.layers() )
{
if (lay.visible(scale_denom))
{
apply_to_layer(lay,
proj,
m_req_.scale(),
scale_denom,
m_req_.width(),
m_req_.height(),
m_req_.extent(),
m_req_.buffer_size());
}
}
}
bool painted() const
{
return painted_;
}
void apply_to_layer(mapnik::layer const& lay,
mapnik::projection const& proj0,
double scale,
double scale_denom,
unsigned width,
unsigned height,
box2d<double> const& extent,
int buffer_size)
{
mapnik::datasource_ptr ds = lay.datasource();
if (!ds)
{
return;
}
mapnik::projection proj1(lay.srs(),true);
mapnik::proj_transform prj_trans(proj0,proj1);
box2d<double> query_ext = extent; // unbuffered
box2d<double> buffered_query_ext(query_ext); // buffered
double buffer_padding = 2.0 * scale;
boost::optional<int> layer_buffer_size = lay.buffer_size();
if (layer_buffer_size) // if layer overrides buffer size, use this value to compute buffered extent
{
buffer_padding *= *layer_buffer_size;
}
else
{
buffer_padding *= buffer_size;
}
buffered_query_ext.width(query_ext.width() + buffer_padding);
buffered_query_ext.height(query_ext.height() + buffer_padding);
// clip buffered extent by maximum extent, if supplied
boost::optional<box2d<double> > const& maximum_extent = m_.maximum_extent();
if (maximum_extent)
{
buffered_query_ext.clip(*maximum_extent);
}
mapnik::box2d<double> layer_ext = lay.envelope();
bool fw_success = false;
bool early_return = false;
// first, try intersection of map extent forward projected into layer srs
if (prj_trans.forward(buffered_query_ext, PROJ_ENVELOPE_POINTS) && buffered_query_ext.intersects(layer_ext))
{
fw_success = true;
layer_ext.clip(buffered_query_ext);
}
// if no intersection and projections are also equal, early return
else if (prj_trans.equal())
{
early_return = true;
}
// next try intersection of layer extent back projected into map srs
else if (prj_trans.backward(layer_ext, PROJ_ENVELOPE_POINTS) && buffered_query_ext.intersects(layer_ext))
{
layer_ext.clip(buffered_query_ext);
// forward project layer extent back into native projection
if (! prj_trans.forward(layer_ext, PROJ_ENVELOPE_POINTS))
{
std::cerr << "feature_style_processor: Layer=" << lay.name()
<< " extent=" << layer_ext << " in map projection "
<< " did not reproject properly back to layer projection";
}
}
else
{
// if no intersection then nothing to do for layer
early_return = true;
}
if (early_return)
{
return;
}
// if we've got this far, now prepare the unbuffered extent
// which is used as a bbox for clipping geometries
if (maximum_extent)
{
query_ext.clip(*maximum_extent);
}
mapnik::box2d<double> layer_ext2 = lay.envelope();
if (fw_success)
{
if (prj_trans.forward(query_ext, PROJ_ENVELOPE_POINTS))
{
layer_ext2.clip(query_ext);
}
}
else
{
if (prj_trans.backward(layer_ext2, PROJ_ENVELOPE_POINTS))
{
layer_ext2.clip(query_ext);
prj_trans.forward(layer_ext2, PROJ_ENVELOPE_POINTS);
}
}
double qw = query_ext.width()>0 ? query_ext.width() : 1;
double qh = query_ext.height()>0 ? query_ext.height() : 1;
mapnik::query::resolution_type res(width/qw,
height/qh);
mapnik::query q(layer_ext,res,scale_denom,extent);
mapnik::layer_descriptor lay_desc = ds->get_descriptor();
BOOST_FOREACH(mapnik::attribute_descriptor const& desc, lay_desc.get_descriptors())
{
q.add_property_name(desc.get_name());
}
mapnik::featureset_ptr features = ds->features(q);
if (!features)
{
return;
}
mapnik::feature_ptr feature = features->next();
if (feature) {
backend_.start_tile_layer(lay.name());
raster_ptr const& source = feature->get_raster();
if (source)
{
box2d<double> target_ext = box2d<double>(source->ext_);
prj_trans.backward(target_ext, PROJ_ENVELOPE_POINTS);
box2d<double> ext = t_.forward(target_ext);
int start_x = static_cast<int>(std::floor(ext.minx()+.5));
int start_y = static_cast<int>(std::floor(ext.miny()+.5));
int end_x = static_cast<int>(std::floor(ext.maxx()+.5));
int end_y = static_cast<int>(std::floor(ext.maxy()+.5));
int raster_width = end_x - start_x;
int raster_height = end_y - start_y;
if (raster_width > 0 && raster_height > 0)
{
#if MAPNIK_VERSION >= 300000
raster target(target_ext, raster_width, raster_height, source->get_filter_factor());
#else
raster target(target_ext, raster_width, raster_height);
#endif
if (!source->premultiplied_alpha_)
{
agg::rendering_buffer buffer(source->data_.getBytes(),
source->data_.width(),
source->data_.height(),
source->data_.width() * 4);
agg::pixfmt_rgba32 pixf(buffer);
pixf.premultiply();
}
if (!prj_trans.equal())
{
double offset_x = ext.minx() - start_x;
double offset_y = ext.miny() - start_y;
#if MAPNIK_VERSION >= 300000
reproject_and_scale_raster(target, *source, prj_trans,
offset_x, offset_y,
width,
scaling_method_);
#else
reproject_and_scale_raster(target, *source, prj_trans,
offset_x, offset_y,
width,
2.0,
scaling_method_);
#endif
}
else
{
double image_ratio_x = ext.width() / source->data_.width();
double image_ratio_y = ext.height() / source->data_.height();
#if MAPNIK_VERSION >= 300000
scale_image_agg<image_data_32>(target.data_,
source->data_,
scaling_method_,
image_ratio_x,
image_ratio_y,
0.0,
0.0,
source->get_filter_factor());
#else
scale_image_agg<image_data_32>(target.data_,
source->data_,
scaling_method_,
image_ratio_x,
image_ratio_y,
0.0,
0.0,
2.0);
#endif
}
mapnik::image_data_32 im_tile(width,height);
composite(im_tile, target.data_,
src_over, 1,
start_x, start_y, false);
agg::rendering_buffer buffer(im_tile.getBytes(),
im_tile.width(),
im_tile.height(),
im_tile.width() * 4);
agg::pixfmt_rgba32 pixf(buffer);
pixf.demultiply();
backend_.start_tile_feature(*feature);
backend_.add_tile_feature_raster(mapnik::save_to_string(im_tile,image_format_));
}
backend_.stop_tile_layer();
return;
}
// vector pathway
while (feature)
{
boost::ptr_vector<mapnik::geometry_type> & paths = feature->paths();
if (paths.empty()) {
feature = features->next();
continue;
}
backend_.start_tile_feature(*feature);
BOOST_FOREACH( mapnik::geometry_type & geom, paths)
{
mapnik::box2d<double> geom_box = geom.envelope();
if (!geom_box.intersects(buffered_query_ext))
{
continue;
}
if (handle_geometry(geom,
prj_trans,
buffered_query_ext) > 0)
{
painted_ = true;
}
}
backend_.stop_tile_feature();
feature = features->next();
}
backend_.stop_tile_layer();
}
}
unsigned handle_geometry(mapnik::geometry_type & geom,
mapnik::proj_transform const& prj_trans,
mapnik::box2d<double> const& buffered_query_ext)
{
unsigned path_count = 0;
switch (geom.type())
{
case MAPNIK_POINT:
{
if (geom.size() > 0)
{
typedef mapnik::coord_transform<mapnik::CoordTransform,
mapnik::geometry_type> path_type;
path_type path(t_, geom, prj_trans);
path_count = backend_.add_path(path, tolerance_, geom.type());
}
break;
}
case MAPNIK_LINESTRING:
{
if (geom.size() > 1)
{
typedef agg::conv_clip_polyline<mapnik::geometry_type> line_clipper;
line_clipper clipped(geom);
clipped.clip_box(
buffered_query_ext.minx(),
buffered_query_ext.miny(),
buffered_query_ext.maxx(),
buffered_query_ext.maxy());
typedef mapnik::coord_transform<mapnik::CoordTransform, line_clipper> path_type;
path_type path(t_, clipped, prj_trans);
path_count = backend_.add_path(path, tolerance_, geom.type());
}
break;
}
case MAPNIK_POLYGON:
{
if (geom.size() > 2)
{
#ifdef CONV_CLIPPER
agg::path_storage ps;
ps.move_to(buffered_query_ext.minx(), buffered_query_ext.miny());
ps.line_to(buffered_query_ext.minx(), buffered_query_ext.maxy());
ps.line_to(buffered_query_ext.maxx(), buffered_query_ext.maxy());
ps.line_to(buffered_query_ext.maxx(), buffered_query_ext.miny());
ps.close_polygon();
typedef agg::conv_clipper<mapnik::geometry_type, agg::path_storage> poly_clipper;
poly_clipper clipped(geom,ps,
agg::clipper_and,
agg::clipper_non_zero,
agg::clipper_non_zero,
1);
//clipped.rewind(0);
#else
typedef agg::conv_clip_polygon<mapnik::geometry_type> poly_clipper;
poly_clipper clipped(geom);
clipped.clip_box(
buffered_query_ext.minx(),
buffered_query_ext.miny(),
buffered_query_ext.maxx(),
buffered_query_ext.maxy());
#endif
typedef mapnik::coord_transform<mapnik::CoordTransform, poly_clipper> path_type;
path_type path(t_, clipped, prj_trans);
path_count = backend_.add_path(path, tolerance_, geom.type());
}
break;
}
case MAPNIK_UNKNOWN:
default:
{
throw std::runtime_error("unhandled geometry type");
break;
}
}
return path_count;
}
};
}} // end ns
#endif // __MAPNIK_VECTOR_TILE_PROCESSOR_H__