Joel Brown edited this page Aug 19, 2014 · 5 revisions

Mapnik's PluginArchitecture supports the use of different input formats.

This plugin supports the OGR library in order to read multiple spatial vector formats.


Make sure that running python scons/ DEBUG=y shows the following line

Checking for C library gdal... yes
Checking for name of ogr library... gdal

To check if the ogr plugin built and was installed correctly, try the usual Python from mapnik import * on a DEBUG=y build, and look for the following debug line

registered datasource : ogr


parameter value description default
file string file to display
base string base path where to search for file parameter
layer string name of the layer to display (a single ogr datasource can contain multiple layers)
layer_by_index integer index of the layer to display, this becomes mandatory if no "layer" parameter is specified
layer_by_sql string SQL statement to execute against the OGR-datasource. The result set is used as the layer definition.
multiple_geometries boolean whether to use multiple different objects or a single one when dealing with multi-objects (this is mainly related to how the label are used in the map, one label for a multi-polygon or one label for each polygon of a multi-polygon) false
encoding string internal file encoding utf-8
string string optional (replaces file parameter) string of literal OGR-datasource data, like GeoJSON
extent string maximum extent of the layer. should be provided when an extent cannot be automatically determined by OGR


Note: The layer names of OGR datasources are returned by Mapnik in the error message when you do not provide the layer parameter.

    >>> import mapnik
    >>> mapnik.Ogr(file='test_point_line.gpx')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Python/2.5/site-packages/mapnik/", line 295, in Ogr
        return CreateDatasource(keywords)
    RuntimeError: missing <layer> parameter, available layers are:  'waypoints'  'routes'  'tracks'  'route_points'  'track_points'
    >>> mapnik.Ogr(file='test_point_line.gpx',layer='waypoints')
    <mapnik.Datasource object at 0x23f6b0> # works!

However the best way to discover the layer names is to use the OGR provided utility called ogrinfo. For example running ogrinfo on a test GPX files provided with the OGR source code reveals the layer names and geometry types:

    $ ogrinfo test_point_line.gpx
    Had to open data source read-only.
    INFO: Open of `test_point_line.gpx'
          using driver `GPX' successful.
    1: waypoints (Point)
    2: routes (Line String)
    3: tracks (Multi Line String)
    4: route_points (Point)
    5: track_points (Point)


        <Layer name="gps_waypoints">
                <Parameter name="type">ogr</Parameter>
                <Parameter name="file">test_point_line.gpx</Parameter>
               <Parameter name="layer">waypoints</Parameter>
        <Layer name="states">
                <Parameter name="type">ogr</Parameter>
                <Parameter name="base">tests/data</Parameter>
                <Parameter name="file">us_states.shp</Parameter>
                <!-- OGR supports formats with multiple layers and while shapefiles
                only have one, we still need to specify it by name -->
               <Parameter name="layer">us_states</Parameter>
    <Layer name="mssql" srs="+proj=latlong +datum=WGS84">
            <!-- mssql database must contain geometry_columns and spatial_ref_sys metadata tables-->
            <Parameter name="type">ogr</Parameter>
            <Parameter name="string">MSSQL:server=localhost;database=gis;trusted_connection=yes</Parameter>
            <Parameter name="layer_by_sql">SELECT * FROM dbo.planet_osm_line</Parameter>
            <Parameter name="extent">-180,-90,180,89.99</Parameter>


Plugin datasource initialization example code can be found on PluginArchitecture.

A OGR datasource may be created as follows:

        parameters p;
        // Bridges
        Layer lyr("Bridges");

Further References

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.