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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2013-10-24 00:42:01 +02:00
|
|
|
#include "boost_std_shared_shim.hpp"
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
// boost
|
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>
|
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>
|
2012-12-16 21:50:36 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2013-07-30 21:45:46 +02:00
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
// mapnik
|
2013-08-14 00:52:04 +02:00
|
|
|
#include <mapnik/value_types.hpp>
|
2006-12-06 21:26:59 +01:00
|
|
|
#include <mapnik/feature.hpp>
|
2013-07-30 21:45:46 +02:00
|
|
|
#include <mapnik/feature_factory.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>
|
2013-07-30 21:45:46 +02:00
|
|
|
#include <mapnik/json/feature_parser.hpp>
|
2012-02-20 11:55:25 +01:00
|
|
|
#include <mapnik/json/geojson_generator.hpp>
|
2013-11-04 13:39:10 +01:00
|
|
|
#include <mapnik/json/generic_json.hpp>
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2013-06-03 05:19:33 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2010-03-19 14:42:25 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-07-30 21:45:46 +02:00
|
|
|
mapnik::geometry_type const& (mapnik::feature_impl::*get_geometry_by_const_ref)(std::size_t) const = &mapnik::feature_impl::get_geometry;
|
2013-05-16 20:54:56 +02:00
|
|
|
boost::ptr_vector<mapnik::geometry_type> const& (mapnik::feature_impl::*get_paths_by_const_ref)() const = &mapnik::feature_impl::paths;
|
2012-01-23 15:22:14 +01:00
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
void feature_add_geometries_from_wkb(mapnik::feature_impl &feature, std::string wkb)
|
2010-03-19 14:42:25 +01:00
|
|
|
{
|
2013-07-04 20:31:46 +02:00
|
|
|
bool result = geometry_utils::from_wkb(feature.paths(), wkb.c_str(), wkb.size());
|
|
|
|
if (!result) throw std::runtime_error("Failed to parse WKB");
|
2010-03-19 14:42:25 +01:00
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
void feature_add_geometries_from_wkt(mapnik::feature_impl &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
|
|
|
}
|
|
|
|
|
2013-07-30 21:45:46 +02:00
|
|
|
mapnik::feature_ptr from_geojson_impl(std::string const& json, mapnik::context_ptr const& ctx)
|
|
|
|
{
|
|
|
|
mapnik::transcoder tr("utf8");
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx,1));
|
2013-11-04 13:39:10 +01:00
|
|
|
mapnik::json::generic_json<std::string::const_iterator> json_base;
|
|
|
|
mapnik::json::feature_parser<std::string::const_iterator> parser(json_base, tr);
|
2013-07-30 21:45:46 +02:00
|
|
|
if (!parser.parse(json.begin(), json.end(), *feature))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse geojson feature");
|
|
|
|
}
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
std::string feature_to_geojson(mapnik::feature_impl const& feature)
|
2012-02-20 11:55:25 +01:00
|
|
|
{
|
|
|
|
std::string json;
|
2012-02-20 13:05:11 +01:00
|
|
|
mapnik::json::feature_generator g;
|
2012-02-20 11:55:25 +01:00
|
|
|
if (!g.generate(json,feature))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to generate GeoJSON");
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
mapnik::value __getitem__(mapnik::feature_impl const& feature, std::string const& name)
|
2012-01-17 17:23:32 +01:00
|
|
|
{
|
|
|
|
return feature.get(name);
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
mapnik::value __getitem2__(mapnik::feature_impl const& feature, std::size_t index)
|
2012-01-19 22:57:46 +01:00
|
|
|
{
|
|
|
|
return feature.get(index);
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
void __setitem__(mapnik::feature_impl & feature, std::string const& name, mapnik::value const& val)
|
2012-01-13 13:03:26 +01:00
|
|
|
{
|
2012-01-30 15:05:41 +01:00
|
|
|
feature.put_new(name,val);
|
2012-01-13 13:03:26 +01:00
|
|
|
}
|
|
|
|
|
2013-05-16 20:54:56 +02:00
|
|
|
boost::python::dict attributes(mapnik::feature_impl 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)
|
|
|
|
{
|
2013-10-11 13:35:34 +02:00
|
|
|
attributes[std::get<0>(*itr)] = std::get<1>(*itr);
|
2012-01-24 09:27:27 +01:00
|
|
|
}
|
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
|
|
|
|
2013-08-14 00:52:04 +02:00
|
|
|
struct unicode_string_from_python_str
|
2009-12-16 21:02:06 +01:00
|
|
|
{
|
2013-08-14 00:52:04 +02:00
|
|
|
unicode_string_from_python_str()
|
2009-12-16 21:02:06 +01:00
|
|
|
{
|
2010-03-19 14:42:58 +01:00
|
|
|
boost::python::converter::registry::push_back(
|
|
|
|
&convertible,
|
|
|
|
&construct,
|
2013-08-14 00:52:04 +02:00
|
|
|
boost::python::type_id<mapnik::value_unicode_string>());
|
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 = (
|
2013-08-14 00:52:04 +02:00
|
|
|
(boost::python::converter::rvalue_from_python_storage<mapnik::value_unicode_string>*)
|
2010-03-19 14:42:58 +01:00
|
|
|
data)->storage.bytes;
|
2013-08-14 00:52:04 +02:00
|
|
|
new (storage) mapnik::value_unicode_string(value);
|
2010-03-19 14:42:58 +01:00
|
|
|
data->convertible = storage;
|
2009-12-16 21:02:06 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-12 03:13:33 +01:00
|
|
|
|
|
|
|
struct value_null_from_python
|
|
|
|
{
|
|
|
|
value_null_from_python()
|
|
|
|
{
|
|
|
|
boost::python::converter::registry::push_back(
|
|
|
|
&convertible,
|
|
|
|
&construct,
|
|
|
|
boost::python::type_id<mapnik::value_null>());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* convertible(PyObject* obj_ptr)
|
|
|
|
{
|
|
|
|
if (obj_ptr == Py_None) return obj_ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void construct(
|
|
|
|
PyObject* obj_ptr,
|
|
|
|
boost::python::converter::rvalue_from_python_stage1_data* data)
|
|
|
|
{
|
|
|
|
if (obj_ptr != Py_None) boost::python::throw_error_already_set();
|
|
|
|
void* storage = (
|
|
|
|
(boost::python::converter::rvalue_from_python_storage<mapnik::value_null>*)
|
|
|
|
data)->storage.bytes;
|
|
|
|
new (storage) mapnik::value_null();
|
|
|
|
data->convertible = storage;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
void export_feature()
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
using namespace boost::python;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2012-01-17 23:33:43 +01:00
|
|
|
// Python to mapnik::value converters
|
2012-12-12 03:13:33 +01:00
|
|
|
// NOTE: order matters here. For example value_null must be listed before
|
|
|
|
// bool otherwise Py_None will be interpreted as bool (false)
|
2012-12-18 19:05:45 +01:00
|
|
|
implicitly_convertible<mapnik::value_unicode_string,mapnik::value>();
|
2012-12-12 03:13:33 +01:00
|
|
|
implicitly_convertible<mapnik::value_null,mapnik::value>();
|
2012-12-14 11:24:50 +01:00
|
|
|
implicitly_convertible<mapnik::value_integer,mapnik::value>();
|
|
|
|
implicitly_convertible<mapnik::value_double,mapnik::value>();
|
2012-12-19 10:39:51 +01:00
|
|
|
implicitly_convertible<mapnik::value_bool,mapnik::value>();
|
2012-02-02 02:30:26 +01:00
|
|
|
|
2012-12-12 03:13:33 +01:00
|
|
|
// http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/
|
2013-08-14 00:52:04 +02:00
|
|
|
unicode_string_from_python_str();
|
2012-12-12 03:13:33 +01:00
|
|
|
value_null_from_python();
|
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
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
class_<mapnik::feature_impl,std::shared_ptr<mapnik::feature_impl>,
|
2012-12-20 04:24:52 +01:00
|
|
|
boost::noncopyable>("Feature",init<context_ptr,mapnik::value_integer>("Default ctor."))
|
2013-05-16 20:54:56 +02:00
|
|
|
.def("id",&mapnik::feature_impl::id)
|
|
|
|
.def("__str__",&mapnik::feature_impl::to_string)
|
2012-01-13 17:31:13 +01:00
|
|
|
.def("add_geometries_from_wkb", &feature_add_geometries_from_wkb)
|
|
|
|
.def("add_geometries_from_wkt", &feature_add_geometries_from_wkt)
|
2013-05-16 20:54:56 +02:00
|
|
|
.def("add_geometry", &mapnik::feature_impl::add_geometry)
|
|
|
|
.def("num_geometries",&mapnik::feature_impl::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>()))
|
2013-05-16 20:54:56 +02:00
|
|
|
.def("envelope", &mapnik::feature_impl::envelope)
|
|
|
|
.def("has_key", &mapnik::feature_impl::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__)
|
2013-01-11 08:11:45 +01:00
|
|
|
.def("__contains__",&__getitem__)
|
2012-01-19 22:57:46 +01:00
|
|
|
.def("__getitem__",&__getitem__)
|
|
|
|
.def("__getitem__",&__getitem2__)
|
2013-05-16 20:54:56 +02:00
|
|
|
.def("__len__", &mapnik::feature_impl::size)
|
|
|
|
.def("context",&mapnik::feature_impl::context)
|
2012-02-20 11:55:25 +01:00
|
|
|
.def("to_geojson",&feature_to_geojson)
|
2013-07-30 21:45:46 +02:00
|
|
|
.def("from_geojson",from_geojson_impl)
|
|
|
|
.staticmethod("from_geojson")
|
2012-01-17 17:23:32 +01:00
|
|
|
;
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|