Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A tool to parse the OSM API's list of expired z12 tiles and delete the corresponding tiles in a TileStache disk cache.
import sys
import xml.etree.cElementTree as ET
import urllib2
# Expire tiles in a TileStache default file cache based on the OpenStreetMap server's z12 change output
# at
maxZoom = 19
if len(sys.argv) < 2:
print 'Missing tile cache root path argument.'
rootPath = sys.argv[1]
urlstream = urllib2.urlopen('')
def deleteZoom(z, x, y):
xStr = str(x).zfill(9)
yStr = str(y).zfill(9)
path = "%s/%02d/%s/%s/%s/%s/%s/%s.png" % (rootPath,
xStr[0:3], xStr[3:6], xStr[6:9],
yStr[0:3], yStr[3:6], yStr[6:9])
print path
if z == maxZoom:
# delete subtiles
deleteZoom(z + 1, 2*x , 2*y )
deleteZoom(z + 1, 2*x+1, 2*y )
deleteZoom(z + 1, 2*x , 2*y+1)
deleteZoom(z + 1, 2*x+1, 2*y+1)
for event, elem in ET.iterparse(urlstream, events=('start',)):
if elem.tag == "tile":
z12z = int(elem.attrib['z'])
z12x = int(elem.attrib['x'])
z12y = int(elem.attrib['y'])
deleteZoom(z12z, z12x, z12y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment