Mapnik Maps on the Command-Line

Nik2img is a third-party program that allows you to easily generate mapnik graphics from Mapnik xml or Cascadenik mml.

  • No python coding is required, only a working Mapnik installation and an XML mapfile.
  • Nik2img has a variety of options that make it a useful mapfile debugger.
  • Using Nik2img is an easy way to test and develop your xml mapfiles before launching on a server.
  • Nik2img should automatically open the rendered map once completed.
  • Like Mapnik, it should run on both Mac, Linux, and Windows

To Install:


Nik2img output using cascadenik osm style.mml

If you want to render your XML mapfile just do:

$ nik2img.py -m your_mapfile.xml -o your_map_rendered.png
  • Note: The nik2img.py default output is a 600,300 pixel png, rendered at the maximum extent of all your layers: Use the 's','-i', and '-e' flags to set custom map size, format, and bbox extents, respectively.

And say you want to see your map reprojected ('-p') to Google Spherical Mercator, use the url (or an epsg:code):

$ nik2img.py -m map.xml -o map.png -p http://spatialreference.org/ref/user/6/
  • Note: the above projection can also be called with '-p epsg:900913'

Or you want to test all the output formats ('-i all') of Mapnik's AGG and Cairo renderers and see Verbose debugging output ('-v'):

$ nik2img.py -m map.xml -o maps -i all -v

Or you want to run in 'Dry Run' mode('-n') and output no maps, but step through each part of the rendering process using 2 second pauses ('--pause') at each step to watch the progress, and finally set a python debugger trace ('--pdb') at step 5 to enter interactive mode:

Well, admit you are crazy, but do it like:

$ nik2img.py -m map.xml -o maps -n --pause 2 --pdb 5 # in this case the pdb_trace() is set immediately following load_map()
  • Note: The python debugger tool emulates your map being loaded and rendered by a custom python script within the python interpreter, allowing you to play around and learn the Mapnik python bindings like:
    STEP: 5 // --> BBOX (max extent of all layers) is: Envelope(-180.0,-93.188202,180.0,86.811798)
    Total time: 6.58748102188 seconds | Last step: 5.00679016113e-06 seconds
    
    >>> Entering PDB interpreter
    --Return--
    > /usr/local/bin/nik2img.py(131)set_trace()->None
    -> pdb.set_trace()
    
    (Pdb) mapnik_map
    <mapnik._mapnik.Map object at 0x2e0db0>
    (Pdb) mapnik_map.scale()
    0.59999999999999998
    (Pdb) mapnik_map.envelope()
    Envelope(-180.0,-93.188202,180.0,86.811798)
    (Pdb) mapnik_map.envelope().center()
    Coord(0.0,-3.188202)
    (Pdb) mapnik_map.background
    <mapnik._mapnik.Color object at 0x287960>
    (Pdb) mapnik_map.background.__str__()
    'rgb (70,130,180,255)'
    (Pdb) mapnik_map.srs
    '+proj=latlong +datum=WGS84'
    
  • Then do:
    (Pdb) continue # will leave the pdb intepreter
    

And for all the options available do:

$ nik2img.py -h

Attachments