Mapnik Users ->

I've been using Mapnik of late for desktop/print map development, and really enjoying not having to fire up a graphical program as much to do basic desktop cartography. However I've still thought it would be useful to georeference some of mapnik's png output to view in graphical programs like QGIS and uDig against existing data. This helps to get a quick sense of the resolution of your output to the input data.

Using an ESRI world file would allow for single mapnik pngs (or jpegs) to be opened in a variety of gis viewers and since GDAL also reads projection information gdal2tiles.py could even convert mapnik graphics to tiled kml overlays in a single step.

I think i've got a simple solution hacked up, but I'm curious if anyone has suggestions to improve upon it...

If I take the hello world tutorial download (http://trac.mapnik.org/attachment/wiki/XMLGettingStarted/hello_world.zip ) and modify it a bit this is what writing a world file might look like using the mapnik python bindings:

cheers,

Dane


----------------


#!/usr/bin/env python
from mapnik import *

def render_to_wld(map, path, x_rotation=0.0, y_rotation=0.0):
    """
    Outputs an ESRI world file that can be used to load the resulting
    image as a georeferenced raster in a variety of gis viewers.

A world file file is a plain ASCII text file consisting of six values separated
    by newlines. The format is:
        pixel X size
        rotation about the Y axis (usually 0.0)
        rotation about the X axis (usually 0.0)
        negative pixel Y size
        X coordinate of upper left pixel center
        Y coordinate of upper left pixel center

    Info from: http://gdal.osgeo.org/frmt_various.html#WLD
    """
    scale = map.scale()
    extent= map.envelope()
    upper_left_x_center = extent.minx+(scale/2)
    upper_left_y_center = extent.maxy+(scale/2)
wld_string = '%f\n%s\n%s\n-%f\n%f\n%f\n' % (scale ,y_rotation,x_rotation,scale,upper_left_x_center,upper_left_y_center)
    wld_file = open(path, 'w')
    wld_file.write(wld_string)
    print 'world file output written to %s:' % path
    print wld_string

m = Map(10000,5000,'+proj=latlong +datum=WGS84')
m.background = Color('steelblue')
s = Style()
r=Rule()
r.symbols.append(PolygonSymbolizer(Color('#f2eff9')))
r.symbols.append(LineSymbolizer(Color('rgb(50%,50%,50%)'),0.1))
s.rules.append(r)
m.append_style('My Style',s)
lyr = Layer('world')
lyr.datasource = Shapefile(file='../data/world_borders')
lyr.styles.append('My Style')
m.layers.append(lyr)
m.zoom_to_box(lyr.envelope())
filepath = 'map/hello_world_in_pure_python'
raster = '%s.png' % filepath
world_file = '%s.wld' % filepath
render_to_file(m, raster)
render_to_wld(m, world_file)







_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users

Reply via email to