Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created June 17, 2010 19:42
Show Gist options
  • Save tmcw/442673 to your computer and use it in GitHub Desktop.
Save tmcw/442673 to your computer and use it in GitHub Desktop.
import tornado.httpserver
import tornado.ioloop
import tornado.web
import mapnik
from twcache import TWCache
from sphericalmercator import SphericalMercator
class TileHandler(tornado.web.RequestHandler):
def get(self, datafile, mapfile, z, x, y):
z, x, y = map(int, [z, x, y])
merc = SphericalMercator(levels=23, size=256)
mapfile_cache = TWCache('mapfiles')
datafile_cache = TWCache('data')
envelope = merc.xyz_to_envelope(x, y, z)
_mapnik_map = mapnik.Map(256, 256)
im = mapnik.Image(256, 256)
mapnik.load_map(_mapnik_map,
mapfile_cache.filecache(mapfile))
for layer in _mapnik_map.layers:
layer.datasource = datafile_cache.dscache(datafile)
_mapnik_map.layers.append(layer)
_mapnik_map.zoom_to_box(envelope)
_mapnik_map.buffer_size = 128
mapnik.render(_mapnik_map, im)
self.set_header("Content-Type", "image/png")
self.write(im.tostring('png'))
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/tile/([^/]+)/([^/]+)/([0-9]+)/([0-9]+)/([0-9]+)", TileHandler),
]
tornado.web.Application.__init__(self, handlers)
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment