-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathpython_to_value.hpp
More file actions
70 lines (64 loc) · 2.33 KB
/
python_to_value.hpp
File metadata and controls
70 lines (64 loc) · 2.33 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
/*****************************************************************************
*
* 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_PYTHON_BINDING_PYTHON_TO_VALUE
#define MAPNIK_PYTHON_BINDING_PYTHON_TO_VALUE
// mapnik
#include <mapnik/value.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/attribute.hpp>
//pybind11
#include <pybind11/pybind11.h>
namespace py = pybind11;
namespace mapnik {
static mapnik::attributes dict2attr(py::dict const& d)
{
mapnik::attributes vars;
mapnik::transcoder tr_("utf8");
for (auto item : d)
{
std::string key = std::string(py::str(item.first));
py::handle handle = item.second;
if (py::isinstance<py::str>(handle))
{
vars[key] = tr_.transcode(handle.cast<std::string>().c_str());
}
else if (py::isinstance<py::bool_>(handle))
{
vars[key] = handle.cast<mapnik::value_bool>();
}
else if (py::isinstance<py::float_>(handle))
{
vars[key] = handle.cast<mapnik::value_double>();
}
else if (py::isinstance<py::int_>(handle))
{
vars[key] = handle.cast<mapnik::value_integer>();
}
else
{
vars[key] = tr_.transcode(py::str(handle).cast<std::string>().c_str());
}
}
return vars;
}
}
#endif // MAPNIK_PYTHON_BINDING_PYTHON_TO_VALUE