|
27 | 27 | #include <mapnik/unicode.hpp> |
28 | 28 | #include <mapnik/value.hpp> |
29 | 29 | #include <mapnik/value/types.hpp> |
30 | | -// stl |
31 | | -#include <iterator> |
| 30 | +#include "mapnik_value_converter.hpp" |
32 | 31 | //pybind11 |
33 | 32 | #include <pybind11/pybind11.h> |
34 | | - |
| 33 | +#include <pybind11/stl.h> |
| 34 | +#include <pybind11/stl_bind.h> |
35 | 35 | namespace py = pybind11; |
36 | 36 |
|
37 | 37 | using mapnik::parameter; |
38 | 38 | using mapnik::parameters; |
39 | 39 |
|
40 | | -struct parameter_pickle_suite : boost::python::pickle_suite |
41 | | -{ |
42 | | - static boost::python::tuple |
43 | | - getinitargs(const parameter& p) |
44 | | - { |
45 | | - using namespace boost::python; |
46 | | - return boost::python::make_tuple(p.first,p.second); |
47 | | - } |
48 | | -}; |
49 | | - |
50 | | -struct parameters_pickle_suite : boost::python::pickle_suite |
51 | | -{ |
52 | | - static boost::python::tuple |
53 | | - getstate(const parameters& p) |
54 | | - { |
55 | | - using namespace boost::python; |
56 | | - dict d; |
57 | | - parameters::const_iterator pos=p.begin(); |
58 | | - while(pos!=p.end()) |
59 | | - { |
60 | | - d[pos->first] = pos->second; |
61 | | - ++pos; |
62 | | - } |
63 | | - return boost::python::make_tuple(d); |
64 | | - } |
65 | | - |
66 | | - static void setstate(parameters& p, boost::python::tuple state) |
67 | | - { |
68 | | - using namespace boost::python; |
69 | | - if (len(state) != 1) |
70 | | - { |
71 | | - PyErr_SetObject(PyExc_ValueError, |
72 | | - ("expected 1-item tuple in call to __setstate__; got %s" |
73 | | - % state).ptr() |
74 | | - ); |
75 | | - throw_error_already_set(); |
76 | | - } |
77 | | - |
78 | | - dict d = extract<dict>(state[0]); |
79 | | - boost::python::list keys = d.keys(); |
80 | | - for (int i=0; i<len(keys); ++i) |
81 | | - { |
82 | | - std::string key = extract<std::string>(keys[i]); |
83 | | - object obj = d[key]; |
84 | | - extract<std::string> ex0(obj); |
85 | | - extract<mapnik::value_integer> ex1(obj); |
86 | | - extract<double> ex2(obj); |
87 | | - extract<mapnik::value_unicode_string> ex3(obj); |
88 | | - |
89 | | - // TODO - this is never hit - we need proper python string -> std::string to get invoked here |
90 | | - if (ex0.check()) |
91 | | - { |
92 | | - p[key] = ex0(); |
93 | | - } |
94 | | - else if (ex1.check()) |
95 | | - { |
96 | | - p[key] = ex1(); |
97 | | - } |
98 | | - else if (ex2.check()) |
99 | | - { |
100 | | - p[key] = ex2(); |
101 | | - } |
102 | | - else if (ex3.check()) |
103 | | - { |
104 | | - std::string buffer; |
105 | | - mapnik::to_utf8(ex3(),buffer); |
106 | | - p[key] = buffer; |
107 | | - } |
108 | | - else |
109 | | - { |
110 | | - MAPNIK_LOG_DEBUG(bindings) << "parameters_pickle_suite: Could not unpickle key=" << key; |
111 | | - } |
112 | | - } |
113 | | - } |
114 | | -}; |
115 | | - |
116 | | - |
117 | | -mapnik::value_holder get_params_by_key1(mapnik::parameters const& p, std::string const& key) |
118 | | -{ |
119 | | - parameters::const_iterator pos = p.find(key); |
120 | | - if (pos != p.end()) |
121 | | - { |
122 | | - // will be auto-converted to proper python type by `mapnik_params_to_python` |
123 | | - return pos->second; |
124 | | - } |
125 | | - return mapnik::value_null(); |
126 | | -} |
127 | | - |
128 | | -mapnik::value_holder get_params_by_key2(mapnik::parameters const& p, std::string const& key) |
129 | | -{ |
130 | | - parameters::const_iterator pos = p.find(key); |
131 | | - if (pos == p.end()) |
132 | | - { |
133 | | - PyErr_SetString(PyExc_KeyError, key.c_str()); |
134 | | - boost::python::throw_error_already_set(); |
135 | | - } |
136 | | - // will be auto-converted to proper python type by `mapnik_params_to_python` |
137 | | - return pos->second; |
138 | | -} |
139 | | - |
140 | | -mapnik::parameter get_params_by_index(mapnik::parameters const& p, int index) |
141 | | -{ |
142 | | - if (index < 0 || static_cast<unsigned>(index) > p.size()) |
143 | | - { |
144 | | - PyErr_SetString(PyExc_IndexError, "Index is out of range"); |
145 | | - throw boost::python::error_already_set(); |
146 | | - } |
147 | | - |
148 | | - parameters::const_iterator itr = p.begin(); |
149 | | - std::advance(itr, index); |
150 | | - if (itr != p.end()) |
151 | | - { |
152 | | - return *itr; |
153 | | - } |
154 | | - PyErr_SetString(PyExc_IndexError, "Index is out of range"); |
155 | | - throw boost::python::error_already_set(); |
156 | | -} |
157 | | - |
158 | | -std::size_t get_params_size(mapnik::parameters const& p) |
159 | | -{ |
160 | | - return p.size(); |
161 | | -} |
162 | | - |
163 | | -void add_parameter(mapnik::parameters & p, mapnik::parameter const& param) |
164 | | -{ |
165 | | - p[param.first] = param.second; |
166 | | -} |
167 | | - |
168 | | -mapnik::value_holder get_param(mapnik::parameter const& p, int index) |
169 | | -{ |
170 | | - if (index == 0) |
171 | | - { |
172 | | - return p.first; |
173 | | - } |
174 | | - else if (index == 1) |
175 | | - { |
176 | | - return p.second; |
177 | | - } |
178 | | - else |
179 | | - { |
180 | | - PyErr_SetString(PyExc_IndexError, "Index is out of range"); |
181 | | - throw boost::python::error_already_set(); |
182 | | - } |
183 | | -} |
184 | | - |
185 | | -std::shared_ptr<mapnik::parameter> create_parameter(mapnik::value_unicode_string const& key, mapnik::value_holder const& value) |
186 | | -{ |
187 | | - std::string key_utf8; |
188 | | - mapnik::to_utf8(key, key_utf8); |
189 | | - return std::make_shared<mapnik::parameter>(key_utf8,value); |
190 | | -} |
191 | | - |
192 | | -bool contains(mapnik::parameters const& p, std::string const& key) |
193 | | -{ |
194 | | - parameters::const_iterator pos = p.find(key); |
195 | | - return pos != p.end(); |
196 | | -} |
197 | | - |
198 | | -// needed for Python_Unicode to std::string (utf8) conversion |
199 | | - |
200 | | -std::shared_ptr<mapnik::parameter> create_parameter_from_string(mapnik::value_unicode_string const& key, mapnik::value_unicode_string const& ustr) |
201 | | -{ |
202 | | - std::string key_utf8; |
203 | | - std::string ustr_utf8; |
204 | | - mapnik::to_utf8(key, key_utf8); |
205 | | - mapnik::to_utf8(ustr,ustr_utf8); |
206 | | - return std::make_shared<mapnik::parameter>(key_utf8, ustr_utf8); |
207 | | -} |
208 | 40 |
|
209 | | -void export_parameters() |
| 41 | +void export_parameters(py::module const& m) |
210 | 42 | { |
211 | | - using namespace boost::python; |
212 | | - implicitly_convertible<std::string,mapnik::value_holder>(); |
213 | | - implicitly_convertible<mapnik::value_null,mapnik::value_holder>(); |
214 | | - implicitly_convertible<mapnik::value_integer,mapnik::value_holder>(); |
215 | | - implicitly_convertible<mapnik::value_double,mapnik::value_holder>(); |
216 | | - |
217 | | - class_<parameter,std::shared_ptr<parameter> >("Parameter",no_init) |
218 | | - .def("__init__", make_constructor(create_parameter), |
219 | | - "Create a mapnik.Parameter from a pair of values, the first being a string\n" |
220 | | - "and the second being either a string, and integer, or a float") |
221 | | - .def("__init__", make_constructor(create_parameter_from_string), |
222 | | - "Create a mapnik.Parameter from a pair of values, the first being a string\n" |
223 | | - "and the second being either a string, and integer, or a float") |
224 | | - |
225 | | - .def_pickle(parameter_pickle_suite()) |
226 | | - .def("__getitem__",get_param) |
227 | | - ; |
228 | | - |
229 | | - class_<parameters>("Parameters",init<>()) |
230 | | - .def_pickle(parameters_pickle_suite()) |
231 | | - .def("get",get_params_by_key1) |
232 | | - .def("__getitem__",get_params_by_key2) |
233 | | - .def("__getitem__",get_params_by_index) |
234 | | - .def("__len__",get_params_size) |
235 | | - .def("__contains__",contains) |
236 | | - .def("append",add_parameter) |
237 | | - .def("iteritems",iterator<parameters>()) |
238 | | - ; |
| 43 | + py::bind_map<mapnik::parameters>(m, "Parameters", py::module_local()); |
239 | 44 | } |
0 commit comments