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.
- The AGG subpixel anti-aliasing capabilities are the standout reason for the beauty of Mapnik output.
- http://www.antigrain.com/demo/aa_demo.gif (on the other hand, AGG only supports one kind of AA / sampling method)
- Anti-Aliasing and Subpixel Rendering on Wikipedia
- The AGG renderer supports high quality output of map graphics to both PNG, PNG256, and JPEG.
- Version 2.3 of the AGG C++ library is included/embedded within the source tree of Mapnik and compiled automatically during the Scons process.
- Work is underway to allow Mapnik to build against a system version of AGG (r724).
- Use the INTERNAL_LIBAGG option as a scons flag:
INTERNAL_LIBAGG=False
- The need for this option is due in part to concerns about packaging.
- Use the INTERNAL_LIBAGG option as a scons flag:
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:
- http://www.nabble.com/MapGuide-fork-of-AGG-tt13321313.html#a13406886
- http://www.nabble.com/Re%3A-MapGuide-fork-of-AGG-p13359220.html
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
