New in 0.8
(Note: This document has been copied and translated from https://github.com/BenMoores/mapnik-trunk/wiki/RasterColorizer
The Raster Colorizer allows the palette of rasters to be modified, for example, colorizing a height map.
To use the colorizer, create a style with a raster symbolizer and add a raster colorizer to it.
The DataSource making use of the style must be a single band raster file opened with the GDAL input plugin. It will not work with other input plugins; and it will not work if more than one band is loaded. It will not work unless you explicitly request the band to be loaded (the default band=-1 does not work even if the input has a single band). It will not work with the PostGIS Raster gdal driver (as of GDAL-0.11).
The colorizer works in the following way:
The available modes are inherit, discrete, linear, and exact. Inherit is only valid for stops, and not the default colorizer mode. It means that the stop will inherit the mode of the containing colorizer.
Discrete causes all input values from the stops value, up until the next stops value (or forever if this is the last stop) to be translated to the stops color.
Linear causes all input values from the stops value, up until the next stops value to be translated to a color which is linearly interpolated between the two stops colors. If there is no next stop, then the discrete mode will be used.
Exact causes an input value which matches the stops value to be translated to the stops color. The colorizers epsilon value can be used to make the match a bit fuzzy (in the 'greater than' direction).
Required attributes: none
See https://github.com/mapnik/mapnik/blob/master/tests/data/good_maps/raster_symbolizer.xml for more examples.
In this example XML, the following value to color translation is performed:
<?xml version="1.0" encoding="utf-8"?> <Map srs="+proj=latlong +datum=WGS84"> <Style name="elevation"> <Rule> <RasterSymbolizer> <RasterColorizer default-mode="linear" default-color="white" epsilon="0.001"> <stop color="blue" value = "-1000" /> <stop color="red" value = "-500" mode = "discrete" /> <stop color="yellow" value = "0" /> <stop value = "5" /> <stop color="red" value = "10" /> <stop color="green" value = "15" /> <stop color="black" value = "17" mode = "exact" /> <stop color="indigo" value = "100" /> </RasterColorizer> </RasterSymbolizer> </Rule> </Style> <Layer name="dataraster"> <StyleName>elevation</StyleName> <Datasource> <Parameter name="file">../mapnik2/tests/data/raster/dataraster.tif</Parameter> <Parameter name="type">gdal</Parameter> <Parameter name="band">1</Parameter> </Datasource> </Layer> </Map>
Python bindings are available. The objects are as follows:
It has the properties default_color, default_mode, epsilon, and stops (read only).
It has the functions add_stop, and get_color.
This is an array of ColorizerStop objects. It is the type of the RasterColorizer stops property.
It has the properties color, value, and mode.
This is the enumeration of the stop modes. The values are mapnik2.COLORIZER_LINEAR, mapnik2.COLORIZER_DISCRETE, mapnik2.COLORIZER_EXACT, and mapnik2.COLORIZER_INHERIT.
import mapnik2 c = mapnik2.RasterColorizer( mapnik2.COLORIZER_DISCRETE , mapnik2.Color(0,0,0,255) ) c.epsilon = 0.001 c.add_stop(-10) c.add_stop(15.83, mapnik2.COLORIZER_EXACT) c.add_stop(20, mapnik2.Color("red")) c.add_stop(30.25, mapnik2.COLORIZER_LINEAR, mapnik2.Color("green")) c.get_color(23.124) c.stops.color