I set up Mapnik to custom-render a GeoTiff file. python and xml files to do so were taken from a github mapnik example, and the successful image output is a rainbow-esque .png from the original .tif. Excellent.

All works fine, but now the question is: how can I configure Mapnik (either the python, xml or otherwise) to consider multiple .tif files when rendering images? These won't be overlapping tifs that contain different sets of information, but rather geographically-adjacent tifs that contain the same type of information. Together, they will cover a larger geographical area.

For larger context: my goal is to download all SRTM files (there are 1000s) and store them in a single directory. Using mod_tile, Mapnik, and whatever other bits of software I need, I want to be able to generate tile images of the SRTM data for consumption by a map in a browser (e.g. Leaflet, Google Map). mod_tile will be used for caching, but I will need to use Mapnik to generate images on the fly according to certain user preferences (e.g. user may be able to specify their own color bands for the elevation data).

How do I best make Mapnik consider multiple side-by-side GeoTiffs, when generating tiles? Is that even possible? do I have to create a single (massive) GeoTiff by stitching all of the SRTM files together? GDAL virtual format?

I may be looking at the problem the wrong way, as I am new to Mapnik and creating tiles - please let me know if I am.

Here is the python code:

import mapnik
mapxmlfile = 'raster_colorizer_small.xml'
outputfile = 'raster_colorizer_test.png'
m = mapnik.Map(800, 600)
mapnik.load_map(m, mapxmlfile)
mapnik.render_to_file(m, outputfile)

And here is the raster_colorizer_small.xml file:

<Map background-color="white" srs="+init=epsg:32630">
  <Style name="DISCRETE RAINBOW">
        <RasterColorizer default-mode="discrete" default-color="white" >
            <stop color="red"    value = "0"  />
            <stop color="orange" value = "5"  />
            <stop color="yellow" value = "10" />
            <stop color="green"  value = "15" />
            <stop color="blue"   value = "20" />
            <stop color="indigo" value = "25" />
  <Layer name="dataraster" srs="+init=epsg:32630">
      <StyleName>DISCRETE RAINBOW</StyleName>
          <Parameter name="file">../data/dataraster.tif</Parameter>
          <Parameter name="type">gdal</Parameter>
          <Parameter name="band">1</Parameter>

1 Answer 1


This sounds like a perfectly reasonable thing to do with mapnik. You have (at least) two options to do that.

The first and simplest option is to do something along the lines of (pseudo code):

create a mapnik map
create a style (potentially based on user input) # you can also create a style per layer
append the style to the map
for each file
    create a datasource (with the Gdal plugin in your case probably)
    create a layer
    attach the datasource to the layer
    append your style to the layer styles
    append the layer to the map
set the geographic extent of the map
render the map

You can have a look there: https://github.com/mapnik/mapnik/wiki/GettingStartedInPython and there: http://mapnik.org/docs/v2.0.2/api/python/mapnik._mapnik-module.html

The other option is to build your mapnik xml on the fly, load the map with the xml and then render the map. You can find the mapnik xml reference there: https://github.com/mapnik/mapnik/wiki/XMLConfigReference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.