File: mapnik_feature.cpp

package info (click to toggle)
mapnik 0.5.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,136 kB
  • ctags: 14,550
  • sloc: cpp: 68,887; python: 24,895; xml: 1,534; makefile: 503; sh: 79
file content (210 lines) | stat: -rw-r--r-- 6,803 bytes parent folder | download
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
/*****************************************************************************
 * 
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon
 *
 * 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
 *
 *****************************************************************************/
//$Id$

// boost
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/python/iterator.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python.hpp>
// mapnik
#include <mapnik/feature.hpp>

namespace boost { namespace python {
      struct value_converter : public boost::static_visitor<PyObject*>
      {
            PyObject * operator() (int val) const
            {
               return ::PyInt_FromLong(val);
            }
            
            PyObject * operator() (double val) const
            {
               return ::PyFloat_FromDouble(val);
            }
            
            PyObject * operator() (std::wstring const& s) const
            {
               return ::PyUnicode_FromWideChar(s.data(),implicit_cast<ssize_t>(s.size()));
            }
      };
      
      struct mapnik_value_to_python
      {
            static PyObject* convert(mapnik::value const& v)
            {
               return boost::apply_visitor(value_converter(),v.base());
            }
      };
      
      // Forward declaration
      template <class Container, bool NoProxy, class DerivedPolicies>
      class map_indexing_suite2;

      namespace detail
      {
         template <class Container, bool NoProxy>
         class final_map_derived_policies
            : public map_indexing_suite2<Container,
                                         NoProxy, final_map_derived_policies<Container, NoProxy> > {};
      }
    
      template <
         class Container,
         bool NoProxy = false,
         class DerivedPolicies
         = detail::final_map_derived_policies<Container, NoProxy> >
      class map_indexing_suite2
         : public indexing_suite<
         Container
         , DerivedPolicies
         , NoProxy
         , true
         , typename Container::value_type::second_type
         , typename Container::key_type
         , typename Container::key_type
         >
      {
         public:

            typedef typename Container::value_type value_type;
            typedef typename Container::value_type::second_type data_type;
            typedef typename Container::key_type key_type;
            typedef typename Container::key_type index_type;
            typedef typename Container::size_type size_type;
            typedef typename Container::difference_type difference_type;

            template <class Class>
            static void
            extension_def(Class& cl)
            {
               
            }

            static data_type&
            get_item(Container& container, index_type i_)
            {
               typename Container::iterator i = container.find(i_);
               if (i == container.end())
               {
                  PyErr_SetString(PyExc_KeyError, "Invalid key");
                  throw_error_already_set();
               }
               return i->second;
            }
            
            static void
            set_item(Container& container, index_type i, data_type const& v)
            {
               container[i] = v;
            }
            
            static void
            delete_item(Container& container, index_type i)
            {
               container.erase(i);
            }
            
            static size_t
            size(Container& container)
            {
               return container.size();
            }
            
            static bool
            contains(Container& container, key_type const& key)
            {
               return container.find(key) != container.end();
            }
            
            static bool
            compare_index(Container& container, index_type a, index_type b)
            {
               return container.key_comp()(a, b);
            }
            
            static index_type
            convert_index(Container& /*container*/, PyObject* i_)
            {
               extract<key_type const&> i(i_);
               if (i.check())
               {
                  return i();
               }
               else
               {
                  extract<key_type> i(i_);
                  if (i.check())
                     return i();
               }
               
               PyErr_SetString(PyExc_TypeError, "Invalid index type");
               throw_error_already_set();
               return index_type();
            }
      };
      

      template <typename T1, typename T2>
      struct std_pair_to_tuple
      {
            static PyObject* convert(std::pair<T1, T2> const& p)
            {
               return boost::python::incref(
                  boost::python::make_tuple(p.first, p.second).ptr());
            }
      };
      
      template <typename T1, typename T2>
      struct std_pair_to_python_converter
      {
            std_pair_to_python_converter()
            {
               boost::python::to_python_converter<
               std::pair<T1, T2>,
                  std_pair_to_tuple<T1, T2> >();
            }
      };
   }
}

void export_feature()
{
   using namespace boost::python;
   using mapnik::Feature;
   
   std_pair_to_python_converter<std::string const,mapnik::value>();
   to_python_converter<mapnik::value,mapnik_value_to_python>();
   class_<Feature,boost::shared_ptr<Feature>,
      boost::noncopyable>("Feature",no_init)
      .def("id",&Feature::id)
      .def("__str__",&Feature::to_string)
      .add_property("properties", 
                    make_function(&Feature::props,return_value_policy<reference_existing_object>()))
      ;

   class_<std::map<std::string, mapnik::value> >("Properties")
      .def(map_indexing_suite2<std::map<std::string, mapnik::value>, true >())
      .def("iteritems",iterator<std::map<std::string,mapnik::value> > ())
      ;
}