Skip to content

Instantly share code, notes, and snippets.

@iandees
Created October 8, 2011 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandees/1271803 to your computer and use it in GitHub Desktop.
Save iandees/1271803 to your computer and use it in GitHub Desktop.
A tool to parse the OSM API's list of expired z12 tiles and delete the corresponding tiles in a TileStache disk cache.
#!/bin/python
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 http://www.openstreetmap.org/api/0.6/changes
maxZoom = 19
if len(sys.argv) < 2:
print 'Missing tile cache root path argument.'
sys.exit(1)
rootPath = sys.argv[1]
urlstream = urllib2.urlopen('http://www.openstreetmap.org/api/0.6/changes')
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,
z,
xStr[0:3], xStr[3:6], xStr[6:9],
yStr[0:3], yStr[3:6], yStr[6:9])
print path
if z == maxZoom:
return
# 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)
elem.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment