/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2013 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 * *****************************************************************************/ #include "boost_std_shared_shim.hpp" // FIXME - do we need it? // The functions in this file produce deprecation warnings. // But as shield symbolizer doesn't fully support more than one // placement from python yet these functions are actually the // correct ones. #define NO_DEPRECATION_WARNINGS // boost #include #include #include // mapnik #include #include #include #include #include #include #include #include "mapnik_enumeration.hpp" #include "mapnik_svg.hpp" #include #include #include #include // for known_svg_prefix_ #include #include #include // stl #include using mapnik::symbolizer; using mapnik::point_symbolizer; using mapnik::line_symbolizer; using mapnik::line_pattern_symbolizer; using mapnik::polygon_symbolizer; using mapnik::polygon_pattern_symbolizer; using mapnik::raster_symbolizer; using mapnik::shield_symbolizer; using mapnik::text_symbolizer; using mapnik::building_symbolizer; using mapnik::markers_symbolizer; using mapnik::debug_symbolizer; using mapnik::group_symbolizer; using mapnik::symbolizer_base; using mapnik::color; using mapnik::path_processor_type; using mapnik::path_expression_ptr; using mapnik::guess_type; using mapnik::expression_ptr; using mapnik::parse_path; namespace { using namespace boost::python; void __setitem__(mapnik::symbolizer_base & sym, std::string const& name, mapnik::symbolizer_base::value_type const& val) { put(sym, mapnik::get_key(name), val); } std::shared_ptr numeric_wrapper(const object& arg) { std::shared_ptr result; if (PyBool_Check(arg.ptr())) { mapnik::value_bool val = extract(arg); result.reset(new mapnik::symbolizer_base::value_type(val)); } else if (PyFloat_Check(arg.ptr())) { mapnik::value_double val = extract(arg); result.reset(new mapnik::symbolizer_base::value_type(val)); } else { mapnik::value_integer val = extract(arg); result.reset(new mapnik::symbolizer_base::value_type(val)); } return result; } struct extract_python_object : public boost::static_visitor { using result_type = boost::python::object; template auto operator() (T const& val) const -> result_type { return result_type(val); // wrap into python object } }; boost::python::object __getitem__(mapnik::symbolizer_base const& sym, std::string const& name) { using const_iterator = symbolizer_base::cont_type::const_iterator; mapnik::keys key = mapnik::get_key(name); const_iterator itr = sym.properties.find(key); if (itr != sym.properties.end()) { return boost::apply_visitor(extract_python_object(), itr->second); } //mapnik::property_meta_type const& meta = mapnik::get_meta(key); //return boost::apply_visitor(extract_python_object(), std::get<1>(meta)); return boost::python::object(); } std::string __str__(mapnik::symbolizer const& sym) { return boost::apply_visitor(mapnik::symbolizer_to_json(), sym); } std::string get_symbolizer_type(symbolizer const& sym) { return mapnik::symbolizer_name(sym); // FIXME - do we need this ? } struct symbolizer_hash_visitor : public boost::static_visitor { template std::size_t operator() (T const& sym) const { return mapnik::symbolizer_hash::value(sym); } }; std::size_t hash_impl(symbolizer const& sym) { return boost::apply_visitor(symbolizer_hash_visitor(), sym); } template std::size_t hash_impl_2(T const& sym) { return mapnik::symbolizer_hash::value(sym); } struct extract_underlying_type_visitor : boost::static_visitor { template boost::python::object operator() (T const& sym) const { return boost::python::object(sym); } }; boost::python::object extract_underlying_type(symbolizer const& sym) { return boost::apply_visitor(extract_underlying_type_visitor(), sym); } } void export_symbolizer() { using namespace boost::python; //implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); implicitly_convertible, mapnik::symbolizer_base::value_type>(); enum_("keys") .value("gamma", mapnik::keys::gamma) .value("gamma_method",mapnik::keys::gamma_method) ; class_("Symbolizer",no_init) .def("type",get_symbolizer_type) .def("__hash__",hash_impl) .def("extract", extract_underlying_type) ; class_("NumericWrapper") .def("__init__", make_constructor(numeric_wrapper)) ; class_("SymbolizerBase",no_init) .def("__setitem__",&__setitem__) .def("__setattr__",&__setitem__) .def("__getitem__",&__getitem__) .def("__getattr__",&__getitem__) .def("__str__", &__str__) .def(self == self) // __eq__ ; } void export_shield_symbolizer() { using namespace boost::python; class_< shield_symbolizer, bases >("ShieldSymbolizer", init<>("Default ctor")) .def("__hash__",hash_impl_2) ; } void export_polygon_symbolizer() { using namespace boost::python; class_ >("PolygonSymbolizer", init<>("Default ctor")) .def("__hash__",hash_impl_2) ; } void export_polygon_pattern_symbolizer() { using namespace boost::python; mapnik::enumeration_("pattern_alignment") .value("LOCAL",mapnik::LOCAL_ALIGNMENT) .value("GLOBAL",mapnik::GLOBAL_ALIGNMENT) ; class_("PolygonPatternSymbolizer", init<>("Default ctor")) .def("__hash__",hash_impl_2) ; } void export_raster_symbolizer() { using namespace boost::python; class_ >("RasterSymbolizer", init<>("Default ctor")) ; } void export_point_symbolizer() { using namespace boost::python; mapnik::enumeration_("point_placement") .value("CENTROID",mapnik::CENTROID_POINT_PLACEMENT) .value("INTERIOR",mapnik::INTERIOR_POINT_PLACEMENT) ; class_ >("PointSymbolizer", init<>("Default Point Symbolizer - 4x4 black square")) .def("__hash__",hash_impl_2) ; } void export_markers_symbolizer() { using namespace boost::python; mapnik::enumeration_("marker_placement") .value("POINT_PLACEMENT",mapnik::MARKER_POINT_PLACEMENT) .value("INTERIOR_PLACEMENT",mapnik::MARKER_INTERIOR_PLACEMENT) .value("LINE_PLACEMENT",mapnik::MARKER_LINE_PLACEMENT) ; mapnik::enumeration_("marker_multi_policy") .value("EACH",mapnik::MARKER_EACH_MULTI) .value("WHOLE",mapnik::MARKER_WHOLE_MULTI) .value("LARGEST",mapnik::MARKER_LARGEST_MULTI) ; class_ >("MarkersSymbolizer", init<>("Default Markers Symbolizer - circle")) .def("__hash__",hash_impl_2) ; } void export_line_symbolizer() { using namespace boost::python; mapnik::enumeration_("line_rasterizer") .value("FULL",mapnik::RASTERIZER_FULL) .value("FAST",mapnik::RASTERIZER_FAST) ; mapnik::enumeration_("stroke_linecap", "The possible values for a line cap used when drawing\n" "with a stroke.\n") .value("BUTT_CAP",mapnik::BUTT_CAP) .value("SQUARE_CAP",mapnik::SQUARE_CAP) .value("ROUND_CAP",mapnik::ROUND_CAP) ; mapnik::enumeration_("stroke_linejoin", "The possible values for the line joining mode\n" "when drawing with a stroke.\n") .value("MITER_JOIN",mapnik::MITER_JOIN) .value("MITER_REVERT_JOIN",mapnik::MITER_REVERT_JOIN) .value("ROUND_JOIN",mapnik::ROUND_JOIN) .value("BEVEL_JOIN",mapnik::BEVEL_JOIN) ; class_ >("LineSymbolizer", init<>("Default LineSymbolizer - 1px solid black")) .def("__hash__",hash_impl_2) ; } void export_line_pattern_symbolizer() { using namespace boost::python; class_ >("LinePatternSymbolizer", init<> ("Default LinePatternSymbolizer")) .def("__hash__",hash_impl_2) ; } void export_debug_symbolizer() { using namespace boost::python; mapnik::enumeration_("debug_symbolizer_mode") .value("COLLISION",mapnik::DEBUG_SYM_MODE_COLLISION) .value("VERTEX",mapnik::DEBUG_SYM_MODE_VERTEX) ; class_ >("DebugSymbolizer", init<>("Default debug Symbolizer")) .def("__hash__",hash_impl_2) ; } void export_building_symbolizer() { using namespace boost::python; class_ >("BuildingSymbolizer", init<>("Default BuildingSymbolizer")) .def("__hash__",hash_impl_2) ; } namespace { void group_symbolizer_properties_set_layout_simple(mapnik::group_symbolizer_properties &p, mapnik::simple_row_layout &s) { p.set_layout(s); } void group_symbolizer_properties_set_layout_pair(mapnik::group_symbolizer_properties &p, mapnik::pair_layout &s) { p.set_layout(s); } std::shared_ptr group_rule_construct1(mapnik::expression_ptr p) { return std::make_shared(p, mapnik::expression_ptr()); } } // anonymous namespace void export_group_symbolizer() { using namespace boost::python; using mapnik::group_rule; using mapnik::simple_row_layout; using mapnik::pair_layout; using mapnik::group_symbolizer_properties; class_ >("GroupRule", init()) .def("__init__", boost::python::make_constructor(group_rule_construct1)) .def("append", &group_rule::append) .def("set_filter", &group_rule::set_filter) .def("set_repeat_key", &group_rule::set_repeat_key) ; class_("SimpleRowLayout") .def("item_margin", &simple_row_layout::get_item_margin) .def("set_item_margin", &simple_row_layout::set_item_margin) ; class_("PairLayout") .def("item_margin", &simple_row_layout::get_item_margin) .def("set_item_margin", &simple_row_layout::set_item_margin) .def("max_difference", &pair_layout::get_max_difference) .def("set_max_difference", &pair_layout::set_max_difference) ; class_ >("GroupSymbolizerProperties") .def("add_rule", &group_symbolizer_properties::add_rule) .def("set_layout", &group_symbolizer_properties_set_layout_simple) .def("set_layout", &group_symbolizer_properties_set_layout_pair) ; class_ >("GroupSymbolizer", init<>("Default GroupSymbolizer")) .def("__hash__",hash_impl_2) ; }