/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon * * 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 * *****************************************************************************/ //$Id$ // boost #include #include #include #include #include #include // mapnik #include #include #include mapnik::geometry2d & (mapnik::Feature::*get_geom1)(unsigned) = &mapnik::Feature::get_geometry; namespace { using mapnik::Feature; using mapnik::geometry_utils; void feature_add_wkb_geometry(Feature &feature, std::string wkb) { geometry_utils::from_wkb(feature, wkb.c_str(), wkb.size(), true); } } // end anonymous namespace namespace boost { namespace python { struct value_converter : public boost::static_visitor { PyObject * operator() (int val) const { return ::PyInt_FromLong(val); } PyObject * operator() (double val) const { return ::PyFloat_FromDouble(val); } PyObject * operator() (UnicodeString const& s) const { std::string buffer; mapnik::to_utf8(s,buffer); PyObject *obj = Py_None; obj = ::PyUnicode_DecodeUTF8(buffer.c_str(),implicit_cast(buffer.length()),0); return obj; } PyObject * operator() (mapnik::value_null const& s) const { return NULL; } }; struct mapnik_value_to_python { static PyObject* convert(mapnik::value const& v) { return boost::apply_visitor(value_converter(),v.base()); } }; // Forward declaration template class map_indexing_suite2; namespace detail { template class final_map_derived_policies : public map_indexing_suite2 > {}; } template < class Container, bool NoProxy = false, class DerivedPolicies = detail::final_map_derived_policies > class map_indexing_suite2 : public indexing_suite< Container , DerivedPolicies , NoProxy , true , typename Container::value_type::second_type , typename Container::key_type , typename Container::key_type > { public: typedef typename Container::value_type value_type; typedef typename Container::value_type::second_type data_type; typedef typename Container::key_type key_type; typedef typename Container::key_type index_type; typedef typename Container::size_type size_type; typedef typename Container::difference_type difference_type; template static void extension_def(Class& cl) { } static data_type& get_item(Container& container, index_type i_) { typename Container::iterator i = container.props().find(i_); if (i == container.end()) { PyErr_SetString(PyExc_KeyError, "Invalid key"); throw_error_already_set(); } return i->second; } static void set_item(Container& container, index_type i, data_type const& v) { container[i] = v; } static void delete_item(Container& container, index_type i) { container.props().erase(i); } static size_t size(Container& container) { return container.props().size(); } static bool contains(Container& container, key_type const& key) { return container.props().find(key) != container.end(); } static bool compare_index(Container& container, index_type a, index_type b) { return container.props().key_comp()(a, b); } static index_type convert_index(Container& /*container*/, PyObject* i_) { extract i(i_); if (i.check()) { return i(); } else { extract i(i_); if (i.check()) return i(); } PyErr_SetString(PyExc_TypeError, "Invalid index type"); throw_error_already_set(); return index_type(); } }; template struct std_pair_to_tuple { static PyObject* convert(std::pair const& p) { return boost::python::incref( boost::python::make_tuple(p.first, p.second).ptr()); } }; template struct std_pair_to_python_converter { std_pair_to_python_converter() { boost::python::to_python_converter< std::pair, std_pair_to_tuple >(); } }; } } /* mapnik::feature_ptr create_feature_(int id) { return mapnik::feature_ptr(new mapnik::Feature(id)); } struct UnicodeString_from_python_str { UnicodeString_from_python_str() { boost::python::converter::registry::push_back( &convertible, &construct, boost::python::type_id()); } static void* convertible(PyObject* obj_ptr) { if (!PyString_Check(obj_ptr)) return 0; return obj_ptr; } static void construct( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data) { const char* value = PyString_AsString(obj_ptr); if (value == 0) boost::python::throw_error_already_set(); void* storage = ( (boost::python::converter::rvalue_from_python_storage*) data)->storage.bytes; new (storage) UnicodeString(value); data->convertible = storage; } }; */ void export_feature() { using namespace boost::python; using mapnik::Feature; implicitly_convertible(); implicitly_convertible(); //implicitly_convertible(); implicitly_convertible(); std_pair_to_python_converter(); to_python_converter(); //UnicodeString_from_python_str(); class_, boost::noncopyable>("Feature",init("Default ctor.")) .def("id",&Feature::id) .def("__str__",&Feature::to_string) // .add_property("properties", // make_function(&Feature::props,return_value_policy())) .def("add_geometry", &feature_add_wkb_geometry) .def("num_geometries",&Feature::num_geometries) .def("get_geometry", make_function(get_geom1,return_value_policy())) .def("envelope", &Feature::envelope) //.def("create",create_feature_) //.staticmethod("create") .def(map_indexing_suite2()) .def("iteritems",iterator ()) // TODO define more mapnik::Feature methods ; //def("Feature", &create_feature_); //class_ >("Properties") // .def(map_indexing_suite2, true >()) // .def("iteritems",iterator > ()) // ; }