/usr/share/pyshared/TileCache/Layers/Mapnik.py is in tilecache 2.11-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # BSD Licensed, Copyright (c) 2006-2010 TileCache Contributors
import sys
from TileCache.Layer import MetaLayer
class Mapnik(MetaLayer):
config_properties = [
{'name':'name', 'description': 'Name of Layer'},
{'name':'mapfile', 'description': 'Location of Mapnik XML map description.'},
{'name':'projection', 'description': 'Target map projection.'},
{'name':'fonts', 'description': 'Comma-seperated list of fonts to add to the Mapik registered fonts list.'},
] + MetaLayer.config_properties
def __init__ (self, name, mapfile = None, projection = None, fonts = None, **kwargs):
MetaLayer.__init__(self, name, **kwargs)
self.mapfile = mapfile
self.mapnik = None
self.projection = projection
if fonts:
self.fonts = fonts.split(",")
else:
self.fonts = []
def renderTile(self, tile):
import mapnik
if self.mapnik:
m = self.mapnik
else:
if self.fonts:
engine = mapnik.FontEngine.instance()
for font in self.fonts:
engine.register_font(font)
# Init it as 0,0
m = mapnik.Map( 0, 0 )
mapnik.load_map(m,self.mapfile)
if self.projection:
m.srs = self.projection
# Restrict layer list, if requested
if self.layers and self.layers != self.name:
layers = self.layers.split(",")
for layer_num in range(len(m.layers)-1, -1, -1):
l = m.layers[layer_num]
if l.name not in layers:
del m.layers[layer_num]
if self.debug:
print >>sys.stderr, "Removed layer %s loaded from %s, not in list: %s" % (l.name, self.mapfile, layers)
# this will insure that it gets cached in mod_python
self.mapnik = m
# Set the mapnik size to match the size of the current tile
m.width = tile.size()[0]
m.height = tile.size()[1]
bbox = tile.bounds()
bbox = mapnik.Envelope(bbox[0], bbox[1], bbox[2], bbox[3])
m.zoom_to_box(bbox)
im = mapnik.Image( *tile.size() )
mapnik.render(m, im)
if hasattr(im, 'tostring'):
if self.paletted:
data = im.tostring('png256')
else:
data = im.tostring(self.extension)
tile.data = data
return tile.data
elif hasattr(mapnik, 'rawdata'):
data = mapnik.rawdata(im)
import PIL.Image, StringIO
im = PIL.Image.fromstring('RGBA', tile.size(), data)
buffer = StringIO.StringIO()
if self.paletted:
print >>sys.stderr, "Mapnik's 8-bit (png256) format not supported with PIL"
im.save(buffer, self.extension)
buffer.seek(0)
tile.data = buffer.read()
return tile.data
else:
raise Exception("Something is wrong: your version of Mapnik can't create a string from an image.")
|