PostGIS – Creating Mapnik XML File for Database

mapnikogr2ogrpostgis

I have a folder of sea maps in S57 format (i.e. file.000 files). And I want to create a .png at some location as input. So my tools are: old debian-based OS, S57 as map format, navi2pg or ogr2ogr to parse maps into database, GDAL version 1.10.1, PostgreSQL+PostGIS to hold the maps and mapnik to render PNG.
I use navi2pg https://github.com/nextgis/navi2pg like this

$ ./navi2pg --schema public PG:"host=localhost user='user' password='useruser' dbname=testdb port=5433" ./map_file.000 ./templates/mapserver.map.template ./templates/mapnik.xml.template ./templates/mapnik.py.template

What it does is parse the map file to db and generates 3 files: mapserver file, which I don't need, .xml file (which is needed for mapnik) and .py file, see below, which I can launch with Python to create a PNG picture. All is good, but the problem is I can't use it, or I don't know how, for multiple map files, it shows an error that layer is already exists and exits when I try it. I tried to tinker code a bit but I did not succeed.

I use ogr2ogr like this

$ ogr2ogr -skipfailures -append -update -s_srs "+init=epsg:4326" -f PostgreSQL PG:"host=localhost user='user' password='useruser' dbname=testdb port=5433" ./map_file.000

it parses map to db and you can use -append to write more to existing tables, as far as I understand. But the problem is I don't have the .xml file that has rules how to render the picture with mapnik. I tried to change the XML file that comes from navi2pg but I failed again, because ogr2ogr and navi2pg put the map into the db differently, not only different names, but structurally as well; in ogr2ogr all layers have different tables, in navi2pg they are grouped in some way. Also I am not exactly sure if I was doing the right thing.
I checked both databases in QGIS and they look the same, so no question about the db part for one file.
When I have the db and XML ready, I want to use mapnik.py, it doesnt have location input, it just creates a PNG from the map:

import mapnik

m = mapnik.Map(1024,1024)
m.background = mapnik.Color('steelblue')
mapnik.load_map(m, 'file.mapnik.xml')

extent = mapnik.Box2d(2.914476e+06, 8.260046e+06, 3.131549e+06, 8.531149e+06)

m.zoom_to_box(extent)

mapnik.render_to_file(m,'file.mapnik.png', 'png')
print "rendered image to 'file.mapnik.png'"

This creates a picture with the whole map in 1024×1024. I can later add arguments and change the extent with location and scale and have different parts of map on PNG.

Additional problem comes with Box2d arguments, when I use navi2pg it generates the min and max sizes and I can understand the size of map. and I can't understand how it does it, and these are not geo coords. I found a PostGIS function ST_EstimatedExtent
postgis.net/docs/ST_EstimatedExtent.html and it looks like something I need, but I can't understand on which column to use it.

Best Answer

The solution i used - write the XML file myself. After i decided what layers i will need for final picture, i used templates and adapted them for my case.

Related Question