forked from mapnik/python-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapnik_vector_tile.cpp
More file actions
320 lines (288 loc) · 11.8 KB
/
Copy pathmapnik_vector_tile.cpp
File metadata and controls
320 lines (288 loc) · 11.8 KB
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko, Jean-Francois Doyon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#include <mapnik/config.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_processor.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_compression.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_load_tile.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_datasource_pbf.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_tile.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_wafer.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_merc_tile.hpp>
#define BOOST_PYTHON_MAX_ARITY 20
#include <boost/python.hpp>
boost::python::object create_mvt_merc(
mapnik::Map const& map,
std::uint64_t x,
std::uint64_t y,
std::uint64_t z,
std::uint32_t tile_size,
boost::optional<std::int32_t> buffer_size,
double scale_denom,
int offset_x,
int offset_y,
bool style_level_filter,
double simplify_distance,
double area_threshold,
bool process_all_rings,
bool multi_polygon_union,
mapnik::vector_tile_impl::polygon_fill_type fill_type,
std::string const& image_format,
mapnik::scaling_method_e scaling_method,
std::launch threading_mode)
{
mapnik::vector_tile_impl::processor proc(map);
proc.set_area_threshold(area_threshold);
proc.set_simplify_distance(simplify_distance);
proc.set_multi_polygon_union(multi_polygon_union);
proc.set_process_all_rings(process_all_rings);
proc.set_fill_type(fill_type);
proc.set_image_format(image_format);
proc.set_scaling_method(scaling_method);
proc.set_threading_mode(threading_mode);
mapnik::vector_tile_impl::merc_tile tile(proc.create_tile(
x, y, z, tile_size, buffer_size, scale_denom,
offset_x, offset_y, style_level_filter));
std::string const& buffer = tile.get_buffer();
return boost::python::object(boost::python::handle<>(
PyBytes_FromStringAndSize(buffer.data(), buffer.size())));
}
boost::python::list create_mvt_wafer_merc(
mapnik::Map const& map,
std::uint64_t x,
std::uint64_t y,
std::uint64_t z,
unsigned span,
std::uint32_t tile_size,
boost::optional<std::int32_t> buffer_size,
double scale_denom,
int offset_x,
int offset_y,
bool style_level_filter,
double simplify_distance,
double area_threshold,
bool process_all_rings,
bool multi_polygon_union,
mapnik::vector_tile_impl::polygon_fill_type fill_type,
std::string const& image_format,
mapnik::scaling_method_e scaling_method,
std::launch threading_mode)
{
mapnik::vector_tile_impl::processor proc(map);
proc.set_area_threshold(area_threshold);
proc.set_simplify_distance(simplify_distance);
proc.set_multi_polygon_union(multi_polygon_union);
proc.set_process_all_rings(process_all_rings);
proc.set_fill_type(fill_type);
proc.set_image_format(image_format);
proc.set_scaling_method(scaling_method);
proc.set_threading_mode(threading_mode);
mapnik::vector_tile_impl::merc_wafer wafer(proc.create_wafer(
x, y, z, span, tile_size, buffer_size, scale_denom,
offset_x, offset_y, style_level_filter));
boost::python::list tiles;
for (auto const & tile : wafer.tiles())
{
std::string const& buffer = tile.get_buffer();
tiles.append(boost::python::object(boost::python::handle<>(
PyBytes_FromStringAndSize(buffer.data(), buffer.size()))));
}
return tiles;
}
boost::python::object compress_mvt(std::string const& input)
{
std::string output;
mapnik::vector_tile_impl::zlib_compress(input, output);
return boost::python::object(boost::python::handle<>(
PyBytes_FromStringAndSize(output.data(), output.size())));
}
boost::python::object decompress_mvt(std::string const& input)
{
std::string output;
mapnik::vector_tile_impl::zlib_decompress(input, output);
return boost::python::object(boost::python::handle<>(
PyBytes_FromStringAndSize(output.data(), output.size())));
}
void export_mvt_create()
{
using namespace boost::python;
using fill_type = mapnik::vector_tile_impl::polygon_fill_type;
enum_<fill_type>("polygon_fill_type")
.value("even_odd", fill_type::even_odd_fill)
.value("non_zero", fill_type::non_zero_fill)
.value("positive", fill_type::positive_fill)
.value("negative", fill_type::negative_fill)
;
def("create_mvt_merc", &create_mvt_merc,
(arg("map"),
arg("x"),
arg("y"),
arg("z"),
arg("tile_size") = 4096,
// If None, buffer size is taken from the Map.
arg("buffer_size") = boost::optional<std::int32_t>(),
arg("scale_denom") = 0.0,
arg("offset_x") = 0,
arg("offset_y") = 0,
// Filter features by rule conditions in styles.
arg("style_level_filter") = false,
// Positive value in the units of vector tile coordinates will turn on
// douglas-peucker with given simplification distance.
arg("simplify_distance") = 0.0,
// Skip polygons with area below this threshold,
// in vector tile coordinates units.
arg("area_threshold") = 0.1,
// Process all rings even exterior ring is degenerated
// or smaller than area_threshold.
arg("process_all_rings") = false,
// Conflate multi-polygon.
arg("multi_polygon_union") = false,
// Polygon fill strategy used during clipping.
arg("fill_type") = fill_type::positive_fill,
// Raster image format.
arg("image_format") = std::string("tiff"),
// Raster scaling method.
arg("scaling_method") = mapnik::SCALING_BILINEAR,
// Allows parallel processing of layers.
arg("threading_mode") = std::launch::deferred),
"Creates MVT into a buffer\n"
"mapnik.create_mvt_merc(m, 2257, 1393, 12, 4096, 0, 0, 0, 0)");
def("create_mvt_wafer_merc", &create_mvt_wafer_merc,
(arg("map"),
arg("x"),
arg("y"),
arg("z"),
arg("span"),
arg("tile_size") = 4096,
// If None, buffer size is taken from the Map.
arg("buffer_size") = boost::optional<std::int32_t>(),
arg("scale_denom") = 0.0,
arg("offset_x") = 0,
arg("offset_y") = 0,
// Filter features by rule conditions in styles.
arg("style_level_filter") = false,
// Positive value in the units of vector tile coordinates will turn on
// douglas-peucker with given simplification distance.
arg("simplify_distance") = 0.0,
// Skip polygons with area below this threshold,
// in vector tile coordinates units.
arg("area_threshold") = 0.1,
// Process all rings even exterior ring is degenerated
// or smaller than area_threshold.
arg("process_all_rings") = false,
// Conflate multi-polygon.
arg("multi_polygon_union") = false,
// Polygon fill strategy used during clipping.
arg("fill_type") = fill_type::positive_fill,
// Raster image format.
arg("image_format") = std::string("tiff"),
// Raster scaling method.
arg("scaling_method") = mapnik::SCALING_BILINEAR,
// Allows parallel processing of layers.
arg("threading_mode") = std::launch::deferred),
"Creates MVT into a buffer\n"
"mapnik.create_mvt_wafer_merc(m, 2257, 1393, 12, 8, 4096, 0, 0, 0, 0)");
def("compress_mvt", &compress_mvt,
"gzip compression");
def("decompress_mvt", &decompress_mvt,
"decompress zlib or gzip compressed data");
}
void merge_compressed_buffer(mapnik::vector_tile_impl::merc_tile & tile,
std::string const& buf)
{
merge_from_compressed_buffer(tile, buf.data(), buf.size());
}
void add_image_layer(mapnik::vector_tile_impl::merc_tile & tile,
std::string const& layer_name,
std::string const& image_buffer)
{
add_image_buffer_as_tile_layer(tile,
layer_name,
image_buffer.data(),
image_buffer.size());
}
boost::python::object mvt_get_buffer(mapnik::vector_tile_impl::tile const& tile)
{
std::string const& buf = tile.get_buffer();
return boost::python::object(boost::python::handle<>(
PyBytes_FromStringAndSize(buf.data(), buf.size())));
}
extern void export_mvt_render();
extern void export_mvt_preview();
extern void export_mvt_info();
void export_mvt()
{
using namespace boost::python;
using mapnik::vector_tile_impl::tile;
using mapnik::vector_tile_impl::merc_tile;
using tile_size_get = std::uint32_t (tile::*)() const;
using tile_size_set = void (tile::*)(std::uint32_t);
using buffer_size_get = std::int32_t (tile::*)() const;
using buffer_size_set = void (tile::*)(std::int32_t);
using xyz_get = std::uint64_t (merc_tile::*)() const;
using xyz_set = void (merc_tile::*)(std::uint64_t);
class_<tile>("VectorTile",
"Represents a MVT",
init<mapnik::box2d<double> const&,
std::uint32_t, std::int32_t>(
(arg("extent"),
arg("tile_size") = 4096,
arg("buffer_size") = 128)
))
.add_property("tile_size",
static_cast<tile_size_get>(&merc_tile::tile_size),
static_cast<tile_size_set>(&merc_tile::tile_size))
.add_property("buffer_size",
static_cast<buffer_size_get>(&merc_tile::buffer_size),
static_cast<buffer_size_set>(&merc_tile::buffer_size))
.add_property("is_empty", &merc_tile::is_empty)
.add_property("is_painted", &merc_tile::is_painted)
.add_property("extent", make_function(&merc_tile::extent,
return_value_policy<copy_const_reference>()))
.def("get_buffer", mvt_get_buffer)
;
class_<merc_tile, bases<tile>>("VectorTileMerc",
"A specialization of MVT for Mercator",
init<std::uint64_t, std::uint64_t, std::uint64_t,
std::uint32_t, std::int32_t>(
(arg("x"), arg("y"), arg("z"),
arg("tile_size") = 4096,
arg("buffer_size") = 128)
))
.add_property("x",
static_cast<xyz_get>(&merc_tile::x),
static_cast<xyz_set>(&merc_tile::x))
.add_property("y",
static_cast<xyz_get>(&merc_tile::y),
static_cast<xyz_set>(&merc_tile::y))
.add_property("z",
static_cast<xyz_get>(&merc_tile::z),
static_cast<xyz_set>(&merc_tile::z))
;
def("merge_compressed_buffer", &merge_compressed_buffer,
"Merge MVT buffer with given tile. The buffer can be compressed.");
def("add_image_layer", &add_image_layer,
"Creates new layer with bitmap image content.");
export_mvt_create();
export_mvt_info();
export_mvt_render();
export_mvt_preview();
}