Overview
Mapnik supports a basic Plugin architecture.
If you take a look in the plugins/ directory you'll see a set of (currently) five input/ plugins:
GDAL/ support for GDAL datasets OSM/ support for reading OpenStreetMap? (OSM) datasets PostGIS/ support for accessing geospatial data through PostGIS, the spatial database extension for PostgreSQL Raster/ raster TIFF image dataset support Shape/ vector SHP (ESRI Shapefile) parsing
Note that when you compile Mapnik using SCons, you probably won't be compiling all these plugins. That's okay -- after all, these are plugins!
Querying plugins
Hopefully, you compiled Mapnik with DEBUG=y, like so.
$ python scons/scons.py DEBUG=y
Then, when you run python and try
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) >>> from mapnik import *
you'll get a response, more or less, like so
registered datasource : gdal registered datasource : raster registered datasource : shape
These are the registered datasource plugins that Mapnik's python binding currently knows about.
C++
Plugins can be used from C++ to read in different kinds of files. Here's an example of C++ code that implicitly uses the Shape plugin to read in a geospatial SHP file.
Plugins may be initialized like so:
std::string mapnik_dir(argv[1]); // assume mapnik home directory, such as "~/src/mapnik" passed in std::string plugins_dir(mapnik_dir + "/plugins/input/"); datasource_cache::instance()->register_datasources(plugins_dir + "shape"); // ESRI SHP support datasource_cache::instance()->register_datasources(plugins_dir + "postgis"); // PostGIS integration
and used like so,
{ parameters p; p["type"]="shape"; p["file"]="../data/statesp020"; // State Boundaries of the United States [SHP] Layer lyr("Cali"); lyr.set_datasource(datasource_cache::instance()->create(p)); // Note use of datasource_cache factory method here! lyr.add_style("cali"); // style.xml lyr.add_style("elsewhere"); // this file m.addLayer(lyr); }
Let's drill-down into what's actually going on. We constructed a parameter object and passed it to a factory method (datasource_cache::instance()->create(p)). It then returned a shared pointer to a datasource object, which was then passed on a new Layer object.
In C++, the parameters object is-a param_map (a std::map from string keys to "value_holder"s, where a value_holder is a boost::variant that can either hold a double or a string. In other words, "parameters p" is a semi-generic parameter hash, then passed to and read by a factory method.
To specify the kind of datasource, note that we specified p's "type" as "shape". Other options might be "osm", "postgis", or "raster".
PostGIS
Mapnik supports the rendering of geodata stored in a geospatial database such as PostGIS (an extension to PostgreSQL).
See the Mapnik wiki page on PostGIS for more details on how to use the PostGIS input plugin.
