File: mapnik_parameters.cpp

package info (click to toggle)
mapnik 2.2.0%2Bds1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,288 kB
  • ctags: 18,382
  • sloc: cpp: 115,128; python: 9,298; xml: 5,692; ansic: 3,726; makefile: 160; sh: 159; lisp: 13
file content (225 lines) | stat: -rw-r--r-- 7,135 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 Artem Pavlenko
 *
 * 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
 *
 *****************************************************************************/

// boost
#include <boost/python.hpp>
#include <boost/make_shared.hpp>

// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/params.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/value.hpp>
// stl
#include <iterator>

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);
    }
};

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;
            ++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,
                            ("expected 1-item tuple in call to __setstate__; got %s"
                             % state).ptr()
                );
            throw_error_already_set();
        }

        dict d = extract<dict>(state[0]);
        boost::python::list keys = d.keys();
        for (int i=0; i<len(keys); ++i)
        {
            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<UnicodeString> ex3(obj);

            // TODO - this is never hit - we need proper python string -> std::string to get invoked here
            if (ex0.check())
            {
                p[key] = ex0();
            }
            else if (ex1.check())
            {
                p[key] = ex1();
            }
            else if (ex2.check())
            {
                p[key] = ex2();
            }
            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;
            }
        }
    }
};


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();
}

void add_parameter(mapnik::parameters & p, mapnik::parameter const& param)
{
    p[param.first] = param.second;
}

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(UnicodeString const& key, mapnik::value_holder const& value)
{
    std::string key_utf8;
    mapnik::to_utf8(key, key_utf8);
    return boost::make_shared<mapnik::parameter>(key_utf8,value);
}

// needed for Python_Unicode to std::string (utf8) conversion

boost::shared_ptr<mapnik::parameter> create_parameter_from_string(UnicodeString const& key, UnicodeString const& ustr)
{
    std::string key_utf8;
    std::string ustr_utf8;
    mapnik::to_utf8(key, key_utf8);
    mapnik::to_utf8(ustr,ustr_utf8);
    return boost::make_shared<mapnik::parameter>(key_utf8, ustr_utf8);
}

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,boost::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")

        .def_pickle(parameter_pickle_suite())
        .def("__getitem__",get_param)
        ;

    class_<parameters>("Parameters",init<>())
        .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>())
        ;
}