I am trying to make mapnik read this geotiff file and render it as a PNG file.
Unfortunately all the colors from the geotiff file are not retained. The resulting PNG is completely blank.
This is my Python code:
import mapnik
m = mapnik.Map(800, 600)
m.srs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
s = mapnik.Style()
r = mapnik.Rule()
s.rules.append(r)
m.append_style('Default', s)
ds = mapnik.Gdal(file='K100_2017_1_20_612_62.tif', band=1)
layer = mapnik.Layer('layer')
layer.datasource = ds
layer.styles.append('Default')
m.layers.append(layer)
m.zoom_all()
mapnik.render_to_file(m, 'mapnik.png', 'png32:z=1')
I can get some visible pixels and colors into the PNG by adding a RasterColorizer like this:
rs.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(0, 0, 0, 0))
rs.colorizer.add_stop(-417, mapnik.Color(0, 0, 0))
rs.colorizer.add_stop(68, mapnik.Color(255, 255, 255))
rs.colorizer.add_stop(234, mapnik.Color(255, 0, 0))
rs.colorizer.add_stop(461, mapnik.Color(0, 0, 255))
rs.colorizer.add_stop(719, mapnik.Color(0, 255, 0))
s = mapnik.Style()
r = mapnik.Rule()
r.symbols.append(rs)
But then I need to manually define all the colours in the PNG, which just doesn't make sense when all the colours are already defined in the TIFF file. And in the example the PNG-colors are plain wrong and useless.
What am I missing to "reuse" the colors from the TIF in the rendered PNG ?
SOLUTION
Based on input from user30184 this Python code renders correctly:
import mapnik
map = mapnik.Map(800, 600)
style=mapnik.Style()
rule=mapnik.Rule()
rule.symbols.append(mapnik.RasterSymbolizer())
style.rules.append(rule)
map.append_style('Raster Style',style)
lyr = mapnik.Layer('GDAL Layer from TIFF file')
lyr.datasource = mapnik.Gdal(file='K100_2017_1_20_612_62.tif')
lyr.styles.append('Raster Style')
map.srs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
map.layers.append(lyr)
map.zoom_all()
mapnik.render_to_file(map, 'mapnik.png', 'png32:z=1')
It renders this PNG:
Time to study the details...