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
|
|
|
*
|
2011-12-03 02:59:57 +01:00
|
|
|
* Copyright (C) 2011 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
|
2013-10-24 00:42:01 +02:00
|
|
|
#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"
|
2013-10-24 00:42:01 +02:00
|
|
|
|
2006-02-01 00:09:52 +01:00
|
|
|
#include <boost/python.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2009-05-24 08:12:32 +02:00
|
|
|
|
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/params.hpp>
|
2011-12-03 02:59:57 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
2013-08-14 00:52:04 +02:00
|
|
|
#include <mapnik/value_types.hpp>
|
2011-12-03 02:59:57 +01:00
|
|
|
#include <mapnik/value.hpp>
|
2013-01-04 17:17:51 +01:00
|
|
|
// 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;
|
2011-12-03 02:59:57 +01:00
|
|
|
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())
|
|
|
|
{
|
2011-12-03 02:59:57 +01:00
|
|
|
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]);
|
2009-05-24 08:12:32 +02:00
|
|
|
boost::python::list keys = d.keys();
|
|
|
|
for (int i=0; i<len(keys); ++i)
|
2006-02-01 00:09:52 +01:00
|
|
|
{
|
2009-05-24 08:12:32 +02:00
|
|
|
std::string key = extract<std::string>(keys[i]);
|
|
|
|
object obj = d[key];
|
|
|
|
extract<std::string> ex0(obj);
|
2012-12-03 14:12:09 +01:00
|
|
|
extract<mapnik::value_integer> ex1(obj);
|
2009-05-24 08:12:32 +02:00
|
|
|
extract<double> ex2(obj);
|
2013-08-14 00:52:04 +02:00
|
|
|
extract<mapnik::value_unicode_string> ex3(obj);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2011-12-03 02:59:57 +01:00
|
|
|
// TODO - this is never hit - we need proper python string -> std::string to get invoked here
|
2009-05-24 08:12:32 +02:00
|
|
|
if (ex0.check())
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
p[key] = ex0();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
else if (ex1.check())
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
p[key] = ex1();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
else if (ex2.check())
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
p[key] = ex2();
|
2011-11-14 04:54:32 +01:00
|
|
|
}
|
2011-12-03 02:59:57 +01:00
|
|
|
else if (ex3.check())
|
|
|
|
{
|
|
|
|
std::string buffer;
|
|
|
|
mapnik::to_utf8(ex3(),buffer);
|
|
|
|
p[key] = buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 21:41:56 +02:00
|
|
|
MAPNIK_LOG_DEBUG(bindings) << "parameters_pickle_suite: Could not unpickle key=" << key;
|
2011-12-03 02:59:57 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
}
|
2006-02-01 00:09:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-03 02:59:57 +01:00
|
|
|
|
|
|
|
mapnik::value_holder get_params_by_key1(mapnik::parameters const& p, std::string const& key)
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2011-12-03 02:59:57 +01:00
|
|
|
parameters::const_iterator pos = p.find(key);
|
|
|
|
if (pos != p.end())
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2011-12-03 02:59:57 +01:00
|
|
|
// will be auto-converted to proper python type by `mapnik_params_to_python`
|
|
|
|
return pos->second;
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
2011-12-03 02:59:57 +01:00
|
|
|
return mapnik::value_null();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 02:59:57 +01:00
|
|
|
mapnik::value_holder get_params_by_key2(mapnik::parameters const& p, std::string const& key)
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2011-12-03 02:59:57 +01:00
|
|
|
parameters::const_iterator pos = p.find(key);
|
|
|
|
if (pos == p.end())
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2011-12-03 02:59:57 +01:00
|
|
|
PyErr_SetString(PyExc_KeyError, key.c_str());
|
|
|
|
boost::python::throw_error_already_set();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
2011-12-03 02:59:57 +01:00
|
|
|
// will be auto-converted to proper python type by `mapnik_params_to_python`
|
|
|
|
return pos->second;
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:55:18 +01:00
|
|
|
mapnik::parameter get_params_by_index(mapnik::parameters const& p, int index)
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2012-02-13 20:01:16 +01:00
|
|
|
if (index < 0 || static_cast<unsigned>(index) > p.size())
|
2011-12-03 02:59:57 +01:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_IndexError, "Index is out of range");
|
|
|
|
throw boost::python::error_already_set();
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters::const_iterator itr = p.begin();
|
2013-01-04 17:17:51 +01:00
|
|
|
std::advance(itr, index);
|
|
|
|
if (itr != p.end())
|
2011-12-03 02:59:57 +01:00
|
|
|
{
|
2013-01-04 17:55:18 +01:00
|
|
|
return *itr;
|
2011-12-03 02:59:57 +01:00
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_IndexError, "Index is out of range");
|
|
|
|
throw boost::python::error_already_set();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
|
2014-11-05 19:41:24 +01:00
|
|
|
unsigned get_params_size(mapnik::parameters const& p)
|
|
|
|
{
|
|
|
|
return p.size();
|
|
|
|
}
|
|
|
|
|
2011-12-03 02:59:57 +01:00
|
|
|
void add_parameter(mapnik::parameters & p, mapnik::parameter const& param)
|
2009-05-24 08:12:32 +02:00
|
|
|
{
|
2012-02-02 02:30:26 +01:00
|
|
|
p[param.first] = param.second;
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
2006-02-01 00:09:52 +01:00
|
|
|
|
2011-12-03 02:59:57 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<mapnik::parameter> create_parameter(mapnik::value_unicode_string const& key, mapnik::value_holder const& value)
|
2011-12-03 02:59:57 +01:00
|
|
|
{
|
2013-01-17 16:59:42 +01:00
|
|
|
std::string key_utf8;
|
|
|
|
mapnik::to_utf8(key, key_utf8);
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<mapnik::parameter>(key_utf8,value);
|
2011-12-03 02:59:57 +01:00
|
|
|
}
|
|
|
|
|
2013-01-17 16:17:46 +01:00
|
|
|
// needed for Python_Unicode to std::string (utf8) conversion
|
2013-01-17 16:59:42 +01:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<mapnik::parameter> create_parameter_from_string(mapnik::value_unicode_string const& key, mapnik::value_unicode_string const& ustr)
|
2013-01-17 16:17:46 +01:00
|
|
|
{
|
2013-01-17 16:59:42 +01:00
|
|
|
std::string key_utf8;
|
|
|
|
std::string ustr_utf8;
|
|
|
|
mapnik::to_utf8(key, key_utf8);
|
|
|
|
mapnik::to_utf8(ustr,ustr_utf8);
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<mapnik::parameter>(key_utf8, ustr_utf8);
|
2013-01-17 16:17:46 +01:00
|
|
|
}
|
2011-12-03 02:59:57 +01:00
|
|
|
|
2006-02-01 00:09:52 +01:00
|
|
|
void export_parameters()
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
2013-01-04 20:28:00 +01:00
|
|
|
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>();
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
class_<parameter,std::shared_ptr<parameter> >("Parameter",no_init)
|
2013-01-04 20:28:00 +01:00
|
|
|
.def("__init__", make_constructor(create_parameter),
|
2011-12-03 02:59:57 +01:00
|
|
|
"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")
|
2013-01-17 16:17:46 +01:00
|
|
|
.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())
|
2011-12-03 02:59:57 +01:00
|
|
|
.def("__getitem__",get_param)
|
2006-02-01 00:09:52 +01:00
|
|
|
;
|
|
|
|
|
2006-02-20 02:34:02 +01:00
|
|
|
class_<parameters>("Parameters",init<>())
|
2006-02-01 00:09:52 +01:00
|
|
|
.def_pickle(parameters_pickle_suite())
|
2011-12-03 02:59:57 +01:00
|
|
|
.def("get",get_params_by_key1)
|
|
|
|
.def("__getitem__",get_params_by_key2)
|
|
|
|
.def("__getitem__",get_params_by_index)
|
2014-11-05 19:41:24 +01:00
|
|
|
.def("__len__",get_params_size)
|
2011-12-03 02:59:57 +01:00
|
|
|
.def("append",add_parameter)
|
|
|
|
.def("iteritems",iterator<parameters>())
|
2006-02-01 00:09:52 +01:00
|
|
|
;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|