0

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)

Which renders this PNG: this image

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:

enter image description here

Time to study the details...

  • Your tiff file has four bands, R, G, B and alpha. What is your aim when you read just the first band? – user30184 Dec 9 '17 at 14:53
  • I am new to mapnik and the geotiff format; so my lack of knowledge my play a role. Do you mean that "band=1" should be "band=4" ? – tbsalling Dec 9 '17 at 15:00
  • Computers build colors from red, green, and blue components. Therefore you must read bands 1, 2, and 3. Fourth band (the alpha band) defines transparency. I am sure that you will find code examples from the net. – user30184 Dec 9 '17 at 15:12
  • I do know that. I am also aware of other color models supported by TIFF. My question is how to bring these colours specified in the TIFF file to the rendered PNG file? I cannot find any examples via web search. – tbsalling Dec 9 '17 at 17:29
  • Try this github.com/mapnik/mapnik/wiki/GDAL – user30184 Dec 9 '17 at 17:40
0

Usage of the Mapnik GDAL PlugIn is documented in https://github.com/mapnik/mapnik/wiki/GDAL.

The original code selects only the first band of the source image

ds = mapnik.Gdal(file='K100_2017_1_20_612_62.tif', band=1)

band integer request for a specific raster band index, -1 means all bands. Note that a band read from a single band raster gets interpreted as Grayscale if band=-1 is specified while they retain their original value when explicitly referenced with the "band" parameter. This affects effectiveness of RasterColorizer

Band is an optional parameter and therefore you can read all bands either by using "band=-1" or shorter as in the example on the above mentioned wiki page:

lyr.datasource = mapnik.Gdal(file='K100_2017_1_20_612_62.tif')

Usually it is not necessary to know how many bands the source file has but it is easy to check with gdalinfo

gdalinfo K100_2017_1_20_612_62.tif
Driver: GTiff/GeoTIFF
Files: K100_2017_1_20_612_62.tif
Size is 2000, 2000
Coordinate System is:
PROJCS["ETRS89 / UTM zone 32N",
    GEOGCS["ETRS89",
        DATUM["European_Terrestrial_Reference_System_1989",
            SPHEROID["GRS 1980",6378137,298.257222101,
                AUTHORITY["EPSG","7019"]],
            TOWGS84[0,0,0,0,0,0,0],
            AUTHORITY["EPSG","6258"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4258"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",9],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",EAST],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","25832"]]
Origin = (620000.000000000000000,6140000.000000000000000)
Pixel Size = (10.000000000000000,-10.000000000000000)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_IMAGEDESCRIPTION=6140_620
  TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
  TIFFTAG_SOFTWARE=Handmade Software, Inc. Image Alchemy v1.13
  TIFFTAG_XRESOLUTION=254
  TIFFTAG_YRESOLUTION=254
Image Structure Metadata:
  COMPRESSION=DEFLATE
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  620000.000, 6140000.000) ( 10d53'40.34"E, 55d23'29.67"N)
Lower Left  (  620000.000, 6120000.000) ( 10d53' 9.58"E, 55d12'43.03"N)
Upper Right (  640000.000, 6140000.000) ( 11d12'36.18"E, 55d23'10.60"N)
Lower Right (  640000.000, 6120000.000) ( 11d12' 0.32"E, 55d12'24.09"N)
Center      (  630000.000, 6130000.000) ( 11d 2'51.61"E, 55d17'57.21"N)
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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