2006-12-06 20:26:59 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* 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
|
2007-06-05 09:12:30 +00:00
|
|
|
#include <boost/python/suite/indexing/indexing_suite.hpp>
|
|
|
|
#include <boost/python/iterator.hpp>
|
|
|
|
#include <boost/python/call_method.hpp>
|
|
|
|
#include <boost/python/tuple.hpp>
|
2006-12-06 20:26:59 +00:00
|
|
|
#include <boost/python.hpp>
|
2009-03-31 22:59:34 +00:00
|
|
|
#include <boost/scoped_array.hpp>
|
2006-12-06 20:26:59 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/feature.hpp>
|
2009-12-16 20:02:06 +00:00
|
|
|
#include <mapnik/datasource.hpp>
|
2010-03-19 13:42:25 +00:00
|
|
|
#include <mapnik/wkb.hpp>
|
2006-12-06 20:26:59 +00:00
|
|
|
|
2009-01-13 00:56:09 +00:00
|
|
|
mapnik::geometry2d & (mapnik::Feature::*get_geom1)(unsigned) = &mapnik::Feature::get_geometry;
|
|
|
|
|
2010-03-19 13:42:25 +00:00
|
|
|
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
|
|
|
|
|
2007-06-05 09:12:30 +00:00
|
|
|
namespace boost { namespace python {
|
2010-06-02 11:03:30 +00:00
|
|
|
struct value_converter : public boost::static_visitor<PyObject*>
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
PyObject * operator() (int val) const
|
|
|
|
{
|
|
|
|
return ::PyInt_FromLong(val);
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
PyObject * operator() (double val) const
|
|
|
|
{
|
|
|
|
return ::PyFloat_FromDouble(val);
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
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<ssize_t>(buffer.length()),0);
|
|
|
|
return obj;
|
|
|
|
}
|
2008-04-12 15:22:27 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
PyObject * operator() (mapnik::value_null const& s) const
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
struct mapnik_value_to_python
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
static PyObject* convert(mapnik::value const& v)
|
|
|
|
{
|
|
|
|
return boost::apply_visitor(value_converter(),v.base());
|
|
|
|
}
|
|
|
|
};
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
// Forward declaration
|
2010-06-02 11:03:30 +00:00
|
|
|
template <class Container, bool NoProxy, class DerivedPolicies>
|
|
|
|
class map_indexing_suite2;
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template <class Container, bool NoProxy>
|
|
|
|
class final_map_derived_policies
|
|
|
|
: public map_indexing_suite2<Container,
|
|
|
|
NoProxy, final_map_derived_policies<Container, NoProxy> > {};
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
template <class Container,bool NoProxy = false,
|
|
|
|
class DerivedPolicies = detail::final_map_derived_policies<Container, NoProxy> >
|
|
|
|
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:
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
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;
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
template <class Class>
|
|
|
|
static void
|
|
|
|
extension_def(Class& cl)
|
|
|
|
{
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
static void
|
|
|
|
set_item(Container& container, index_type i, data_type const& v)
|
|
|
|
{
|
|
|
|
container[i] = v;
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
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();
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
static bool
|
|
|
|
compare_index(Container& container, index_type a, index_type b)
|
|
|
|
{
|
|
|
|
return container.props().key_comp()(a, b);
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
static index_type
|
|
|
|
convert_index(Container& /*container*/, PyObject* i_)
|
|
|
|
{
|
|
|
|
extract<key_type const&> i(i_);
|
|
|
|
if (i.check())
|
|
|
|
{
|
|
|
|
return i();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extract<key_type> i(i_);
|
|
|
|
if (i.check())
|
|
|
|
return i();
|
|
|
|
}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "Invalid index type");
|
|
|
|
throw_error_already_set();
|
|
|
|
return index_type();
|
|
|
|
}
|
|
|
|
};
|
2007-06-05 09:12:30 +00:00
|
|
|
|
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
struct std_pair_to_tuple
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
static PyObject* convert(std::pair<T1, T2> const& p)
|
|
|
|
{
|
|
|
|
return boost::python::incref(
|
|
|
|
boost::python::make_tuple(p.first, p.second).ptr());
|
|
|
|
}
|
|
|
|
};
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
struct std_pair_to_python_converter
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
std_pair_to_python_converter()
|
|
|
|
{
|
|
|
|
boost::python::to_python_converter<
|
|
|
|
std::pair<T1, T2>,
|
|
|
|
std_pair_to_tuple<T1, T2> >();
|
|
|
|
}
|
|
|
|
};
|
2009-12-16 20:02:06 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
}}
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
struct UnicodeString_from_python_str
|
|
|
|
{
|
|
|
|
UnicodeString_from_python_str()
|
|
|
|
{
|
2010-03-19 13:42:58 +00:00
|
|
|
boost::python::converter::registry::push_back(
|
|
|
|
&convertible,
|
|
|
|
&construct,
|
|
|
|
boost::python::type_id<UnicodeString>());
|
2009-12-16 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* convertible(PyObject* obj_ptr)
|
|
|
|
{
|
2010-03-19 13:42:58 +00:00
|
|
|
if (!(PyString_Check(obj_ptr) || PyUnicode_Check(obj_ptr)))
|
|
|
|
return 0;
|
|
|
|
return obj_ptr;
|
2009-12-16 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void construct(
|
2010-03-19 13:42:58 +00:00
|
|
|
PyObject* obj_ptr,
|
|
|
|
boost::python::converter::rvalue_from_python_stage1_data* data)
|
2009-12-16 20:02:06 +00:00
|
|
|
{
|
2010-03-19 13:42:58 +00:00
|
|
|
char * value=0;
|
|
|
|
if (PyUnicode_Check(obj_ptr)) {
|
|
|
|
PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace");
|
|
|
|
if (encoded) {
|
|
|
|
value = PyString_AsString(encoded);
|
|
|
|
Py_DecRef(encoded);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = PyString_AsString(obj_ptr);
|
|
|
|
}
|
|
|
|
if (value == 0) boost::python::throw_error_already_set();
|
|
|
|
void* storage = (
|
|
|
|
(boost::python::converter::rvalue_from_python_storage<UnicodeString>*)
|
|
|
|
data)->storage.bytes;
|
|
|
|
new (storage) UnicodeString(value);
|
|
|
|
data->convertible = storage;
|
2009-12-16 20:02:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-12-06 20:26:59 +00:00
|
|
|
void export_feature()
|
|
|
|
{
|
2009-12-16 20:02:06 +00:00
|
|
|
using namespace boost::python;
|
|
|
|
using mapnik::Feature;
|
|
|
|
|
|
|
|
implicitly_convertible<int,mapnik::value>();
|
|
|
|
implicitly_convertible<double,mapnik::value>();
|
2010-03-19 13:42:58 +00:00
|
|
|
implicitly_convertible<UnicodeString,mapnik::value>();
|
2009-12-16 20:02:06 +00:00
|
|
|
implicitly_convertible<bool,mapnik::value>();
|
2007-06-05 09:12:30 +00:00
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
std_pair_to_python_converter<std::string const,mapnik::value>();
|
|
|
|
to_python_converter<mapnik::value,mapnik_value_to_python>();
|
2010-03-19 13:42:58 +00:00
|
|
|
UnicodeString_from_python_str();
|
2009-12-16 20:02:06 +00:00
|
|
|
|
|
|
|
class_<Feature,boost::shared_ptr<Feature>,
|
2010-06-02 11:03:30 +00:00
|
|
|
boost::noncopyable>("Feature",init<int>("Default ctor."))
|
|
|
|
.def("id",&Feature::id)
|
|
|
|
.def("__str__",&Feature::to_string)
|
|
|
|
.def("add_geometry", &feature_add_wkb_geometry)
|
|
|
|
.def("num_geometries",&Feature::num_geometries)
|
|
|
|
.def("get_geometry", make_function(get_geom1,return_value_policy<reference_existing_object>()))
|
|
|
|
.def("envelope", &Feature::envelope)
|
|
|
|
.def(map_indexing_suite2<Feature, true >())
|
|
|
|
.def("iteritems",iterator<Feature> ())
|
|
|
|
// TODO define more mapnik::Feature methods
|
|
|
|
;
|
2006-12-06 20:26:59 +00:00
|
|
|
}
|