mapnik/bindings/python/mapnik_parameters.cpp

247 lines
7.6 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
2011-11-14 04:54:32 +01:00
*
2006-03-31 12:32:02 +02:00
* This file is part of Mapnik (c++ mapping toolkit)
2006-02-01 00:09:52 +01:00
*
2014-11-20 15:25:50 +01:00
* Copyright (C) 2014 Artem Pavlenko
2006-02-01 00:09:52 +01:00
*
2006-03-31 12:32:02 +02:00
* 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,
2006-02-01 00:09:52 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02:00
* 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
2006-02-01 00:09:52 +01:00
*
2006-03-31 12:32:02 +02:00
*****************************************************************************/
2006-02-01 00:09:52 +01:00
2014-08-12 18:43:04 +02:00
#include <mapnik/config.hpp>
2014-10-22 01:37:27 +02:00
// boost
#include "boost_std_shared_shim.hpp"
2014-10-22 01:37:27 +02: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"
2006-02-01 00:09:52 +01:00
#include <boost/python.hpp>
2014-10-22 01:37:27 +02:00
#pragma GCC diagnostic pop
// mapnik
2012-04-08 02:20:56 +02:00
#include <mapnik/debug.hpp>
#include <mapnik/params.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/value.hpp>
// stl
#include <iterator>
2006-02-01 00:09:52 +01:00
using mapnik::parameter;
using mapnik::parameters;
struct parameter_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const parameter& p)
{
using namespace boost::python;
return boost::python::make_tuple(p.first,p.second);
2006-02-01 00:09:52 +01:00
}
};
struct parameters_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getstate(const parameters& p)
{
using namespace boost::python;
dict d;
parameters::const_iterator pos=p.begin();
while(pos!=p.end())
{
d[pos->first] = pos->second;
2006-02-01 00:09:52 +01:00
++pos;
}
return boost::python::make_tuple(d);
}
static void setstate(parameters& p, boost::python::tuple state)
{
using namespace boost::python;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
2010-06-02 13:03:30 +02:00
("expected 1-item tuple in call to __setstate__; got %s"
% state).ptr()
);
2006-02-01 00:09:52 +01:00
throw_error_already_set();
}
2011-11-14 04:54:32 +01:00
2006-02-01 00:09:52 +01:00
dict d = extract<dict>(state[0]);
boost::python::list keys = d.keys();
for (int i=0; i<len(keys); ++i)
2006-02-01 00:09:52 +01:00
{
std::string key = extract<std::string>(keys[i]);
object obj = d[key];
extract<std::string> ex0(obj);
extract<mapnik::value_integer> ex1(obj);
extract<double> ex2(obj);
extract<mapnik::value_unicode_string> ex3(obj);
2011-11-14 04:54:32 +01:00
// TODO - this is never hit - we need proper python string -> std::string to get invoked here
if (ex0.check())
{
2010-06-02 13:03:30 +02:00
p[key] = ex0();
}
else if (ex1.check())
{
2010-06-02 13:03:30 +02:00
p[key] = ex1();
}
else if (ex2.check())
{
2010-06-02 13:03:30 +02:00
p[key] = ex2();
2011-11-14 04:54:32 +01:00
}
else if (ex3.check())
{
std::string buffer;
mapnik::to_utf8(ex3(),buffer);
p[key] = buffer;
}
else
{
MAPNIK_LOG_DEBUG(bindings) << "parameters_pickle_suite: Could not unpickle key=" << key;
}
2011-11-14 04:54:32 +01:00
}
2006-02-01 00:09:52 +01:00
}
};
mapnik::value_holder get_params_by_key1(mapnik::parameters const& p, std::string const& key)
{
parameters::const_iterator pos = p.find(key);
if (pos != p.end())
{
// will be auto-converted to proper python type by `mapnik_params_to_python`
return pos->second;
}
return mapnik::value_null();
}
mapnik::value_holder get_params_by_key2(mapnik::parameters const& p, std::string const& key)
{
parameters::const_iterator pos = p.find(key);
if (pos == p.end())
{
PyErr_SetString(PyExc_KeyError, key.c_str());
boost::python::throw_error_already_set();
}
// will be auto-converted to proper python type by `mapnik_params_to_python`
return pos->second;
}
mapnik::parameter get_params_by_index(mapnik::parameters const& p, int index)
{
if (index < 0 || static_cast<unsigned>(index) > p.size())
{
PyErr_SetString(PyExc_IndexError, "Index is out of range");
throw boost::python::error_already_set();
}
parameters::const_iterator itr = p.begin();
std::advance(itr, index);
if (itr != p.end())
{
return *itr;
}
PyErr_SetString(PyExc_IndexError, "Index is out of range");
throw boost::python::error_already_set();
}
unsigned get_params_size(mapnik::parameters const& p)
{
return p.size();
}
void add_parameter(mapnik::parameters & p, mapnik::parameter const& param)
{
p[param.first] = param.second;
}
2006-02-01 00:09:52 +01:00
mapnik::value_holder get_param(mapnik::parameter const& p, int index)
{
if (index == 0)
{
return p.first;
}
else if (index == 1)
{
return p.second;
}
else
{
PyErr_SetString(PyExc_IndexError, "Index is out of range");
throw boost::python::error_already_set();
}
}
std::shared_ptr<mapnik::parameter> create_parameter(mapnik::value_unicode_string const& key, mapnik::value_holder const& value)
{
std::string key_utf8;
mapnik::to_utf8(key, key_utf8);
return std::make_shared<mapnik::parameter>(key_utf8,value);
}
2014-12-09 12:37:10 +01:00
bool contains(mapnik::parameters const& p, std::string const& key)
{
parameters::const_iterator pos = p.find(key);
return pos != p.end();
}
// needed for Python_Unicode to std::string (utf8) conversion
std::shared_ptr<mapnik::parameter> create_parameter_from_string(mapnik::value_unicode_string const& key, mapnik::value_unicode_string const& ustr)
{
std::string key_utf8;
std::string ustr_utf8;
mapnik::to_utf8(key, key_utf8);
mapnik::to_utf8(ustr,ustr_utf8);
return std::make_shared<mapnik::parameter>(key_utf8, ustr_utf8);
}
2006-02-01 00:09:52 +01:00
void export_parameters()
{
using namespace boost::python;
implicitly_convertible<std::string,mapnik::value_holder>();
implicitly_convertible<mapnik::value_null,mapnik::value_holder>();
implicitly_convertible<mapnik::value_integer,mapnik::value_holder>();
implicitly_convertible<mapnik::value_double,mapnik::value_holder>();
class_<parameter,std::shared_ptr<parameter> >("Parameter",no_init)
.def("__init__", make_constructor(create_parameter),
"Create a mapnik.Parameter from a pair of values, the first being a string\n"
"and the second being either a string, and integer, or a float")
.def("__init__", make_constructor(create_parameter_from_string),
"Create a mapnik.Parameter from a pair of values, the first being a string\n"
"and the second being either a string, and integer, or a float")
2006-02-01 00:09:52 +01:00
.def_pickle(parameter_pickle_suite())
.def("__getitem__",get_param)
2006-02-01 00:09:52 +01:00
;
class_<parameters>("Parameters",init<>())
2006-02-01 00:09:52 +01:00
.def_pickle(parameters_pickle_suite())
.def("get",get_params_by_key1)
.def("__getitem__",get_params_by_key2)
.def("__getitem__",get_params_by_index)
.def("__len__",get_params_size)
2014-12-09 12:37:10 +01:00
.def("__contains__",contains)
.def("append",add_parameter)
.def("iteritems",iterator<parameters>())
2006-02-01 00:09:52 +01:00
;
2010-06-02 13:03:30 +02:00
}