forked from springmeyer/quantumnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_wrapper.py
35 lines (30 loc) · 1.06 KB
/
render_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
import os
try:
import mapnik2 as mapnik
except ImportError:
import mapnik
def render_to_file(mapnik_map,output,format):
# get the full path for a users directory
if '~' in output:
output = os.path.expanduser(output)
# mapnik won't create directories so
# we have to make sure they exist first...
dirname = os.path.dirname(output)
if not os.path.exists(dirname):
os.makedirs(dirname)
# render out to the desired format
if format in ('png','png256','jpeg') or (hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 700):
try:
mapnik.render_to_file(mapnik_map,output,format)
except Exception, e:
return (False,e)
else:
try:
import cairo
surface = getattr(cairo,'%sSurface' % format.upper())(output,mapnik_map.width,mapnik_map.height)
mapnik.render(mapnik_map, surface)
surface.finish()
except Exception, e:
return (False,e)
return (True,mapnik_map)