/usr/share/pyshared/TileStache/Providers.py is in tilestache 1.31.0-1.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | """ The provider bits of TileStache.
A Provider is the part of TileStache that actually renders imagery. A few default
providers are found here, but it's possible to define your own and pull them into
TileStache dynamically by class name.
Built-in providers:
- mapnik (Mapnik)
- proxy (Proxy)
- vector (TileStache.Vector.Provider)
- url template (UrlTemplate)
- mbtiles (TileStache.MBTiles.Provider)
Example built-in provider, for JSON configuration file:
"layer-name": {
"provider": {"name": "mapnik", "mapfile": "style.xml"},
...
}
Example external provider, for JSON configuration file:
"layer-name": {
"provider": {"class": "Module:Classname", "kwargs": {"frob": "yes"}},
...
}
- The "class" value is split up into module and classname, and dynamically
included. If this doesn't work for some reason, TileStache will fail loudly
to let you know.
- The "kwargs" value is fed to the class constructor as a dictionary of keyword
args. If your defined class doesn't accept any of these keyword arguments,
TileStache will throw an exception.
A provider must offer one of two methods for rendering map areas.
The renderTile() method draws a single tile at a time, and has these arguments:
- width, height: in pixels
- srs: projection as Proj4 string.
"+proj=longlat +ellps=WGS84 +datum=WGS84" is an example,
see http://spatialreference.org for more.
- coord: Coordinate object representing a single tile.
The renderArea() method draws a variably-sized area, and is used when drawing
metatiles. It has these arguments:
- width, height: in pixels
- srs: projection as Proj4 string.
"+proj=longlat +ellps=WGS84 +datum=WGS84" is an example,
see http://spatialreference.org for more.
- xmin, ymin, xmax, ymax: coordinates of bounding box in projected coordinates.
- zoom: zoom level of final map. Technically this can be derived from the other
arguments, but that's a hassle so we'll pass it in explicitly.
A provider may offer a method for custom response type, getTypeByExtension().
This method accepts a single argument, a filename extension string (e.g. "png",
"json", etc.) and returns a tuple with twon strings: a mime-type and a format.
Note that for image and non-image tiles alike, renderArea() and renderTile()
methods on a provider class must return a object with a save() method that
can accept a file-like object and a format name, e.g. this should word:
provder.renderArea(...).save(fp, "TEXT")
... if "TEXT" is a valid response format according to getTypeByExtension().
Non-image providers and metatiles do not mix.
For an example of a non-image provider, see TileStache.Vector.Provider.
"""
import os
import logging
from StringIO import StringIO
from posixpath import exists
from thread import allocate_lock
from urlparse import urlparse, urljoin
import urllib
from tempfile import mkstemp
from string import Template
from urllib import urlopen
import urllib2
from glob import glob
from time import time
try:
import mapnik2 as mapnik
except ImportError:
try:
import mapnik
except ImportError:
# It's possible to get by without mapnik,
# if you don't plan to use the mapnik provider.
pass
try:
from PIL import Image
except ImportError:
# On some systems, PIL.Image is known as Image.
import Image
import ModestMaps
from ModestMaps.Core import Point, Coordinate
import Vector
import MBTiles
import Geography
def getProviderByName(name):
""" Retrieve a provider object by name.
Raise an exception if the name doesn't work out.
"""
if name.lower() == 'mapnik':
return Mapnik
elif name.lower() == 'proxy':
return Proxy
elif name.lower() == 'url template':
return UrlTemplate
elif name.lower() == 'vector':
return Vector.Provider
elif name.lower() == 'mbtiles':
return MBTiles.Provider
raise Exception('Unknown provider name: "%s"' % name)
class Proxy:
""" Proxy provider, to pass through and cache tiles from other places.
This provider is identified by the name "proxy" in the TileStache config.
Additional arguments:
- url (optional)
URL template for remote tiles, for example:
"http://tile.openstreetmap.org/{Z}/{X}/{Y}.png"
- provider (optional)
Provider name string from Modest Maps built-ins.
See ModestMaps.builtinProviders.keys() for a list.
Example: "OPENSTREETMAP".
One of the above is required. When both are present, url wins.
Example configuration:
{
"name": "proxy",
"url": "http://tile.openstreetmap.org/{Z}/{X}/{Y}.png"
}
"""
def __init__(self, layer, url=None, provider_name=None):
""" Initialize Proxy provider with layer and url.
"""
if url:
self.provider = ModestMaps.Providers.TemplatedMercatorProvider(url)
elif provider_name:
if provider_name in ModestMaps.builtinProviders:
self.provider = ModestMaps.builtinProviders[provider_name]()
else:
raise Exception('Unkown Modest Maps provider: "%s"' % provider_name)
else:
raise Exception('Missing required url or provider parameter to Proxy provider')
def renderTile(self, width, height, srs, coord):
"""
"""
if srs != Geography.SphericalMercator.srs:
raise Exception('Projection doesn\'t match EPSG:900913: "%(srs)s"' % locals())
if (width, height) != (256, 256):
raise Exception("Image dimensions don't match expected tile size: %(width)dx%(height)d" % locals())
img = None
urls = self.provider.getTileUrls(coord)
for url in urls:
body = urllib.urlopen(url).read()
tile = Image.open(StringIO(body)).convert('RGBA')
if len(urls) == 1:
#
# if there is only one URL, don't bother
# with PIL's non-Porter-Duff alpha channeling.
#
return tile
elif img is None:
#
# for many URLs, paste them to a new image.
#
img = Image.new('RGBA', (width, height))
img.paste(tile, (0, 0), tile)
return img
global_mapnik_lock = allocate_lock()
class Mapnik:
""" Built-in Mapnik provider. Renders map images from Mapnik XML files.
This provider is identified by the name "mapnik" in the TileStache config.
Additional arguments:
- mapfile (required)
Local file path to Mapnik XML file.
- fonts (optional)
Local directory path to *.ttf font files.
More information on Mapnik and Mapnik XML:
- http://mapnik.org
- http://trac.mapnik.org/wiki/XMLGettingStarted
- http://trac.mapnik.org/wiki/XMLConfigReference
"""
def __init__(self, layer, mapfile, fonts=None):
""" Initialize Mapnik provider with layer and mapfile.
XML mapfile keyword arg comes from TileStache config,
and is an absolute path by the time it gets here.
"""
maphref = urljoin(layer.config.dirpath, mapfile)
scheme, h, path, q, p, f = urlparse(maphref)
if scheme in ('file', ''):
self.mapfile = path
else:
self.mapfile = maphref
self.layer = layer
self.mapnik = None
engine = mapnik.FontEngine.instance()
if fonts:
fontshref = urljoin(layer.config.dirpath, fonts)
scheme, h, path, q, p, f = urlparse(fontshref)
if scheme not in ('file', ''):
raise Exception('Fonts from "%s" can\'t be used by Mapnik' % fontshref)
for font in glob(path.rstrip('/') + '/*.ttf'):
engine.register_font(str(font))
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
"""
"""
start_time = time()
if self.mapnik is None:
self.mapnik = mapnik.Map(0, 0)
if exists(self.mapfile):
mapnik.load_map(self.mapnik, str(self.mapfile))
else:
handle, filename = mkstemp()
os.write(handle, urlopen(self.mapfile).read())
os.close(handle)
mapnik.load_map(self.mapnik, filename)
os.unlink(filename)
logging.debug('TileStache.Providers.Mapnik.renderArea() %.3f to load %s', time() - start_time, self.mapfile)
#
# Mapnik can behave strangely when run in threads, so place a lock on the instance.
#
if global_mapnik_lock.acquire():
self.mapnik.width = width
self.mapnik.height = height
self.mapnik.zoom_to_box(mapnik.Envelope(xmin, ymin, xmax, ymax))
img = mapnik.Image(width, height)
mapnik.render(self.mapnik, img)
global_mapnik_lock.release()
img = Image.fromstring('RGBA', (width, height), img.tostring())
logging.debug('TileStache.Providers.Mapnik.renderArea() %dx%d in %.3f from %s', width, height, time() - start_time, self.mapfile)
return img
class UrlTemplate:
""" Built-in URL Template provider. Proxies map images from WMS servers.
This provider is identified by the name "url template" in the TileStache config.
Additional arguments:
- template (required)
String with substitutions suitable for use in string.Template.
- referer (optional)
String to use in the "Referer" header when making HTTP requests.
More on string substitutions:
- http://docs.python.org/library/string.html#template-strings
"""
def __init__(self, layer, template, referer=None):
""" Initialize a UrlTemplate provider with layer and template string.
http://docs.python.org/library/string.html#template-strings
"""
self.layer = layer
self.template = Template(template)
self.referer = referer
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
""" Return an image for an area.
Each argument (width, height, etc.) is substituted into the template.
"""
mapping = {'width': width, 'height': height, 'srs': srs, 'zoom': zoom}
mapping.update({'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax})
href = self.template.safe_substitute(mapping)
req = urllib2.Request(href)
if self.referer:
req.add_header('Referer', self.referer)
body = urllib2.urlopen(req).read()
tile = Image.open(StringIO(body)).convert('RGBA')
return tile
|