Tribune DataViz

Matters of interest, from the data reporters and developers across Tribune Publishing

Making maps, part 4: Render with Mapnik

with 7 comments

This is part four of a five-part series about our recent explorations making choropleth maps using PostGIS, TileMill, Mapnik and Google Maps. Read parts one, two, three and five for the full effect. Bonus post: part six! The impatient may grab the source code from GitHub, or try out the demo.


You’ll need pip, virtualenv and virtualenvwrapper to follow along. (You’ll also need Mapnik, which you should have installed during part three.) If you’re not familiar with virtualenv yet, this will be a happy moment in your life as a Python developer.

First, let’s fire up an env and install our requirements (I would normally use —no-site-packages here, but I want Mapnik).

Run this in your project directory…

mkvirtualenv making-maps-demo
pip install -r requirements.txt

Carto -> Mapnik

To render your tile layer, you’ve got to first compile the Carto to Mapnik XML. We can use the copy of Carto that’s installed with Mapnik, there’s no need to compile your own. I’ve got a little script that copies the files to the project directory (good for version control!) and compiles things. You will need to adjust the path to TileMill for your installation location.

Run the style script…


#! /bin/sh
cp /Applications/TileMill/files/project/census_demo/* tilemill
/Applications/TileMill/bin/carto tilemill/census_demo.mml > tilemill/census_demo.xml

Scripting Mapnik

Mapnik is not a tile-rendering system. It is an image generator. So, we must explicitly instruct it on how to cut our tiles. Luckily, the folks at Open Street Map already did the hard work. Our Mapnik script (source included in the demo code) is nearly the same, just generalized a bit.

To run the rendering script, you’ll need to know the extents of your state in latitude and longitude. There’s certainly a better way to do this, but my solution has been the LatLng plugins for Google Maps Labs. Grab the coordinates of the upper-left and lower-right corners of the area you’d like to render.

The script clears out the tiles directory (prefixed with a . as to not upset TextMate, it’s bad at lots of files) and tells our Mapnik script where to find your styles, where it should put the tiles, the extents of the area to be rendered, the minimum and maximum zoom levels and the number of processor cores to use.

Finally, run the render script…


#! /bin/sh
rm -rf census-demo/.tiles
mkdir census-demo/.tiles
python tilemill/census_demo.xml census-demo/.tiles/ 42.57 -91.65 36.9 -87.23 7 10 2

The full cycle

If you’ve got everything right, then rendering your tiles should be as easy as pie. (And by that, I mean as difficult as pie. Pie is a damned hard thing to get right. But I digress…) When I’m fine-tuning my maps, I’ll run through this cycle many times:


Looking in your .tiles dir, you will hopefully find a pile of pretty tiles, ready to be overlayed on a map.

Written by Brian Boyer

March 8, 2011 at 2:10 pm

7 Responses

Subscribe to comments with RSS.

  1. […] Render with Mapnik […]

    Making Maps | GIS Lounge

    March 14, 2011 at 3:19 pm

  2. Hi Brian,

    I think you can skip this step (i.e. all of number 4–compiling the carto, scripting mapnik etc.) by exporting the tiles from tilemill as .mbtiles, and extracting the images from the .mbtiles file directly with something like this: and then chucking them onto a server. Dunno if tilemill had that capability when you were using it…

    Only bump is ya gotta redo fetchtile (assuming yr using google maps) slightly; for reasons that make no sense to me, they reversed the y coordinate, so you need something like this:

    var y = Math.pow(2,zoom) – coord.y – 1 ;

    Jacob Fenton

    July 15, 2011 at 8:36 am

    • Thanks, Jacob! We’ve actually changed how we do this recently, and I need to write a follow-up post.

      Chris wrote a fantastic toolkit called Invar ( that makes rendering and deployment to S3 very, very fast. But the .mbtiles method you describe is a great alternative as well. :)

      Brian Boyer

      July 15, 2011 at 9:00 am

  3. […] making choropleth maps using PostGIS, TileMill, Mapnik and Google Maps. Read parts one, two, three, four and five for the full effect. The impatient may grab the source code from GitHub, or try out the […]

  4. […] Maps blog post series: parts one, two, three, four, five, […]

  5. It may be worth noting that the OS X installer for TileMill does not include Carto (unless I’m missing something). Instead, build TileMill from the source following these instructions:
    Carto will be in tilemill/node_modules/carto/bin/carto

    Also, if you haven’t installed Mapnik2 yet, try the latest installer here, although the site was down for some time last week:

    As always, awesome work. It’s good to finally dig in to more advanced mapping techniques!

    Kevin Schaul (@foxyNinja7)

    October 4, 2011 at 3:05 pm

  6. […] the by, I also just learned about this nice five part tutorial by the Chicago Tribune which deals with TileMill and many other tools to create an […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: