Changeset 913

Show
Ignore:
Timestamp:
02/15/09 20:53:34 (18 months ago)
Author:
dane
Message:

+ add pickling interface to layers and datasources (closes #205) - TODO support parameters

Location:
trunk/bindings/python
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/bindings/python/mapnik_datasource.cpp

    r790 r913  
    3232#include <mapnik/feature_layer_desc.hpp> 
    3333#include <mapnik/memory_datasource.hpp> 
     34 
     35 
     36using mapnik::datasource; 
     37using mapnik::point_datasource; 
     38 
     39struct ds_pickle_suite : boost::python::pickle_suite 
     40{ 
     41   static boost::python::tuple 
     42   getinitargs(const datasource& ds) 
     43   { 
     44      mapnik::parameters params = ds.params(); 
     45      return boost::python::make_tuple(params); 
     46   } 
     47}; 
    3448 
    3549namespace   
     
    8498{ 
    8599    using namespace boost::python; 
    86     using mapnik::datasource; 
    87     using mapnik::point_datasource; 
    88          
     100     
    89101    class_<datasource,boost::shared_ptr<datasource>, 
    90102        boost::noncopyable>("Datasource",no_init) 
     103        .def_pickle(ds_pickle_suite() 
     104                ) 
    91105        .def("envelope",&datasource::envelope) 
    92106        .def("descriptor",&datasource::get_descriptor) //todo 
  • trunk/bindings/python/mapnik_layer.cpp

    r776 r913  
    2323 
    2424 
     25// boost 
    2526#include <boost/python.hpp> 
    2627#include <boost/python/detail/api_placeholder.hpp> 
    2728#include <boost/python/suite/indexing/vector_indexing_suite.hpp> 
     29 
     30// mapnik 
    2831#include <mapnik/layer.hpp> 
     32#include <mapnik/datasource.hpp> 
    2933 
    3034using mapnik::Layer; 
    3135using mapnik::parameters; 
     36using mapnik::datasource; 
     37 
     38 
     39struct layer_pickle_suite : boost::python::pickle_suite 
     40{ 
     41   static boost::python::tuple 
     42   getinitargs(const Layer& l) 
     43   { 
     44      return boost::python::make_tuple(l.name(),l.srs()); 
     45   } 
     46 
     47   static  boost::python::tuple 
     48   getstate(const Layer& l) 
     49   { 
     50        boost::python::list s; 
     51        std::vector<std::string> const& style_names = l.styles(); 
     52        for (unsigned i = 0; i < style_names.size(); ++i) 
     53        { 
     54            s.append(style_names[i]); 
     55        }       
     56        return boost::python::make_tuple(l.abstract(),l.title(),l.clear_label_cache(),l.getMinZoom(),l.getMaxZoom(),l.isQueryable(),l.datasource(),s); 
     57   } 
     58 
     59   static void 
     60   setstate (Layer& l, boost::python::tuple state) 
     61   { 
     62        using namespace boost::python; 
     63        if (len(state) != 8) 
     64        { 
     65         PyErr_SetObject(PyExc_ValueError, 
     66                         ("expected 8-item tuple in call to __setstate__; got %s" 
     67                          % state).ptr() 
     68            ); 
     69         throw_error_already_set(); 
     70        } 
     71 
     72        if (state[0]) 
     73        { 
     74            l.set_abstract(extract<std::string>(state[0])); 
     75        } 
     76 
     77        if (state[1]) 
     78        { 
     79            l.set_title(extract<std::string>(state[1])); 
     80        } 
     81 
     82        if (state[2]) 
     83        { 
     84            l.set_clear_label_cache(extract<bool>(state[2])); 
     85        } 
     86 
     87        if (state[3]) 
     88        { 
     89            l.setMinZoom(extract<double>(state[3])); 
     90        } 
     91 
     92        if (state[4]) 
     93        { 
     94            l.setMaxZoom(extract<double>(state[4])); 
     95        } 
     96 
     97        if (state[5]) 
     98        { 
     99            l.setQueryable(extract<bool>(state[5])); 
     100        } 
     101 
     102        if (state[6]) 
     103        { 
     104            boost::shared_ptr<datasource> ds = extract<boost::shared_ptr<datasource> >(state[6]); 
     105            l.set_datasource(ds); 
     106        } 
     107         
     108        boost::python::list s = extract<boost::python::list>(state[7]); 
     109        for (int i=0;i<len(s);++i) 
     110        { 
     111           l.add_style(extract<std::string>(s[i])); 
     112        } 
     113   } 
     114}; 
    32115 
    33116std::vector<std::string> & (mapnik::Layer::*_styles_)() = &mapnik::Layer::styles; 
     
    52135                )) 
    53136 
     137        .def_pickle(layer_pickle_suite() 
     138                ) 
     139          
    54140        .def("envelope",&Layer::envelope,  
    55141                "Return the geographic envelope/bounding box "