Ticket #113: python_point_datasource.patch

File python_point_datasource.patch, 9.4 kB (added by lwu, 18 months ago)

Support for python-based point datasources

  • include/mapnik/memory_featureset.hpp

     
    4141         
    4242        feature_ptr next() 
    4343        { 
    44            /* 
    4544            while (pos_ != end_) 
    4645            { 
    47                 geometry_ptr geom = (*pos_)->get_geometry(); 
    48                 if (geom && bbox_.intersects(geom->envelope())) 
    49                 { 
    50                     return *pos_++; 
     46                for  (unsigned i=0; i<(*pos_)->num_geometries();++i) { 
     47                    geometry2d & geom = (*pos_)->get_geometry(i); 
     48                    if (bbox_.intersects(geom.envelope())) 
     49                    { 
     50                        return *pos_++; 
     51                    } 
    5152                } 
    5253                ++pos_; 
    5354            } 
    54            */ 
     55            
    5556            return feature_ptr(); 
    5657        } 
    5758         
  • include/mapnik/memory_datasource.hpp

     
    2626#define MEMORY_DATASOURCE_HPP 
    2727 
    2828#include <mapnik/datasource.hpp> 
     29#include <mapnik/feature_factory.hpp> // TODO remove 
    2930#include <vector> 
    3031 
    3132namespace mapnik { 
     
    4647    private: 
    4748        std::vector<mapnik::feature_ptr> features_; 
    4849    };  
     50 
     51    // This class implements a simple way of displaying point-based data 
     52    // TODO -- possible redesign, move into separate file 
     53    // 
     54    class point_datasource : public mapnik::memory_datasource { 
     55    public: 
     56            point_datasource() : feat_id(0) {} 
     57            void add_point(double x, double y, const char* key, const char* value) { 
     58                        mapnik::feature_ptr feature(mapnik::feature_factory::create(feat_id++)); 
     59                        mapnik::geometry2d * pt = new mapnik::point_impl; 
     60                        pt->move_to(x,y); 
     61                        feature->add_geometry(pt); 
     62                        mapnik::transcoder tr("utf-8"); 
     63                        (*feature)[key] = tr.transcode(value); 
     64                        this->push(feature); 
     65        } 
     66        int type() const { return mapnik::datasource::Raster; } 
     67 
     68    private: 
     69            int feat_id; 
     70    }; 
     71 
    4972} 
    5073 
    5174#endif // MEMORY_DATASOURCE_HPP 
  • demo/c++/rundemo.cpp

     
    4848    try { 
    4949        std::cout << " running demo ... \n"; 
    5050        std::string mapnik_dir(argv[1]); 
    51         datasource_cache::instance()->register_datasources(mapnik_dir + "/lib/mapnik/input/");  
    52         freetype_engine::register_font(mapnik_dir + "/lib/mapnik/fonts/DejaVuSans.ttf"); 
     51        datasource_cache::instance()->register_datasources(mapnik_dir + "/plugins/input/shape");  
     52        freetype_engine::register_font(mapnik_dir + "/fonts/dejavu-ttf-2.14/DejaVuSans.ttf"); 
    5353         
    5454        Map m(800,600); 
    5555        m.set_background(color_factory::from_string("white")); 
  • bindings/python/mapnik_query.cpp

     
    2323 
    2424#include <boost/python.hpp> 
    2525#include <mapnik/query.hpp> 
     26#include <mapnik/envelope.hpp> 
    2627 
    2728void export_query() 
    2829{ 
     30    using namespace boost::python; 
     31 
    2932    using mapnik::query; 
    30     //class_<query>("Query",init< 
     33    using mapnik::Envelope; 
     34 
     35    class_<query>("Query", "a spatial query data object",  
     36                  init<Envelope<double>,double>() ) 
     37        .add_property("resolution", &query::resolution) 
     38        .add_property("bbox", make_function(&query::get_bbox, 
     39                                            return_value_policy<copy_const_reference>()) ) 
     40        .add_property("property_names", make_function(&query::property_names, 
     41                                                      return_value_policy<copy_const_reference>()) ) 
     42        .def("add_property_name", &query::add_property_name); 
    3143} 
    3244 
    3345 
  • bindings/python/mapnik_featureset.cpp

     
    3333     
    3434    inline mapnik::feature_ptr next(mapnik::featureset_ptr const& itr) 
    3535    { 
    36         mapnik::feature_ptr f = itr->next(); 
    37         if (!f) 
     36        if (!itr) 
    3837        { 
    3938            PyErr_SetString(PyExc_StopIteration, "No more features."); 
    4039            boost::python::throw_error_already_set(); 
    4140        } 
    42         return f;  
     41 
     42        return itr->next(); 
    4343    }     
    4444} 
    4545 
  • bindings/python/mapnik_python.cpp

     
    3333void export_parameters(); 
    3434void export_envelope(); 
    3535void export_query(); 
     36void export_geometry(); 
    3637void export_image(); 
    3738void export_image_view(); 
    3839void export_map(); 
     
    151152 
    152153    register_exception_translator<mapnik::config_error>(translator); 
    153154    register_cairo(); 
    154     export_query();     
     155    export_query(); 
     156    export_geometry(); 
    155157    export_feature(); 
    156158    export_featureset(); 
    157159    export_datasource(); 
  • bindings/python/mapnik_datasource.cpp

     
    3030#include <mapnik/datasource.hpp> 
    3131#include <mapnik/datasource_cache.hpp> 
    3232#include <mapnik/feature_layer_desc.hpp> 
     33#include <mapnik/memory_datasource.hpp> 
    3334 
    3435namespace   
    3536{ 
     
    8384{ 
    8485    using namespace boost::python; 
    8586    using mapnik::datasource; 
     87    using mapnik::point_datasource; 
    8688         
    8789    class_<datasource,boost::shared_ptr<datasource>, 
    8890        boost::noncopyable>("Datasource",no_init) 
     
    9799     
    98100    def("Describe",&describe); 
    99101    def("CreateDatasource",&create_datasource); 
     102 
     103    class_<point_datasource, bases<datasource>, boost::noncopyable>("PointDatasource", init<>()) 
     104        .def("add_point",&point_datasource::add_point) 
     105        ; 
    100106} 
  • bindings/python/mapnik_geometry.cpp

     
     1/***************************************************************************** 
     2 *  
     3 * This file is part of Mapnik (c++ mapping toolkit) 
     4 * 
     5 * This library is free software; you can redistribute it and/or 
     6 * modify it under the terms of the GNU Lesser General Public 
     7 * License as published by the Free Software Foundation; either 
     8 * version 2.1 of the License, or (at your option) any later version. 
     9 * 
     10 * This library is distributed in the hope that it will be useful, 
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     13 * Lesser General Public License for more details. 
     14 * 
     15 * You should have received a copy of the GNU Lesser General Public 
     16 * License along with this library; if not, write to the Free Software 
     17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
     18 * 
     19 *****************************************************************************/ 
     20//$Id$ 
     21 
     22// boost 
     23#include <boost/python.hpp> 
     24#include <boost/python/def.hpp> 
     25 
     26// mapnik 
     27#include <mapnik/geometry.hpp> 
     28 
     29void export_geometry() 
     30{ 
     31   using namespace boost::python; 
     32   using mapnik::geometry2d; 
     33    
     34   class_<geometry2d, boost::noncopyable>("Geometry2d",no_init) 
     35      .def("envelope",&geometry2d::envelope) 
     36       // .def("__str__",&geometry2d::to_string) 
     37       .def("type",&geometry2d::type) 
     38       // TODO add other geometry2d methods 
     39      ; 
     40} 
  • bindings/python/mapnik_filter.cpp

     
    4747    class_<filter<Feature>,boost::noncopyable>("Filter", 
    4848                                               "An expression which allows " 
    4949                                               "to select features.",no_init) 
     50       .def("passes", &filter<Feature>::pass) // note: "pass" is a reserved word in Python 
    5051       .def("__str__",&filter<Feature>::to_string); 
    5152    ; 
    5253     
  • bindings/python/mapnik_feature.cpp

     
    3030// mapnik 
    3131#include <mapnik/feature.hpp> 
    3232 
     33mapnik::geometry2d & (mapnik::Feature::*get_geom1)(unsigned) = &mapnik::Feature::get_geometry; 
     34 
    3335namespace boost { namespace python { 
    3436      struct value_converter : public boost::static_visitor<PyObject*> 
    3537      { 
     
    206208      .def("__str__",&Feature::to_string) 
    207209      .add_property("properties",  
    208210                    make_function(&Feature::props,return_value_policy<reference_existing_object>())) 
     211//      .def("add_geometry", // TODO define more mapnik::Feature methods 
     212      .def("num_geometries",&Feature::num_geometries) 
     213      .def("get_geometry", make_function(get_geom1,return_value_policy<reference_existing_object>())) 
    209214      ; 
    210215 
    211216   class_<std::map<std::string, mapnik::value> >("Properties")