2006-12-06 20:26:59 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:54:32 +00:00
|
|
|
*
|
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
|
2012-01-13 16:31:13 +00:00
|
|
|
|
2007-06-05 09:12:30 +00:00
|
|
|
#include <boost/python/suite/indexing/indexing_suite.hpp>
|
2012-01-18 19:34:22 +00:00
|
|
|
//#include <boost/python/suite/indexing/map_indexing_suite.hpp>
|
2012-01-13 16:31:13 +00:00
|
|
|
|
2007-06-05 09:12:30 +00:00
|
|
|
#include <boost/python/iterator.hpp>
|
|
|
|
#include <boost/python/call_method.hpp>
|
|
|
|
#include <boost/python/tuple.hpp>
|
2012-01-17 22:33:43 +00:00
|
|
|
#include <boost/python/to_python_converter.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>
|
2011-04-26 17:39:21 +00:00
|
|
|
|
2006-12-06 20:26:59 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/feature.hpp>
|
2012-01-17 22:33:43 +00:00
|
|
|
#include <mapnik/feature_kv_iterator.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>
|
2011-09-13 11:54:12 +00:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp>
|
2006-12-06 20:26:59 +00:00
|
|
|
|
2010-03-19 13:42:25 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::geometry_utils;
|
2011-09-13 11:54:12 +00:00
|
|
|
using mapnik::from_wkt;
|
2012-01-19 22:36:27 +00:00
|
|
|
using mapnik::context_type;
|
2012-01-12 10:04:49 +00:00
|
|
|
using mapnik::context_ptr;
|
2012-01-17 22:33:43 +00:00
|
|
|
using mapnik::feature_kv_iterator;
|
2010-03-19 13:42:25 +00:00
|
|
|
|
2012-01-23 14:22:14 +00:00
|
|
|
mapnik::geometry_type const& (mapnik::Feature::*get_geometry_by_const_ref)(unsigned) const = &mapnik::Feature::get_geometry;
|
|
|
|
boost::ptr_vector<mapnik::geometry_type> const& (mapnik::Feature::*get_paths_by_const_ref)() const = &mapnik::Feature::paths;
|
|
|
|
|
2011-09-13 11:54:12 +00:00
|
|
|
void feature_add_geometries_from_wkb(Feature &feature, std::string wkb)
|
2010-03-19 13:42:25 +00:00
|
|
|
{
|
2011-12-06 12:53:16 +00:00
|
|
|
geometry_utils::from_wkb(feature.paths(), wkb.c_str(), wkb.size());
|
2010-03-19 13:42:25 +00:00
|
|
|
}
|
|
|
|
|
2011-09-13 11:54:12 +00:00
|
|
|
void feature_add_geometries_from_wkt(Feature &feature, std::string wkt)
|
2011-02-28 12:40:59 +00:00
|
|
|
{
|
2011-09-13 11:54:12 +00:00
|
|
|
bool result = mapnik::from_wkt(wkt, feature.paths());
|
|
|
|
if (!result) throw std::runtime_error("Failed to parse WKT");
|
2011-02-28 12:40:59 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 22:33:43 +00:00
|
|
|
mapnik::value __getitem__(Feature const& feature, std::string const& name)
|
2012-01-17 16:23:32 +00:00
|
|
|
{
|
|
|
|
return feature.get(name);
|
|
|
|
}
|
|
|
|
|
2012-01-19 21:57:46 +00:00
|
|
|
mapnik::value __getitem2__(Feature const& feature, std::size_t index)
|
|
|
|
{
|
|
|
|
return feature.get(index);
|
|
|
|
}
|
|
|
|
|
2012-01-13 12:03:26 +00:00
|
|
|
void __setitem__(Feature & feature, std::string const& name, mapnik::value const& val)
|
|
|
|
{
|
|
|
|
feature.put(name,val);
|
|
|
|
}
|
|
|
|
|
2010-03-19 13:42:25 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-06-05 09:12:30 +00:00
|
|
|
namespace boost { namespace python {
|
2011-04-26 17:39:21 +00:00
|
|
|
|
2012-01-13 16:31:13 +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-09-26 12:15:16 +00:00
|
|
|
if (!(
|
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2011-11-14 03:54:32 +00:00
|
|
|
PyBytes_Check(obj_ptr)
|
2010-09-26 12:15:16 +00:00
|
|
|
#else
|
2011-11-14 03:54:32 +00:00
|
|
|
PyString_Check(obj_ptr)
|
2010-09-26 12:15:16 +00:00
|
|
|
#endif
|
|
|
|
|| PyUnicode_Check(obj_ptr)))
|
2010-03-19 13:42:58 +00:00
|
|
|
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) {
|
2010-09-26 12:15:16 +00:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
value = PyBytes_AsString(encoded);
|
|
|
|
#else
|
2010-03-19 13:42:58 +00:00
|
|
|
value = PyString_AsString(encoded);
|
2010-09-26 12:15:16 +00:00
|
|
|
#endif
|
2010-03-19 13:42:58 +00:00
|
|
|
Py_DecRef(encoded);
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-26 12:15:16 +00:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
value = PyBytes_AsString(obj_ptr);
|
|
|
|
#else
|
2010-03-19 13:42:58 +00:00
|
|
|
value = PyString_AsString(obj_ptr);
|
2010-09-26 12:15:16 +00:00
|
|
|
#endif
|
2010-03-19 13:42:58 +00:00
|
|
|
}
|
|
|
|
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;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2012-01-17 22:33:43 +00:00
|
|
|
// Python to mapnik::value converters
|
2009-12-16 20:02:06 +00:00
|
|
|
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>();
|
2012-01-13 16:31:13 +00:00
|
|
|
|
2010-03-19 13:42:58 +00:00
|
|
|
UnicodeString_from_python_str();
|
2012-01-17 22:33:43 +00:00
|
|
|
|
2012-01-19 22:36:27 +00:00
|
|
|
class_<context_type,context_ptr,boost::noncopyable>
|
2012-01-13 12:03:26 +00:00
|
|
|
("Context",init<>("Default ctor."))
|
2012-01-19 22:36:27 +00:00
|
|
|
.def("push", &context_type::push)
|
2012-01-13 12:03:26 +00:00
|
|
|
;
|
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
class_<Feature,boost::shared_ptr<Feature>,
|
2012-01-13 12:03:26 +00:00
|
|
|
boost::noncopyable>("Feature",init<context_ptr,int>("Default ctor."))
|
2012-01-13 16:31:13 +00:00
|
|
|
.def("id",&Feature::id)
|
|
|
|
.def("__str__",&Feature::to_string)
|
|
|
|
.def("add_geometries_from_wkb", &feature_add_geometries_from_wkb)
|
|
|
|
.def("add_geometries_from_wkt", &feature_add_geometries_from_wkt)
|
|
|
|
.def("add_geometry", &Feature::add_geometry)
|
|
|
|
.def("num_geometries",&Feature::num_geometries)
|
2012-01-23 14:22:14 +00:00
|
|
|
.def("get_geometry", make_function(get_geometry_by_const_ref,return_value_policy<reference_existing_object>()))
|
|
|
|
.def("geometries",make_function(get_paths_by_const_ref,return_value_policy<reference_existing_object>()))
|
2012-01-13 16:31:13 +00:00
|
|
|
.def("envelope", &Feature::envelope)
|
2012-01-17 16:23:32 +00:00
|
|
|
.def("has_key", &Feature::has_key)
|
2012-01-13 16:31:13 +00:00
|
|
|
.def("__setitem__",&__setitem__)
|
2012-01-19 21:57:46 +00:00
|
|
|
.def("__getitem__",&__getitem__)
|
|
|
|
.def("__getitem__",&__getitem2__)
|
2012-01-17 22:33:43 +00:00
|
|
|
.def("__len__", &Feature::size)
|
|
|
|
.def("context",&Feature::context)
|
2012-01-17 16:23:32 +00:00
|
|
|
;
|
2006-12-06 20:26:59 +00:00
|
|
|
}
|