Skip to content

Instantly share code, notes, and snippets.

@StyXman
Created June 16, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StyXman/4fee8eabdde9229080b291e9fe312c7d to your computer and use it in GitHub Desktop.
Save StyXman/4fee8eabdde9229080b291e9fe312c7d to your computer and use it in GitHub Desktop.
Render SVG maps with mapnik and Pyhton
#! /usr/bin/python2
import mapnik
import cairo
import sys
# a2 = 16.55 x 23.4 inches
# take one inch on each margin
# a2_margin = 14.55 x 21.4
# 300 dpi for printing
a2_margin_px= (int (14.55*300), int (21.4*300))
mp= mapnik.Map (*a2_margin_px)
mapnik.load_map (mp, sys.argv[1], True)
prj= mapnik.Projection (mp.srs)
# perrito
# -64.6738,-31.0117,-64.4602,-30.7684
bbox= mapnik.Box2d (prj.forward (mapnik.Coord (-64.6738, -31.0117)), # W, S
prj.forward (mapnik.Coord (-64.4602, -30.7684))) # E, N
mp.zoom_to_box (bbox)
# mp.zoom_all ()
print repr (mp.envelope ())
# no buffer so labels don't fall off the map
mp.buffer_size= 0
# pdf= mapnik.printing.PDFPrinter (pagesize=mapnik.printing.pagesizes['a2'],
# margin=0.0254,
# resolution=mapnik.printing.resolutions.dpi300,)
# pdf.render_map (mp, 'perrito.pdf')
svg= cairo.SVGSurface ('perrito.svg', *a2_margin_px)
mapnik.render (mp, svg)
img= mapnik.Image (*a2_margin_px)
mapnik.render (mp, img)
img.save ('perrito.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment