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
|
|
|
|
2009-05-24 08:12:32 +02:00
|
|
|
// boost
|
2006-02-01 00:09:52 +01:00
|
|
|
#include <boost/python.hpp>
|
2011-12-03 02:59:57 +01:00
|
|
|
#include <boost/make_shared.hpp>
|
2009-05-24 08:12:32 +02:00
|
|
|
|
|
|
|
// mapnik
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/params.hpp>
|
2011-12-03 02:59:57 +01:00
|
|
|
#include <mapnik/unicode.hpp>
|
|
|
|
#include <mapnik/value.hpp>
|
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);
|
|
|
|
extract<int> ex1(obj);
|
|
|
|
extract<double> ex2(obj);
|
2011-12-03 02:59:57 +01:00
|
|
|
extract<UnicodeString> 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
|
|
|
|
{
|
|
|
|
std::clog << "could not unpickle key: " << key << "\n";
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-12-03 02:59:57 +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();
|
|
|
|
parameters::const_iterator end = p.end();
|
|
|
|
|
|
|
|
unsigned idx = 0;
|
2012-01-09 17:38:46 +01:00
|
|
|
while (itr != end)
|
2011-12-03 02:59:57 +01:00
|
|
|
{
|
|
|
|
if (idx == index)
|
|
|
|
{
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_IndexError, "Index is out of range");
|
|
|
|
throw boost::python::error_already_set();
|
2009-05-24 08:12:32 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<mapnik::parameter> create_parameter_from_string(std::string const& key, std::string const& value)
|
|
|
|
{
|
|
|
|
return boost::make_shared<mapnik::parameter>(key,mapnik::value_holder(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<mapnik::parameter> create_parameter_from_int(std::string const& key, int value)
|
|
|
|
{
|
|
|
|
return boost::make_shared<mapnik::parameter>(key,mapnik::value_holder(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<mapnik::parameter> create_parameter_from_float(std::string const& key, double value)
|
|
|
|
{
|
|
|
|
return boost::make_shared<mapnik::parameter>(key,mapnik::value_holder(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-01 00:09:52 +01:00
|
|
|
void export_parameters()
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
2011-12-03 02:59:57 +01:00
|
|
|
class_<parameter,boost::shared_ptr<parameter> >("Parameter",no_init)
|
|
|
|
.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")
|
|
|
|
.def("__init__", make_constructor(create_parameter_from_int),
|
|
|
|
"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_float),
|
|
|
|
"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)
|
|
|
|
.def("__len__",¶meters::size)
|
|
|
|
.def("append",add_parameter)
|
|
|
|
.def("iteritems",iterator<parameters>())
|
2006-02-01 00:09:52 +01:00
|
|
|
;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|