2006-12-06 21:26:59 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +01:00
|
|
|
*
|
2006-12-06 21:26:59 +01: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 17:31:13 +01:00
|
|
|
|
2007-06-05 11:12:30 +02:00
|
|
|
#include <boost/python/suite/indexing/indexing_suite.hpp>
|
2012-01-18 20:34:22 +01:00
|
|
|
//#include <boost/python/suite/indexing/map_indexing_suite.hpp>
|
2012-01-13 17:31:13 +01:00
|
|
|
|
2007-06-05 11:12:30 +02:00
|
|
|
#include <boost/python/iterator.hpp>
|
|
|
|
#include <boost/python/call_method.hpp>
|
|
|
|
#include <boost/python/tuple.hpp>
|
2012-01-17 23:33:43 +01:00
|
|
|
#include <boost/python/to_python_converter.hpp>
|
2006-12-06 21:26:59 +01:00
|
|
|
#include <boost/python.hpp>
|
2011-04-26 19:39:21 +02:00
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/feature.hpp>
|
2012-01-17 23:33:43 +01:00
|
|
|
#include <mapnik/feature_kv_iterator.hpp>
|
2009-12-16 21:02:06 +01:00
|
|
|
#include <mapnik/datasource.hpp>
|
2010-03-19 14:42:25 +01:00
|
|
|
#include <mapnik/wkb.hpp>
|
2011-09-13 13:54:12 +02:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp>
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-03-19 14:42:25 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::geometry_utils;
|
2011-09-13 13:54:12 +02:00
|
|
|
using mapnik::from_wkt;
|
2012-01-19 23:36:27 +01:00
|
|
|
using mapnik::context_type;
|
2012-01-12 11:04:49 +01:00
|
|
|
using mapnik::context_ptr;
|
2012-01-17 23:33:43 +01:00
|
|
|
using mapnik::feature_kv_iterator;
|
2010-03-19 14:42:25 +01:00
|
|
|
|
2012-01-23 15:22:14 +01: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 13:54:12 +02:00
|
|
|
void feature_add_geometries_from_wkb(Feature &feature, std::string wkb)
|
2010-03-19 14:42:25 +01:00
|
|
|
{
|
2011-12-06 13:53:16 +01:00
|
|
|
geometry_utils::from_wkb(feature.paths(), wkb.c_str(), wkb.size());
|
2010-03-19 14:42:25 +01:00
|
|
|
}
|
|
|
|
|
2011-09-13 13:54:12 +02:00
|
|
|
void feature_add_geometries_from_wkt(Feature &feature, std::string wkt)
|
2011-02-28 13:40:59 +01:00
|
|
|
{
|
2011-09-13 13:54:12 +02:00
|
|
|
bool result = mapnik::from_wkt(wkt, feature.paths());
|
|
|
|
if (!result) throw std::runtime_error("Failed to parse WKT");
|
2011-02-28 13:40:59 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 23:33:43 +01:00
|
|
|
mapnik::value __getitem__(Feature const& feature, std::string const& name)
|
2012-01-17 17:23:32 +01:00
|
|
|
{
|
|
|
|
return feature.get(name);
|
|
|
|
}
|
|
|
|
|
2012-01-19 22:57:46 +01:00
|
|
|
mapnik::value __getitem2__(Feature const& feature, std::size_t index)
|
|
|
|
{
|
|
|
|
return feature.get(index);
|
|
|
|
}
|
|
|
|
|
2012-01-13 13:03:26 +01:00
|
|
|
void __setitem__(Feature & feature, std::string const& name, mapnik::value const& val)
|
|
|
|
{
|
2012-01-30 15:05:41 +01:00
|
|
|
feature.put_new(name,val);
|
2012-01-13 13:03:26 +01:00
|
|
|
}
|
|
|
|
|
2012-01-24 15:48:30 +01:00
|
|
|
boost::python::dict attributes(Feature const& f)
|
2012-01-24 09:27:27 +01:00
|
|
|
{
|
|
|
|
boost::python::dict attributes;
|
2012-01-24 15:48:30 +01:00
|
|
|
feature_kv_iterator itr = f.begin();
|
|
|
|
feature_kv_iterator end = f.end();
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2012-01-24 09:27:27 +01:00
|
|
|
for ( ;itr!=end; ++itr)
|
|
|
|
{
|
|
|
|
attributes[boost::get<0>(*itr)] = boost::get<1>(*itr);
|
|
|
|
}
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2012-01-24 09:27:27 +01:00
|
|
|
return attributes;
|
|
|
|
}
|
2011-04-26 19:39:21 +02:00
|
|
|
|
2012-01-24 09:27:27 +01:00
|
|
|
} // end anonymous namespace
|
2007-06-05 11:12:30 +02:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
struct UnicodeString_from_python_str
|
|
|
|
{
|
|
|
|
UnicodeString_from_python_str()
|
|
|
|
{
|
2010-03-19 14:42:58 +01:00
|
|
|
boost::python::converter::registry::push_back(
|
|
|
|
&convertible,
|
|
|
|
&construct,
|
|
|
|
boost::python::type_id<UnicodeString>());
|
2009-12-16 21:02:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* convertible(PyObject* obj_ptr)
|
|
|
|
{
|
2010-09-26 14:15:16 +02:00
|
|
|
if (!(
|
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
2011-11-14 04:54:32 +01:00
|
|
|
PyBytes_Check(obj_ptr)
|
2010-09-26 14:15:16 +02:00
|
|
|
#else
|
2011-11-14 04:54:32 +01:00
|
|
|
PyString_Check(obj_ptr)
|
2010-09-26 14:15:16 +02:00
|
|
|
#endif
|
|
|
|
|| PyUnicode_Check(obj_ptr)))
|
2010-03-19 14:42:58 +01:00
|
|
|
return 0;
|
|
|
|
return obj_ptr;
|
2009-12-16 21:02:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void construct(
|
2010-03-19 14:42:58 +01:00
|
|
|
PyObject* obj_ptr,
|
|
|
|
boost::python::converter::rvalue_from_python_stage1_data* data)
|
2009-12-16 21:02:06 +01:00
|
|
|
{
|
2010-03-19 14:42:58 +01:00
|
|
|
char * value=0;
|
|
|
|
if (PyUnicode_Check(obj_ptr)) {
|
|
|
|
PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace");
|
|
|
|
if (encoded) {
|
2010-09-26 14:15:16 +02:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
value = PyBytes_AsString(encoded);
|
|
|
|
#else
|
2010-03-19 14:42:58 +01:00
|
|
|
value = PyString_AsString(encoded);
|
2010-09-26 14:15:16 +02:00
|
|
|
#endif
|
2010-03-19 14:42:58 +01:00
|
|
|
Py_DecRef(encoded);
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-26 14:15:16 +02:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
value = PyBytes_AsString(obj_ptr);
|
|
|
|
#else
|
2010-03-19 14:42:58 +01:00
|
|
|
value = PyString_AsString(obj_ptr);
|
2010-09-26 14:15:16 +02:00
|
|
|
#endif
|
2010-03-19 14:42:58 +01: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 21:02:06 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
void export_feature()
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
using namespace boost::python;
|
|
|
|
using mapnik::Feature;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2012-01-17 23:33:43 +01:00
|
|
|
// Python to mapnik::value converters
|
2009-12-16 21:02:06 +01:00
|
|
|
implicitly_convertible<int,mapnik::value>();
|
|
|
|
implicitly_convertible<double,mapnik::value>();
|
2010-03-19 14:42:58 +01:00
|
|
|
implicitly_convertible<UnicodeString,mapnik::value>();
|
2009-12-16 21:02:06 +01:00
|
|
|
implicitly_convertible<bool,mapnik::value>();
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2010-03-19 14:42:58 +01:00
|
|
|
UnicodeString_from_python_str();
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2012-01-19 23:36:27 +01:00
|
|
|
class_<context_type,context_ptr,boost::noncopyable>
|
2012-01-13 13:03:26 +01:00
|
|
|
("Context",init<>("Default ctor."))
|
2012-01-19 23:36:27 +01:00
|
|
|
.def("push", &context_type::push)
|
2012-01-13 13:03:26 +01:00
|
|
|
;
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
class_<Feature,boost::shared_ptr<Feature>,
|
2012-02-02 02:30:26 +01:00
|
|
|
boost::noncopyable>("Feature",init<context_ptr,int>("Default ctor."))
|
2012-01-13 17:31:13 +01: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 15:22:14 +01: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 17:31:13 +01:00
|
|
|
.def("envelope", &Feature::envelope)
|
2012-01-17 17:23:32 +01:00
|
|
|
.def("has_key", &Feature::has_key)
|
2012-01-24 15:48:30 +01:00
|
|
|
.add_property("attributes",&attributes)
|
2012-01-13 17:31:13 +01:00
|
|
|
.def("__setitem__",&__setitem__)
|
2012-01-19 22:57:46 +01:00
|
|
|
.def("__getitem__",&__getitem__)
|
|
|
|
.def("__getitem__",&__getitem2__)
|
2012-01-17 23:33:43 +01:00
|
|
|
.def("__len__", &Feature::size)
|
|
|
|
.def("context",&Feature::context)
|
2012-01-17 17:23:32 +01:00
|
|
|
;
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|