mapnik/bindings/python/mapnik_feature.cpp

238 lines
7.4 KiB
C++
Raw Normal View History

/*****************************************************************************
2011-11-14 03:54:32 +00:00
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2014-11-20 14:25:50 +00:00
* Copyright (C) 2014 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
*
*****************************************************************************/
#include <mapnik/config.hpp>
2014-10-21 23:37:27 +00:00
// boost
#include "boost_std_shared_shim.hpp"
2014-10-21 23:37:27 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include <boost/python.hpp>
#include <boost/python/iterator.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/to_python_converter.hpp>
2012-12-16 20:50:36 +00:00
#include <boost/noncopyable.hpp>
2014-10-21 23:37:27 +00:00
#pragma GCC diagnostic pop
2012-12-16 20:50:36 +00:00
// mapnik
#include <mapnik/value_types.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/feature_kv_iterator.hpp>
2009-12-16 20:02:06 +00:00
#include <mapnik/datasource.hpp>
#include <mapnik/wkb.hpp>
2015-03-05 09:45:27 +00:00
//#include <mapnik/wkt/wkt_factory.hpp>
#include <mapnik/json/feature_parser.hpp>
2014-07-24 21:31:59 +00:00
#include <mapnik/json/feature_generator.hpp>
2013-06-03 03:19:33 +00:00
// stl
#include <stdexcept>
namespace {
using mapnik::geometry_utils;
using mapnik::context_type;
2012-01-12 10:04:49 +00:00
using mapnik::context_ptr;
using mapnik::feature_kv_iterator;
mapnik::feature_ptr from_geojson_impl(std::string const& json, mapnik::context_ptr const& ctx)
{
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx,1));
2014-07-24 21:31:59 +00:00
if (!mapnik::json::from_geojson(json,*feature))
{
throw std::runtime_error("Failed to parse geojson feature");
}
return feature;
}
std::string feature_to_geojson(mapnik::feature_impl const& feature)
{
std::string json;
2014-07-24 21:31:59 +00:00
if (!mapnik::json::to_geojson(json,feature))
{
throw std::runtime_error("Failed to generate GeoJSON");
}
return json;
}
mapnik::value __getitem__(mapnik::feature_impl const& feature, std::string const& name)
{
return feature.get(name);
}
mapnik::value __getitem2__(mapnik::feature_impl const& feature, std::size_t index)
2012-01-19 21:57:46 +00:00
{
return feature.get(index);
}
void __setitem__(mapnik::feature_impl & feature, std::string const& name, mapnik::value const& val)
{
feature.put_new(name,val);
}
boost::python::dict attributes(mapnik::feature_impl const& f)
{
boost::python::dict attributes;
feature_kv_iterator itr = f.begin();
feature_kv_iterator end = f.end();
for ( ;itr!=end; ++itr)
{
attributes[std::get<0>(*itr)] = std::get<1>(*itr);
}
return attributes;
}
} // end anonymous namespace
struct unicode_string_from_python_str
2009-12-16 20:02:06 +00:00
{
unicode_string_from_python_str()
2009-12-16 20:02:06 +00:00
{
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id<mapnik::value_unicode_string>());
2009-12-16 20:02:06 +00:00
}
static void* convertible(PyObject* obj_ptr)
{
if (!(
#if PY_VERSION_HEX >= 0x03000000
2011-11-14 03:54:32 +00:00
PyBytes_Check(obj_ptr)
#else
2011-11-14 03:54:32 +00:00
PyString_Check(obj_ptr)
#endif
|| PyUnicode_Check(obj_ptr)))
return 0;
return obj_ptr;
2009-12-16 20:02:06 +00:00
}
static void construct(
PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
2009-12-16 20:02:06 +00:00
{
char * value=0;
if (PyUnicode_Check(obj_ptr)) {
PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace");
if (encoded) {
#if PY_VERSION_HEX >= 0x03000000
value = PyBytes_AsString(encoded);
#else
value = PyString_AsString(encoded);
#endif
Py_DecRef(encoded);
}
} else {
#if PY_VERSION_HEX >= 0x03000000
value = PyBytes_AsString(obj_ptr);
#else
value = PyString_AsString(obj_ptr);
#endif
}
if (value == 0) boost::python::throw_error_already_set();
void* storage = (
(boost::python::converter::rvalue_from_python_storage<mapnik::value_unicode_string>*)
data)->storage.bytes;
new (storage) mapnik::value_unicode_string(value);
data->convertible = storage;
2009-12-16 20:02:06 +00: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;
}
};
void export_feature()
{
2009-12-16 20:02:06 +00:00
using namespace boost::python;
2011-11-14 03:54:32 +00:00
// Python to mapnik::value converters
// NOTE: order matters here. For example value_null must be listed before
// bool otherwise Py_None will be interpreted as bool (false)
implicitly_convertible<mapnik::value_unicode_string,mapnik::value>();
implicitly_convertible<mapnik::value_null,mapnik::value>();
implicitly_convertible<mapnik::value_integer,mapnik::value>();
implicitly_convertible<mapnik::value_double,mapnik::value>();
implicitly_convertible<mapnik::value_bool,mapnik::value>();
// http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/
unicode_string_from_python_str();
value_null_from_python();
class_<context_type,context_ptr,boost::noncopyable>
("Context",init<>("Default ctor."))
.def("push", &context_type::push)
;
class_<mapnik::feature_impl,std::shared_ptr<mapnik::feature_impl>,
boost::noncopyable>("Feature",init<context_ptr,mapnik::value_integer>("Default ctor."))
.def("id",&mapnik::feature_impl::id)
2015-03-23 09:44:40 +00:00
.add_property("geometry",
make_function(&mapnik::feature_impl::get_geometry,return_value_policy<reference_existing_object>()),
&mapnik::feature_impl::set_geometry_copy)
.def("envelope", &mapnik::feature_impl::envelope)
.def("has_key", &mapnik::feature_impl::has_key)
.add_property("attributes",&attributes)
2012-01-13 16:31:13 +00:00
.def("__setitem__",&__setitem__)
.def("__contains__",&__getitem__)
2012-01-19 21:57:46 +00:00
.def("__getitem__",&__getitem__)
.def("__getitem__",&__getitem2__)
.def("__len__", &mapnik::feature_impl::size)
.def("context",&mapnik::feature_impl::context)
.def("to_geojson",&feature_to_geojson)
.def("from_geojson",from_geojson_impl)
.staticmethod("from_geojson")
;
}