Skip to content

Commit 8482ce8

Browse files
committed
pybind11 [WIP]
1 parent de118c7 commit 8482ce8

11 files changed

+1076
-722
lines changed

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def check_output(args):
7777
"src/mapnik_image.cpp",
7878
"src/mapnik_projection.cpp",
7979
"src/mapnik_proj_transform.cpp",
80-
80+
"src/mapnik_rule.cpp",
81+
"src/mapnik_symbolizer.cpp",
82+
"src/mapnik_polygon_symbolizer.cpp",
83+
"src/mapnik_line_symbolizer.cpp",
8184
],
8285
extra_compile_args=extra_comp_args,
8386
extra_link_args=linkflags,

src/mapnik_line_symbolizer.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*****************************************************************************
2+
*
3+
* This file is part of Mapnik (c++ mapping toolkit)
4+
*
5+
* Copyright (C) 2024 Artem Pavlenko
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
*****************************************************************************/
22+
23+
// mapnik
24+
#include <mapnik/config.hpp>
25+
#include <mapnik/symbolizer.hpp>
26+
#include <mapnik/symbolizer_hash.hpp>
27+
#include <mapnik/symbolizer_utils.hpp>
28+
#include <mapnik/symbolizer_keys.hpp>
29+
30+
#include "mapnik_symbolizer.hpp"
31+
//pybind11
32+
#include <pybind11/pybind11.h>
33+
#include <pybind11/operators.h>
34+
#include <pybind11/stl.h>
35+
#include <pybind11/stl_bind.h>
36+
37+
#define PYBIND11_DETAILED_ERROR_MESSAGES
38+
39+
namespace py = pybind11;
40+
41+
namespace {
42+
43+
std::string get_stroke_dasharray(mapnik::symbolizer_base & sym)
44+
{
45+
auto dash = mapnik::get<mapnik::dash_array>(sym, mapnik::keys::stroke_dasharray);
46+
47+
std::ostringstream os;
48+
for (std::size_t i = 0; i < dash.size(); ++i)
49+
{
50+
os << dash[i].first << "," << dash[i].second;
51+
if (i + 1 < dash.size())
52+
os << ",";
53+
}
54+
return os.str();
55+
}
56+
57+
void set_stroke_dasharray(mapnik::symbolizer_base & sym, std::string str)
58+
{
59+
mapnik::dash_array dash;
60+
if (mapnik::util::parse_dasharray(str, dash))
61+
{
62+
mapnik::put(sym, mapnik::keys::stroke_dasharray, dash);
63+
}
64+
else
65+
{
66+
throw std::runtime_error("Can't parse dasharray");
67+
}
68+
}
69+
70+
}
71+
72+
73+
void export_line_symbolizer(py::module const& m)
74+
{
75+
using namespace python_mapnik;
76+
using mapnik::line_symbolizer;
77+
78+
py::enum_<mapnik::line_rasterizer_enum>(m, "line_rasterizer")
79+
.value("FULL",mapnik::line_rasterizer_enum::RASTERIZER_FULL)
80+
.value("FAST",mapnik::line_rasterizer_enum::RASTERIZER_FAST)
81+
;
82+
83+
py::enum_<mapnik::line_cap_enum>(m, "stroke_linecap")
84+
.value("BUTT_CAP",mapnik::line_cap_enum::BUTT_CAP)
85+
.value("SQUARE_CAP",mapnik::line_cap_enum::SQUARE_CAP)
86+
.value("ROUND_CAP",mapnik::line_cap_enum::ROUND_CAP)
87+
;
88+
89+
py::enum_<mapnik::line_join_enum>(m, "stroke_linejoin")
90+
.value("MITER_JOIN",mapnik::line_join_enum::MITER_JOIN)
91+
.value("MITER_REVERT_JOIN",mapnik::line_join_enum::MITER_REVERT_JOIN)
92+
.value("ROUND_JOIN",mapnik::line_join_enum::ROUND_JOIN)
93+
.value("BEVEL_JOIN",mapnik::line_join_enum::BEVEL_JOIN)
94+
;
95+
96+
py::class_<line_symbolizer, symbolizer_base>(m, "LineSymbolizer")
97+
.def(py::init<>(), "Default LineSymbolizer - 1px solid black")
98+
.def("__hash__",hash_impl_2<line_symbolizer>)
99+
.def_property("stroke",
100+
&get_property<line_symbolizer, mapnik::keys::stroke>,
101+
&set_color_property<line_symbolizer, mapnik::keys::stroke>,
102+
"Stroke color")
103+
.def_property("stroke_width",
104+
&get_property<line_symbolizer,mapnik::keys::stroke_width>,
105+
&set_double_property<line_symbolizer, mapnik::keys::stroke_width>,
106+
"Stroke width")
107+
.def_property("stroke_opacity",
108+
&get_property<line_symbolizer,mapnik::keys::stroke_opacity>,
109+
&set_double_property<line_symbolizer, mapnik::keys::stroke_opacity>,
110+
"Stroke opacity")
111+
.def_property("stroke_gamma",
112+
&get_property<line_symbolizer,mapnik::keys::stroke_gamma>,
113+
&set_double_property<line_symbolizer,mapnik::keys::stroke_gamma>,
114+
"Stroke gamma")
115+
.def_property("stroke_gamma_method",
116+
&get<mapnik::gamma_method_enum, mapnik::keys::stroke_gamma_method>,
117+
&set_enum_property<line_symbolizer, mapnik::gamma_method_enum, mapnik::keys::stroke_gamma_method>,
118+
"Stroke gamma method")
119+
.def_property("line_rasterizer",
120+
&get<mapnik::line_rasterizer_enum, mapnik::keys::line_rasterizer>,
121+
&set_enum_property<line_symbolizer, mapnik::line_rasterizer_enum, mapnik::keys::line_rasterizer>,
122+
"Line rasterizer")
123+
.def_property("stroke_linecap",
124+
&get<mapnik::line_cap_enum, mapnik::keys::stroke_linecap>,
125+
&set_enum_property<line_symbolizer, mapnik::line_cap_enum, mapnik::keys::stroke_linecap>,
126+
"Stroke linecap")
127+
.def_property("stroke_linejoin",
128+
&get<mapnik::line_join_enum, mapnik::keys::stroke_linejoin>,
129+
&set_enum_property<line_symbolizer, mapnik::line_join_enum, mapnik::keys::stroke_linejoin>,
130+
"Stroke linejoin")
131+
.def_property("stroke_dasharray",
132+
&get_stroke_dasharray,
133+
&set_stroke_dasharray,
134+
"Stroke dasharray")
135+
.def_property("stroke_dashoffset",
136+
&get_property<line_symbolizer, mapnik::keys::stroke_dashoffset>,
137+
&set_double_property<line_symbolizer,mapnik::keys::stroke_dashoffset>,
138+
"Stroke dashoffset")
139+
.def_property("stroke_miterlimit",
140+
&get_property<line_symbolizer, mapnik::keys::stroke_miterlimit>,
141+
&set_double_property<line_symbolizer, mapnik::keys::stroke_miterlimit>,
142+
"Stroke miterlimit")
143+
144+
;
145+
}

src/mapnik_polygon_symbolizer.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*****************************************************************************
2+
*
3+
* This file is part of Mapnik (c++ mapping toolkit)
4+
*
5+
* Copyright (C) 2024 Artem Pavlenko
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
*****************************************************************************/
22+
23+
// mapnik
24+
#include <mapnik/config.hpp>
25+
#include <mapnik/symbolizer.hpp>
26+
#include <mapnik/symbolizer_hash.hpp>
27+
#include <mapnik/symbolizer_utils.hpp>
28+
#include <mapnik/symbolizer_keys.hpp>
29+
30+
#include "mapnik_symbolizer.hpp"
31+
//pybind11
32+
#include <pybind11/pybind11.h>
33+
#include <pybind11/operators.h>
34+
#include <pybind11/stl.h>
35+
#include <pybind11/stl_bind.h>
36+
37+
#define PYBIND11_DETAILED_ERROR_MESSAGES
38+
39+
namespace py = pybind11;
40+
41+
42+
43+
void export_polygon_symbolizer(py::module const& m)
44+
{
45+
using namespace python_mapnik;
46+
using mapnik::polygon_symbolizer;
47+
48+
py::class_<polygon_symbolizer, symbolizer_base>(m, "PolygonSymbolizer")
49+
.def(py::init<>(), "Default ctor")
50+
.def("__hash__", hash_impl_2<polygon_symbolizer>)
51+
52+
.def_property("fill",
53+
&get_property<polygon_symbolizer, mapnik::keys::fill>,
54+
&set_color_property<polygon_symbolizer, mapnik::keys::fill>,
55+
"Fill - mapnik.Color, CSS color string or a valid mapnik.Expression")
56+
57+
.def_property("fill_opacity",
58+
&get_property<polygon_symbolizer, mapnik::keys::fill_opacity>,
59+
&set_double_property<polygon_symbolizer, mapnik::keys::fill_opacity>,
60+
"Fill opacity - [0-1] or a valid mapnik.Expression")
61+
62+
.def_property("gamma",
63+
&get_property<polygon_symbolizer, mapnik::keys::gamma>,
64+
&set_double_property<polygon_symbolizer, mapnik::keys::gamma>,
65+
"Fill gamma")
66+
67+
.def_property("gamma_method",
68+
//&get_property<polygon_symbolizer, mapnik::keys::gamma_method>,
69+
&get<mapnik::gamma_method_enum, mapnik::keys::gamma_method>,
70+
&set_enum_property<polygon_symbolizer,mapnik::gamma_method_enum, mapnik::keys::gamma_method>,
71+
"Fill gamma method")
72+
;
73+
74+
}

src/mapnik_python.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ void export_map(py::module const&);
132132
void export_projection(py::module&);
133133
void export_proj_transform(py::module const&);
134134
void export_query(py::module const& m);
135-
135+
void export_rule(py::module const& m);
136+
void export_symbolizer(py::module const& m);
137+
void export_polygon_symbolizer(py::module const& m);
138+
void export_line_symbolizer(py::module const& m);
136139

137140
using mapnik::load_map;
138141
using mapnik::load_map_string;
@@ -159,6 +162,10 @@ PYBIND11_MODULE(_mapnik, m) {
159162
export_projection(m);
160163
export_proj_transform(m);
161164
export_query(m);
165+
export_rule(m);
166+
export_symbolizer(m);
167+
export_polygon_symbolizer(m);
168+
export_line_symbolizer(m);
162169

163170
m.def("mapnik_version", &mapnik_version,"Get the Mapnik version number");
164171
m.def("mapnik_version_string", &mapnik_version_string,"Get the Mapnik version string");

src/mapnik_rule.cpp

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* This file is part of Mapnik (c++ mapping toolkit)
44
*
5-
* Copyright (C) 2015 Artem Pavlenko, Jean-Francois Doyon
5+
* Copyright (C) 2024 Artem Pavlenko
66
*
77
* This library is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public
@@ -21,19 +21,18 @@
2121
*****************************************************************************/
2222

2323
#include <mapnik/config.hpp>
24-
25-
26-
#pragma GCC diagnostic push
27-
#include <mapnik/warning_ignore.hpp>
28-
#include <boost/python.hpp>
29-
#include <boost/python/implicit.hpp>
30-
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
31-
#pragma GCC diagnostic pop
32-
3324
// mapnik
3425
#include <mapnik/rule.hpp>
3526
#include <mapnik/expression.hpp>
3627
#include <mapnik/expression_string.hpp>
28+
//#include "python_variant.hpp"
29+
//pybind11
30+
#include <pybind11/pybind11.h>
31+
#include <pybind11/operators.h>
32+
#include <pybind11/stl.h>
33+
#include <pybind11/stl_bind.h>
34+
35+
namespace py = pybind11;
3736

3837
using mapnik::rule;
3938
using mapnik::expr_node;
@@ -52,45 +51,36 @@ using mapnik::group_symbolizer;
5251
using mapnik::symbolizer;
5352
using mapnik::to_expression_string;
5453

55-
void export_rule()
54+
PYBIND11_MAKE_OPAQUE(std::vector<symbolizer>);
55+
56+
void export_rule(py::module const& m)
5657
{
57-
using namespace boost::python;
58-
implicitly_convertible<point_symbolizer,symbolizer>();
59-
implicitly_convertible<line_symbolizer,symbolizer>();
60-
implicitly_convertible<line_pattern_symbolizer,symbolizer>();
61-
implicitly_convertible<polygon_symbolizer,symbolizer>();
62-
implicitly_convertible<building_symbolizer,symbolizer>();
63-
implicitly_convertible<polygon_pattern_symbolizer,symbolizer>();
64-
implicitly_convertible<raster_symbolizer,symbolizer>();
65-
implicitly_convertible<shield_symbolizer,symbolizer>();
66-
implicitly_convertible<text_symbolizer,symbolizer>();
67-
implicitly_convertible<markers_symbolizer,symbolizer>();
68-
implicitly_convertible<group_symbolizer,symbolizer>();
58+
py::bind_vector<std::vector<symbolizer>>(m, "Symbolizers", py::module_local());
6959

70-
class_<rule::symbolizers>("Symbolizers",init<>("TODO"))
71-
.def(vector_indexing_suite<rule::symbolizers>())
72-
;
60+
py::class_<rule>(m, "Rule")
61+
.def(py::init<>(), "default constructor")
62+
.def(py::init<std::string, double, double>(),
63+
py::arg("name"),
64+
py::arg("min_scale_denominator")=0,
65+
py::arg("max_scale_denominator")=std::numeric_limits<double>::infinity())
7366

74-
class_<rule>("Rule",init<>("default constructor"))
75-
.def(init<std::string const&,
76-
boost::python::optional<double,double> >())
77-
.add_property("name",make_function
78-
(&rule::get_name,
79-
return_value_policy<copy_const_reference>()),
67+
.def_property("name",
68+
&rule::get_name,
8069
&rule::set_name)
81-
.add_property("filter",make_function
82-
(&rule::get_filter,return_value_policy<copy_const_reference>()),
70+
71+
.def_property("filter",
72+
&rule::get_filter,
8373
&rule::set_filter)
84-
.add_property("min_scale",&rule::get_min_scale,&rule::set_min_scale)
85-
.add_property("max_scale",&rule::get_max_scale,&rule::set_max_scale)
86-
.def("set_else",&rule::set_else)
87-
.def("has_else",&rule::has_else_filter)
88-
.def("set_also",&rule::set_also)
89-
.def("has_also",&rule::has_also_filter)
90-
.def("active",&rule::active)
91-
.add_property("symbols",make_function
92-
(&rule::get_symbolizers,return_value_policy<reference_existing_object>()))
93-
.add_property("copy_symbols",make_function
94-
(&rule::get_symbolizers,return_value_policy<copy_const_reference>()))
74+
75+
.def_property("min_scale", &rule::get_min_scale, &rule::set_min_scale)
76+
.def_property("max_scale", &rule::get_max_scale, &rule::set_max_scale)
77+
.def("set_else", &rule::set_else)
78+
.def("has_else", &rule::has_else_filter)
79+
.def("set_also", &rule::set_also)
80+
.def("has_also", &rule::has_also_filter)
81+
.def("active", &rule::active)
82+
.def_property_readonly("symbolizers", &rule::get_symbolizers)//,return_value_policy<reference_existing_object>()))
83+
//.def_property("copy_symbols",make_function
84+
// (&rule::get_symbolizers,return_value_policy<copy_const_reference>()))
9585
;
9686
}

src/mapnik_style.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* This file is part of Mapnik (c++ mapping toolkit)
44
*
5-
* Copyright (C) 2015 Artem Pavlenko, Jean-Francois Doyon
5+
* Copyright (C) 2024 Artem Pavlenko
66
*
77
* This library is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public
@@ -22,17 +22,9 @@
2222

2323
#include <mapnik/config.hpp>
2424

25-
26-
#pragma GCC diagnostic push
27-
#include <mapnik/warning_ignore.hpp>
28-
#include <boost/python.hpp>
29-
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
30-
#pragma GCC diagnostic pop
31-
3225
// mapnik
3326
#include <mapnik/value/error.hpp>
3427
#include <mapnik/rule.hpp>
35-
#include "mapnik_enumeration.hpp"
3628
#include <mapnik/feature_type_style.hpp>
3729
#include <mapnik/image_filter_types.hpp> // generate_image_filters
3830

0 commit comments

Comments
 (0)