TransWikia.com

Add PNG as a layer in Mapnik

Geographic Information Systems Asked by Phu Nguyen on December 23, 2020

I’m playing around with Mapnik and trying to add a PNG image over a map created from an XML file. This is my XML file:

<Map background-color="white" minimum-version="0.7.2" srs="+init=epsg:3395">
    <Style name="population">
        <Rule><PolygonSymbolizer fill="#c7e9b4" /></Rule>
    </Style>

    <Layer name="countries" srs="+init=epsg:3395">
        <StyleName>population</StyleName>
        <Datasource>
            <Parameter name="type">shape</Parameter>
            <Parameter name="file">world_merc</Parameter>
        </Datasource>
    </Layer>
</Map>

And my code is like this:

import mapnik

m = mapnik.Map(600,400)
xml = 'population.xml'
mapnik.load_map(m, xml)

s = mapnik.Style()
rule = mapnik.Rule()
sym = mapnik.RasterSymbolizer()
rule.symbols.append(sym)
s.rules.append(rule)
m.append_style('Raster Style', s)

lyr = mapnik.Layer('GDAL Layer')
#lyr.srs = '+init=epsg:3395'
lyr.datasource = mapnik.Gdal(file='data.png', bbox=(0,0,1000,1000))
lyr.styles.append('Raster Style')
m.layers.append(lyr)

m.zoom_all()
mapnik.render_to_file(m,'map.png')

When that lyr.srs line is uncommented, the image is not shown, only the XML layer is there. When it is commented out, only a small part of the image is shown above the XML layer, the lower left corner of the image seems to be at the position of the origin of the SRS.

I just think that PNG files don’t have info about where they should be, so the parameter bbox of the Gdal function seems to be good; however, it appears that that parameter doesn’t have any effect in my code. The documentation is for mapnik 2.2 and my mapnik version is 3.0 so I’m not sure though.

Could someone tell me whether the parameter bbox has the same meaning as I think, and how I can control the position and size of the image displayed on the map?

One Answer

I suggest using gdal.Translate to convert your PNG to a georeferenced GeoTIFF:

from osgeo import gdal
bounds = [west, north, east, south]
gdal.Translate('output.tif', 'input.png', outputSRS='EPSG:4326', outputBounds=bounds)

Use the coordinates from your bounding box as the outputBounds, making sure the SRS is correct (probably 4326). You can then include this GeoTIFF as a raster layer. The XML would look like this:

<Layer name="geotiff" srs="+init=epsg:4326">
    <StyleName>geotiff</StyleName>
    <Datasource>
        <Parameter name="type">gdal</Parameter>
        <Parameter name="file">output.tif</Parameter>
        <Parameter name="format">tiff</Parameter>
    </Datasource>
</Layer>

<Style name="geotiff">
    <Rule>
        <RasterSymbolizer scaling="lanczos" />
    </Rule>
</Style>

That bbox option you were using: I'm not sure what it does, but I assume it just sets the layer extent, as opposed to actually georeferencing the raster.

Answered by kontextify on December 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP