Mapnik Renderers

Mapnik supports rendering with both AGG and Cairo. See OutputFormats for detailed comparisons of different AGG and Cairo formats.

Anti-Grain Geometry

The AGG renderer (Antigrain Geometry) is the primary renderer in Mapnik.

While Mapnik was the first to use AGG rendering for mapping, the AGG renderer is also now an optional rendering engine in the MapServer and MapGuide projects.

Due to licensing issues much discussion exists in the MapServer and MapGuide communities about the now GPL-licensed AGG:

Cairographics

The Cairo renderer is an auxiliary renderer in Mapnik.

  • Cairo was added in r656 due to its similar reputation for high quality graphics output
  • Cairo has the added advantage of supporting both Vector and Raster output.
  • Cairo may have more support for sophisticated anti-aliasing / pixel sampling (?)
    • Mapnik can render to any surface supported by cairo, either directly or by rendering to a cairo context.
    • You can demo the PNG, JPEG, SVG, PDF, and PS formats using the OSM export tool
  • Cairo is optional during Mapnik Scons build process and is enabled automatically with a call to pkg-config.
    • Pkg-config must find libcairo as well as Cairomm(C++ bindings) and Pycairo (python bindings)
    • If Pkg-config is successful you will see the added compiler flags
      -DHAVE_CAIRO -DHAVE_PYCAIRO
      

Installation of Cairo

Note: requires Mapnik svn trunk checkout (post r726):

Before rebuiling Mapnik:

On Ubuntu, use apt-get:

# apt-get install libcairo2 libcairo2-dev python-cairo python-cairo-dev libcairomm-1.0-1 libcairomm-1.0-dev

On Mac, use macports:

# port install cairo cairomm py-cairo

Python Example Code

Writing to SVG with Mapnik's Cairo renderer:

import mapnik
import cairo

mapfile = 'mapfile.xml'
map_output = 'mapfile.svg'
projection = '+proj=latlong +datum=WGS84'

mapnik_map = mapnik.Map(1000, 500)
mapnik.load_map(mapnik_map, mapfile)
bbox = mapnik.Envelope(-180.0,-90.0,180.0,90.0)
mapnik_map.zoom_to_box(bbox)
file = open(map_output, 'w')
surface = cairo.SVGSurface(file.name, mapnik_map.width, mapnik_map.height)
mapnik.render(mapnik_map, surface)
surface.finish()

or PDF:

import mapnik
import cairo

mapfile = 'mapfile.xml'
map_output = 'mapfile.pdf'
projection = '+proj=latlong +datum=WGS84'

mapnik_map = mapnik.Map(1000, 500)
mapnik.load_map(mapnik_map, mapfile)
bbox = mapnik.Envelope(-180.0,-90.0,180.0,90.0)
mapnik_map.zoom_to_box(bbox)
file = open(map_output, 'wb')
surface = cairo.PDFSurface(file.name, mapnik_map.width, mapnik_map.height)
mapnik.render(mapnik_map, surface)
surface.finish()
  • Note: Cairo can also write to PostScript? and other image formats
  • Note: 'mapnik.render()' can also render to Cairo Contexts

Further References