-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmapnik_symbolizer.hpp
More file actions
298 lines (259 loc) · 8.46 KB
/
mapnik_symbolizer.hpp
File metadata and controls
298 lines (259 loc) · 8.46 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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2024 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#ifndef MAPNIK_SYMBOLIZER_INCLUDED
#define MAPNIK_SYMBOLIZER_INCLUDED
#include <mapnik/color.hpp>
#include <mapnik/symbolizer.hpp>
#include <mapnik/symbolizer_hash.hpp>
#include <mapnik/symbolizer_utils.hpp>
#include <mapnik/symbolizer_keys.hpp>
#include <mapnik/symbolizer_enumerations.hpp>
#include <mapnik/path_expression.hpp>
#include <mapnik/parse_path.hpp>
#include <mapnik/raster_colorizer.hpp>
#include <mapnik/value/error.hpp>
//pybind11
#include <pybind11/pybind11.h>
PYBIND11_MAKE_OPAQUE(mapnik::symbolizer);
PYBIND11_MAKE_OPAQUE(mapnik::path_expression);
namespace py = pybind11;
namespace python_mapnik {
using mapnik::symbolizer;
using mapnik::symbolizer_base;
using mapnik::parse_path;
using mapnik::path_processor;
template <typename TargetType>
struct enum_converter
{
static auto apply(mapnik::enumeration_wrapper const& wrapper, mapnik::keys key) -> py::object
{
return py::cast(TargetType(wrapper.value));
}
};
template <>
struct enum_converter<void>
{
static auto apply(mapnik::enumeration_wrapper const& wrapper, mapnik::keys key) -> py::object
{
auto meta = mapnik::get_meta(key);
auto const& convert_fun_ptr(std::get<1>(meta));
if (convert_fun_ptr)
{
return py::cast(convert_fun_ptr(wrapper));
}
throw pybind11::key_error("Invalid property name");
}
};
template <typename TargetType = void>
struct extract_python_object
{
using result_type = py::object;
mapnik::keys key_;
extract_python_object(mapnik::keys key)
: key_(key) {}
auto operator() (mapnik::value_bool val) const -> result_type
{
return py::bool_(val);
}
auto operator() (mapnik::value_double val) const -> result_type
{
return py::float_(val);
}
auto operator() (mapnik::value_integer val) const -> result_type
{
return py::int_(val);
}
auto operator() (mapnik::color const& col) const -> result_type
{
return py::cast(col);
}
auto operator() (mapnik::expression_ptr const& expr) const -> result_type
{
return py::cast(expr);
}
auto operator() (mapnik::path_expression_ptr const& expr) const ->result_type
{
return py::cast(expr);
}
auto operator() (mapnik::enumeration_wrapper const& wrapper) const ->result_type
{
return enum_converter<TargetType>::apply(wrapper, key_);
}
auto operator() (mapnik::transform_list_ptr const& expr) const ->result_type
{
if (expr) return py::cast(mapnik::transform_processor_type::to_string(*expr));
return py::none();
}
auto operator() (mapnik::raster_colorizer_ptr const& colorizer) const ->result_type
{
return py::cast(colorizer);
}
template <typename T>
auto operator() (T const& val) const -> result_type
{
std::cerr << "Can't convert to Python object [" << typeid(val).name() << "]" << std::endl;
return py::none();
}
};
template <typename Symbolizer, mapnik::keys Key, typename Enum = int>
py::object get_property(Symbolizer const& sym)
{
using const_iterator = symbolizer_base::cont_type::const_iterator;
const_iterator itr = sym.properties.find(Key);
if (itr != sym.properties.end())
{
return mapnik::util::apply_visitor(extract_python_object<Enum>(Key), itr->second);
}
//throw pybind11::key_error("Invalid property name");
return py::none();
}
template <typename Symbolizer, auto Key>
void set_color_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<mapnik::color>(obj))
{
mapnik::put(sym, Key, obj.cast<mapnik::color>());
}
else if (py::isinstance<mapnik::expr_node>(obj))
{
auto expr = obj.cast<mapnik::expression_ptr>();
mapnik::put(sym, Key, expr);
}
else if (py::isinstance<py::str>(obj))
{
mapnik::put(sym, Key, mapnik::color(obj.cast<std::string>()));
}
else throw mapnik::value_error("Error assigning color property");
}
template <typename Symbolizer, auto Key>
void set_boolean_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<py::bool_>(obj))
{
mapnik::put(sym, Key, obj.cast<mapnik::value_bool>());
}
else if (py::isinstance<mapnik::expr_node>(obj))
{
auto expr = obj.cast<mapnik::expression_ptr>();
mapnik::put(sym, Key, expr);
}
else throw mapnik::value_error("Error assigning boolean property");
}
template <typename Symbolizer, auto Key>
void set_integer_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<py::int_>(obj))
{
mapnik::put(sym, Key, obj.cast<mapnik::value_integer>());
}
else if (py::isinstance<mapnik::expr_node>(obj))
{
auto expr = obj.cast<mapnik::expression_ptr>();
mapnik::put(sym, Key, expr);
}
else throw mapnik::value_error("Error assigning int property");
}
template <typename Symbolizer, auto Key>
void set_double_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<py::int_>(obj) || py::isinstance<py::float_>(obj))
{
mapnik::put(sym, Key, obj.cast<mapnik::value_double>());
}
else if (py::isinstance<mapnik::expr_node>(obj))
{
auto expr = obj.cast<mapnik::expression_ptr>();
mapnik::put(sym, Key, expr);
}
else throw mapnik::value_error("Error assigning double property");
}
template <typename Symbolizer, typename Enum, auto Key>
void set_enum_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<Enum>(obj))
{
mapnik::put(sym, Key, obj.cast<Enum>());
}
else if (py::isinstance<mapnik::expr_node>(obj))
{
auto expr = obj.cast<mapnik::expression_ptr>();
mapnik::put(sym, Key, expr);
}
else throw mapnik::value_error("Error assigning enum property");
}
template <typename Symbolizer, auto Key>
void set_path_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<py::str>(obj))
{
mapnik::put(sym, Key, parse_path(obj.cast<std::string>()));
}
else if (py::isinstance<mapnik::path_expression>(obj))
{
auto expr = obj.cast<mapnik::path_expression_ptr>();
mapnik::put(sym, Key, expr);
}
else throw mapnik::value_error("Error assigning path property");
}
template <typename Symbolizer, auto Key>
void set_colorizer_property(Symbolizer & sym, py::object const& obj)
{
if (py::isinstance<mapnik::raster_colorizer>(obj))
{
mapnik::put(sym, Key, obj.cast<mapnik::raster_colorizer_ptr>());
}
else throw mapnik::value_error("Error assigning colorizer property");
}
inline std::size_t hash_impl(symbolizer const& sym)
{
return mapnik::util::apply_visitor(mapnik::symbolizer_hash_visitor(), sym);
}
template <typename T>
std::size_t hash_impl_2(T const& sym)
{
return mapnik::symbolizer_hash::value<T>(sym);
}
template <typename Value, auto Key>
auto get(symbolizer_base const& sym) -> Value
{
return mapnik::get<Value>(sym, Key);
}
template <typename Value, auto Key>
void set(symbolizer_base & sym, Value const& val)
{
mapnik::put<Value>(sym, Key, val);
}
template <auto Key>
std::string get_transform(symbolizer_base const& sym)
{
auto expr = mapnik::get<mapnik::transform_type>(sym, Key);
if (expr)
return mapnik::transform_processor_type::to_string(*expr);
return "";
}
template <auto Key>
void set_transform(symbolizer_base & sym, std::string const& str)
{
mapnik::put(sym, Key, mapnik::parse_transform(str));
}
} // namespace python_mapnik
#endif //MAPNIK_SYMBOLIZER_INCLUDED