mapnik/plugins/input/python/python_datasource.hpp

57 lines
1.7 KiB
C++
Raw Normal View History

python: a new plugin to use arbitrary Python as a data source This plugin allows you to write data sources in the Python programming language. This is useful if you want to rapidly prototype a plugin, perform some custom manipulation on data or if you want to bind mapnik to a datasource which is most conveniently accessed through Python. The plugin may be used from the existing mapnik Python bindings or it can embed the Python interpreter directly allowing it to be used from C++, XML or even JavaScript. Mapnik already has excellent Python bindings but they only directly support calling *into* mapnik *from* Python. This forces mapnik and its input plugins to be the lowest layer of the stack. The role of this plugin is to allow mapnik to call *into* Python itself. This allows mapnik to sit as rendering middleware between a custom Python frontend and a custom Python datasource. This increases the utility of mapnik as a component in a larger system. There already exists MemoryDatasource which can be used to dynamically create geometry in Python. It suffers from the problem that it does not allow generating only the geometry which is seen by a particular query. Similarly the entire geometry must exist in memory before rendering can progress. By using a custom iterator object or by using generator expressions this plugin allows geometry to be created on demand and to be destroyed after use. This can have a great impact on memory efficiency. Since geometry is generated on-demand as rendering progresses there can be arbitrarily complex 'cleverness' optimising the geometry generated for a particular query. Obvious examples of this would be generating only geometry within the query bounding box and generating geometry with an appropriate level of detail for the output resolution.
2012-07-19 20:06:44 +02:00
#ifndef PYTHON_DATASOURCE_HPP
#define PYTHON_DATASOURCE_HPP
// mapnik
#include <mapnik/datasource.hpp>
// boost
#include <boost/python.hpp>
class python_datasource : public mapnik::datasource
{
public:
// constructor
// arguments must not change
python_datasource(mapnik::parameters const& params, bool bind=true);
// destructor
virtual ~python_datasource ();
// mandatory: type of the plugin, used to match at runtime
mapnik::datasource::datasource_t type() const;
// mandatory: name of the plugin
static const char* name();
// mandatory: function to query features by box2d
// this is called when rendering, specifically in feature_style_processor.hpp
mapnik::featureset_ptr features(mapnik::query const& q) const;
// mandatory: function to query features by point (coord2d)
// not used by rendering, but available to calling applications
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
// mandatory: return the box2d of the datasource
// called during rendering to determine if the layer should be processed
mapnik::box2d<double> envelope() const;
// mandatory: optionally return the overal geometry type of the datasource
boost::optional<mapnik::datasource::geometry_t> get_geometry_type() const;
// mandatory: return the layer descriptor
mapnik::layer_descriptor get_descriptor() const;
// mandatory: will bind the datasource given params
void bind() const;
private:
static const char* name_;
mutable mapnik::layer_descriptor desc_;
const std::string factory_;
std::map<std::string, std::string> kwargs_;
mutable boost::python::object datasource_;
};
#endif // PYTHON_DATASOURCE_HPP