Skip to content
Browse files

Cache metatiles on request.

Because some layers can be expensive to render but also don't change
often, this changeset allows the metatiles for specified layers to be
rendered once and reused thereafter.
  • Loading branch information...
1 parent 2b4ecc4 commit efcf4c96006095fdaf32d48336eed8cc825a8fa9 @asciiphil asciiphil committed Jun 9, 2012
Showing with 19 additions and 0 deletions.
  1. +1 −0 env.py
  2. +4 −0 set-toposm-env.templ
  3. +14 −0 toposm.py
View
1 env.py
@@ -30,6 +30,7 @@
JPEG_QUALITY = os.environ['JPEG_QUALITY']
TOPOSM_DEBUG = os.environ['TOPOSM_DEBUG']
EXTRA_FONTS_DIR = os.environ['EXTRA_FONTS_DIR']
+CACHE_LAYERS = frozenset(os.environ['CACHE_LAYERS'].split(','))
##### Common constants
View
4 set-toposm-env.templ
@@ -42,6 +42,10 @@ export TILE_SIZE=256
export BORDER_WIDTH=128
export ERROR_LOG="errors.log"
+# Mapnik layers to cache after rendering and reuse as needed. Comma-separated
+# list. e.g. CACHE_LAYERS="contours,landcoverrelief"
+export CACHE_LAYERS=""
+
# Quality setting for combined JPEG layer
export JPEG_QUALITY=90
View
14 toposm.py
@@ -111,6 +111,15 @@ def renderLoop(self):
self.q.task_done()
+def getCachedMetaTileDir(mapname, z, x):
+ return path.join(TEMPDIR, mapname, str(z), str(x))
+
+def getCachedMetaTilePath(mapname, z, x, y, suffix = "png"):
+ return path.join(getCachedMetaTileDir(mapname, z, x), str(y) + '.' + suffix)
+
+def cachedMetaTileExists(mapname, z, x, y, suffix = "png"):
+ return path.isfile(getCachedMetaTilePath(mapname, z, x, y, suffix))
+
def getMetaTileDir(mapname, z):
return path.join(BASE_TILE_DIR, mapname, str(z))
@@ -187,6 +196,8 @@ def renderMetaTile(z, x, y, ntiles, maps):
def renderLayer(name, z, x, y, ntiles, map, suffix = 'png'):
"""Renders the specified map tile (layer) as a mapnik.Image."""
console.debugMessage(' Rendering layer: ' + name)
+ if name in CACHE_LAYERS and cachedMetaTileExists(name, z, x, y, 'png'):
+ return mapnik.Image.open(getCachedMetaTilePath(name, z, x, y, 'png'))
env = getMercTileEnv(z, x, y, ntiles, True)
tilesize = getTileSize(ntiles, True)
map.zoom_to_box(env)
@@ -198,6 +209,9 @@ def renderLayer(name, z, x, y, ntiles, map, suffix = 'png'):
else:
image = mapnik.Image(tilesize, tilesize)
mapnik.render(map, image)
+ if name in CACHE_LAYERS:
+ ensureDirExists(getCachedMetaTileDir(name, z, x))
+ image.save(getCachedMetaTilePath(name, z, x, y, 'png'))
return image
def saveTiles(z, x, y, ntiles, mapname, image, suffix = 'png', imgtype = None):

0 comments on commit efcf4c9

Please sign in to comment.
Something went wrong with that request. Please try again.