-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathvector_tile_datasource.hpp
423 lines (398 loc) · 16.9 KB
/
vector_tile_datasource.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
#ifndef __MAPNIK_VECTOR_TILE_DATASOURCE_H__
#define __MAPNIK_VECTOR_TILE_DATASOURCE_H__
#include "vector_tile.pb.h"
#include "vector_tile_projection.hpp"
#include <mapnik/box2d.hpp>
#include <mapnik/coord.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/params.hpp>
#include <mapnik/query.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/version.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/well_known_srs.hpp>
#include <mapnik/version.hpp>
#include <mapnik/vertex.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/geom_util.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/raster.hpp>
#include <memory>
#include <stdexcept>
#include <string>
#include <boost/optional.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <unicode/unistr.h>
#include "mapnik3x_compatibility.hpp"
#include MAPNIK_MAKE_SHARED_INCLUDE
#include MAPNIK_SHARED_INCLUDE
#include MAPNIK_VIEW_TRANSFORM_INCLUDE
namespace mapnik { namespace vector_tile_impl {
void add_attributes(mapnik::feature_ptr feature,
vector_tile::Tile_Feature const& f,
vector_tile::Tile_Layer const& layer,
mapnik::transcoder const& tr)
{
std::size_t num_keys = static_cast<std::size_t>(layer.keys_size());
std::size_t num_values = static_cast<std::size_t>(layer.values_size());
for (int m = 0; m < f.tags_size(); m += 2)
{
std::size_t key_name = f.tags(m);
std::size_t key_value = f.tags(m + 1);
if (key_name < num_keys
&& key_value < num_values)
{
std::string const& name = layer.keys(key_name);
if (feature->has_key(name))
{
vector_tile::Tile_Value const& value = layer.values(key_value);
if (value.has_string_value())
{
std::string const& str = value.string_value();
feature->put(name, tr.transcode(str.data(), str.length()));
}
else if (value.has_int_value())
{
feature->put(name, static_cast<mapnik::value_integer>(value.int_value()));
}
else if (value.has_double_value())
{
feature->put(name, static_cast<mapnik::value_double>(value.double_value()));
}
else if (value.has_float_value())
{
feature->put(name, static_cast<mapnik::value_double>(value.float_value()));
}
else if (value.has_bool_value())
{
feature->put(name, static_cast<mapnik::value_bool>(value.bool_value()));
}
else if (value.has_sint_value())
{
feature->put(name, static_cast<mapnik::value_integer>(value.sint_value()));
}
else if (value.has_uint_value())
{
feature->put(name, static_cast<mapnik::value_integer>(value.uint_value()));
}
}
}
}
}
template <typename Filter>
class tile_featureset : public Featureset
{
public:
tile_featureset(Filter const& filter,
mapnik::box2d<double> const& tile_extent,
mapnik::box2d<double> const& unbuffered_query,
std::set<std::string> const& attribute_names,
vector_tile::Tile_Layer const& layer,
double tile_x,
double tile_y,
double scale,
bool multi_geom=false)
: filter_(filter),
tile_extent_(tile_extent),
unbuffered_query_(unbuffered_query),
layer_(layer),
tile_x_(tile_x),
tile_y_(tile_y),
scale_(scale),
itr_(0),
end_(layer_.features_size()),
tr_("utf-8"),
multi_geom_(multi_geom),
ctx_(MAPNIK_MAKE_SHARED<mapnik::context_type>())
{
std::set<std::string>::const_iterator pos = attribute_names.begin();
std::set<std::string>::const_iterator end = attribute_names.end();
for ( ;pos !=end; ++pos)
{
for (int i = 0; i < layer_.keys_size(); ++i)
{
if (layer_.keys(i) == *pos)
{
ctx_->push(*pos);
break;
}
}
}
}
virtual ~tile_featureset() {}
feature_ptr next()
{
while (itr_ < end_)
{
vector_tile::Tile_Feature const& f = layer_.features(itr_);
mapnik::value_integer feature_id = itr_++;
if (f.has_id())
{
feature_id = f.id();
}
if (f.has_raster())
{
std::string const& image_buffer = f.raster();
MAPNIK_UNIQUE_PTR<mapnik::image_reader> reader(mapnik::get_image_reader(image_buffer.data(),image_buffer.size()));
if (reader.get())
{
int image_width = reader->width();
int image_height = reader->height();
if (image_width > 0 && image_height > 0)
{
MAPNIK_VIEW_TRANSFORM t(image_width, image_height, tile_extent_, 0, 0);
box2d<double> intersect = tile_extent_.intersect(unbuffered_query_);
box2d<double> ext = t.forward(intersect);
if (ext.width() > 0.5 && ext.height() > 0.5 )
{
// select minimum raster containing whole ext
int x_off = static_cast<int>(std::floor(ext.minx() +.5));
int y_off = 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));
// clip to available data
if (x_off < 0)
x_off = 0;
if (y_off < 0)
y_off = 0;
if (end_x > image_width)
end_x = image_width;
if (end_y > image_height)
end_y = image_height;
int width = end_x - x_off;
int height = end_y - y_off;
box2d<double> feature_raster_extent(x_off,
y_off,
x_off + width,
y_off + height);
intersect = t.backward(feature_raster_extent);
#if MAPNIK_VERSION >= 300000
double filter_factor = 1.0;
#endif
bool premultiplied = false;
mapnik::raster_ptr raster = MAPNIK_MAKE_SHARED<mapnik::raster>(intersect,
width,
height,
#if MAPNIK_VERSION >= 300000
filter_factor,
#endif
premultiplied
);
reader->read(x_off, y_off, raster->data_);
mapnik::feature_ptr feature = mapnik::feature_factory::create(ctx_,feature_id);
feature->set_raster(raster);
add_attributes(feature,f,layer_,tr_);
return feature;
}
}
}
}
if (f.geometry_size() <= 0)
{
continue;
}
int cmd = -1;
const int cmd_bits = 3;
unsigned length = 0;
double x = tile_x_, y = tile_y_;
bool first = true;
mapnik::box2d<double> envelope;
double first_x=0;
double first_y=0;
mapnik::feature_ptr feature = mapnik::feature_factory::create(ctx_,feature_id);
feature->paths().push_back(new mapnik::geometry_type(MAPNIK_GEOM_TYPE(f.type())));
mapnik::geometry_type * geom = &feature->paths().front();
for (int k = 0; k < f.geometry_size();)
{
if (!length) {
unsigned cmd_length = f.geometry(k++);
cmd = cmd_length & ((1 << cmd_bits) - 1);
length = cmd_length >> cmd_bits;
}
if (length > 0) {
length--;
if (cmd == mapnik::SEG_MOVETO || cmd == mapnik::SEG_LINETO)
{
int32_t dx = f.geometry(k++);
int32_t dy = f.geometry(k++);
dx = ((dx >> 1) ^ (-(dx & 1)));
dy = ((dy >> 1) ^ (-(dy & 1)));
x += (static_cast<double>(dx) / scale_);
y -= (static_cast<double>(dy) / scale_);
if (cmd == mapnik::SEG_MOVETO)
{
if (multi_geom_ && !first) {
feature->paths().push_back(new mapnik::geometry_type(MAPNIK_GEOM_TYPE(f.type())));
geom = &feature->paths().back();
}
first_x = x;
first_y = y;
}
if (first)
{
envelope.init(x,y,x,y);
first = false;
}
else
{
envelope.expand_to_include(x,y);
}
geom->push_vertex(x, y, static_cast<mapnik::CommandType>(cmd));
}
else if (cmd == (mapnik::SEG_CLOSE & ((1 << cmd_bits) - 1)))
{
geom->push_vertex(first_x, first_y, mapnik::SEG_LINETO);
geom->push_vertex(0, 0, mapnik::SEG_CLOSE);
}
else
{
std::stringstream msg;
msg << "Unknown command type (tile_featureset): "
<< cmd;
throw std::runtime_error(msg.str());
}
}
}
if (!filter_.pass(envelope))
{
continue;
}
add_attributes(feature,f,layer_,tr_);
return feature;
}
return feature_ptr();
}
private:
Filter filter_;
mapnik::box2d<double> tile_extent_;
mapnik::box2d<double> unbuffered_query_;
vector_tile::Tile_Layer const& layer_;
double tile_x_;
double tile_y_;
double scale_;
unsigned itr_;
unsigned end_;
mapnik::transcoder tr_;
bool multi_geom_;
mapnik::context_ptr ctx_;
};
class tile_datasource : public datasource
{
public:
tile_datasource(vector_tile::Tile_Layer const& layer,
unsigned x,
unsigned y,
unsigned z,
unsigned tile_size,
bool multi_geom=false);
virtual ~tile_datasource();
datasource::datasource_t type() const;
featureset_ptr features(query const& q) const;
featureset_ptr features_at_point(coord2d const& pt, double tol = 0) const;
void set_envelope(box2d<double> const& bbox);
box2d<double> get_tile_extent() const;
box2d<double> envelope() const;
boost::optional<geometry_t> get_geometry_type() const;
layer_descriptor get_descriptor() const;
private:
mutable mapnik::layer_descriptor desc_;
mutable bool attributes_added_;
vector_tile::Tile_Layer const& layer_;
unsigned x_;
unsigned y_;
unsigned z_;
unsigned tile_size_;
mutable bool extent_initialized_;
bool multi_geom_;
mutable mapnik::box2d<double> extent_;
double tile_x_;
double tile_y_;
double scale_;
};
// tile_datasource impl
inline tile_datasource::tile_datasource(vector_tile::Tile_Layer const& layer,
unsigned x,
unsigned y,
unsigned z,
unsigned tile_size,
bool multi_geom)
: datasource(parameters()),
desc_("in-memory datasource","utf-8"),
attributes_added_(false),
layer_(layer),
x_(x),
y_(y),
z_(z),
tile_size_(tile_size),
extent_initialized_(false),
multi_geom_(multi_geom) {
double resolution = mapnik::EARTH_CIRCUMFERENCE/(1 << z_);
tile_x_ = -0.5 * mapnik::EARTH_CIRCUMFERENCE + x_ * resolution;
tile_y_ = 0.5 * mapnik::EARTH_CIRCUMFERENCE - y_ * resolution;
scale_ = (static_cast<double>(layer_.extent()) / tile_size_) * tile_size_/resolution;
}
inline tile_datasource::~tile_datasource() {}
inline datasource::datasource_t tile_datasource::type() const
{
return datasource::Vector;
}
inline featureset_ptr tile_datasource::features(query const& q) const
{
mapnik::filter_in_box filter(q.get_bbox());
return MAPNIK_MAKE_SHARED<tile_featureset<mapnik::filter_in_box> >
(filter, get_tile_extent(), q.get_unbuffered_bbox(), q.property_names(), layer_, tile_x_, tile_y_, scale_, multi_geom_);
}
inline featureset_ptr tile_datasource::features_at_point(coord2d const& pt, double tol) const
{
mapnik::filter_at_point filter(pt,tol);
std::set<std::string> names;
for (int i = 0; i < layer_.keys_size(); ++i)
{
names.insert(layer_.keys(i));
}
return MAPNIK_MAKE_SHARED<tile_featureset<filter_at_point> >
(filter, get_tile_extent(), get_tile_extent(), names, layer_, tile_x_, tile_y_, scale_, multi_geom_);
}
inline void tile_datasource::set_envelope(box2d<double> const& bbox)
{
extent_initialized_ = true;
extent_ = bbox;
}
inline box2d<double> tile_datasource::get_tile_extent() const
{
spherical_mercator merc(tile_size_);
double minx,miny,maxx,maxy;
merc.xyz(x_,y_,z_,minx,miny,maxx,maxy);
return box2d<double>(minx,miny,maxx,maxy);
}
inline box2d<double> tile_datasource::envelope() const
{
if (!extent_initialized_)
{
extent_ = get_tile_extent();
extent_initialized_ = true;
}
return extent_;
}
inline boost::optional<datasource::geometry_t> tile_datasource::get_geometry_type() const
{
return datasource::Collection;
}
inline layer_descriptor tile_datasource::get_descriptor() const
{
if (!attributes_added_)
{
for (int i = 0; i < layer_.keys_size(); ++i)
{
// Object type here because we don't know the precise value until features are unpacked
desc_.add_descriptor(attribute_descriptor(layer_.keys(i), Object));
}
attributes_added_ = true;
}
return desc_;
}
}} // end ns
#endif // __MAPNIK_VECTOR_TILE_DATASOURCE_H__