mapnik/bindings/python/mapnik_parameters.cpp

225 lines
6.9 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
*
* 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
// boost
2006-02-01 00:09:52 +01:00
#include <boost/python.hpp>
#include <boost/make_shared.hpp>
// mapnik
#include <mapnik/params.hpp>
#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;
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<int> ex1(obj);
extract<double> ex2(obj);
extract<UnicodeString> 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
{
std::clog << "could not unpickle key: " << key << "\n";
}
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 || index > p.size())
{
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)
{
if (idx == index)
{
return *itr;
}
++idx;
++itr;
}
PyErr_SetString(PyExc_IndexError, "Index is out of range");
throw boost::python::error_already_set();
}
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();
}
}
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;
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())
.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__",&parameters::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
}