forked from mapnik/python-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapnik_vector_tile_render.cpp
More file actions
194 lines (175 loc) · 7.07 KB
/
Copy pathmapnik_vector_tile_render.cpp
File metadata and controls
194 lines (175 loc) · 7.07 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
/*****************************************************************************
*
* 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 <mapnik/layer.hpp>
#include <mapnik/map.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/image_any.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/scale_denominator.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_tile.hpp>
#include <mapbox/mapnik-vector-tile/vector_tile_projection.hpp>
#define MAPNIK_VECTOR_TILE_LIBRARY
#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_merc_tile.hpp>
#include <boost/python.hpp>
#include "python_to_value.hpp"
bool set_mvt_datasource(mapnik::layer & lyr,
mapnik::box2d<double> const& buffered_extent,
std::string const& map_srs,
mapnik::vector_tile_impl::merc_tile const& tile)
{
protozero::pbf_reader layer_msg;
if (tile.layer_reader(lyr.name(), layer_msg))
{
lyr.set_srs(map_srs);
using ds_type = mapnik::vector_tile_impl::tile_datasource_pbf;
using ds_holder_type = std::shared_ptr<ds_type>;
ds_holder_type ds = std::make_shared<ds_type>(
layer_msg, tile.x(), tile.y(), tile.z());
ds->set_envelope(buffered_extent);
lyr.set_datasource(ds);
return true;
}
return false;
}
void prepare_sublayers(mapnik::layer & lyr,
mapnik::box2d<double> const& buffered_extent,
double scale_denom,
std::string const& map_srs,
mapnik::vector_tile_impl::merc_tile const& tile)
{
for (auto & sublyr : lyr.layers())
{
bool sublayer_used = sublyr.visible(scale_denom) &&
set_mvt_datasource(sublyr, buffered_extent, map_srs, tile);
sublyr.set_active(sublayer_used);
if (sublayer_used)
{
prepare_sublayers(sublyr, buffered_extent,
scale_denom, map_srs, tile);
}
}
}
template <typename Renderer>
void process_layers(Renderer & ren,
mapnik::request const& m_req,
mapnik::projection const& map_proj,
std::deque<mapnik::layer> const& layers,
double scale_denom,
std::string const& map_srs,
mapnik::vector_tile_impl::merc_tile const& tile)
{
mapnik::box2d<double> buffered_extent = m_req.get_buffered_extent();
for (auto const& lyr : layers)
{
if (lyr.visible(scale_denom))
{
mapnik::layer lyr_copy(lyr);
if (set_mvt_datasource(lyr_copy, buffered_extent, map_srs, tile))
{
prepare_sublayers(lyr_copy, buffered_extent,
scale_denom, map_srs, tile);
protozero::pbf_reader layer_msg;
std::set<std::string> names;
ren.apply_to_layer(lyr_copy,
ren,
map_proj,
m_req.scale(),
scale_denom,
m_req.width(),
m_req.height(),
m_req.extent(),
m_req.buffer_size(),
names);
}
}
}
}
void render_mvt_merc(mapnik::vector_tile_impl::merc_tile const& mvt,
mapnik::Map const& map,
mapnik::image_any& image,
boost::python::dict const& vars_dict,
double scale_factor,
double scale_denominator,
boost::optional<std::int32_t> buffer_size,
boost::optional<std::uint64_t> const& x,
boost::optional<std::uint64_t> const& y,
boost::optional<std::uint64_t> const& z)
{
mapnik::projection map_proj(map.srs(), true);
mapnik::box2d<double> map_extent = mvt.extent();
if (x || y || z)
{
map_extent = mapnik::vector_tile_impl::merc_extent(*x, *y, *z);
}
mapnik::request m_req(image.width(), image.height(), map_extent);
m_req.set_buffer_size(buffer_size ? *buffer_size : map.buffer_size());
if (scale_factor <= 0.0)
{
scale_factor = 1.0;
}
double scale_denom = scale_denominator;
if (scale_denom <= 0.0)
{
scale_denom = mapnik::scale_denominator(m_req.scale(), map_proj.is_geographic());
}
scale_denom *= scale_factor;
std::deque<mapnik::layer> const& layers = map.layers();
mapnik::attributes vars = mapnik::dict2attr(vars_dict);
if (image.is<mapnik::image_rgba8>())
{
mapnik::image_rgba8 & image_data = mapnik::util::get<mapnik::image_rgba8>(image);
mapnik::agg_renderer<mapnik::image_rgba8> ren(map,
m_req,
vars,
image_data,
scale_factor);
ren.start_map_processing(map);
process_layers(ren, m_req, map_proj, layers, scale_denom, map.srs(), mvt);
ren.end_map_processing(map);
}
else
{
throw std::runtime_error("This image type is not currently supported for rendering.");
}
}
void export_mvt_render()
{
using namespace boost::python;
def("render_mvt_merc", &render_mvt_merc,
(arg("tile"),
arg("map"),
arg("image"),
arg("variables") = boost::python::dict(),
arg("scale_factor") = 1.0,
// Override auto-calculated scale denominator
arg("scale_denom") = 0.0,
// Override buffer_size from Map
arg("buffer_size") = boost::optional<std::int32_t>(),
// Override MVT coordinates
arg("x") = boost::optional<std::uint64_t>(),
arg("y") = boost::optional<std::uint64_t>(),
arg("z") = boost::optional<std::uint64_t>()),
"Render vector tile in Mercator to a surface/image");
}