Skip to content

Instantly share code, notes, and snippets.

@andrewharvey
Created August 25, 2011 12:13
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 andrewharvey/1170520 to your computer and use it in GitHub Desktop.
Save andrewharvey/1170520 to your computer and use it in GitHub Desktop.
Expires a TileCache Disk cache given an expired tiles list from osm2pgsql
#!/usr/bin/perl -w
# This script was written to expire tiles from a TileCache Disk Cache by
# deleting them given a list of tiles to expire as generated by osm2pgsql.
#
# Author: Andrew Harvey <andrew.harvey4@gmail.com>
# License: CC0 http://creativecommons.org/publicdomain/zero/1.0/
#
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
use strict;
# function declarations
sub xyz_to_diskcache($$$);
if (defined $ARGV[0] && (($ARGV[0] eq "--help") || ($ARGV[0] eq "-h"))) {
print("Usage: $0 expired-tiles-list tilecache-cache-directory [debug]\n");
print("\n");
print("The expired-tiles-list file should contain one tile reference per line.\n e.g. '17/120518/78742'\n");
print("The debug option if present will make output verbose, and will not remove the\nexpired-tile-list after cleaning the tile cache\n");
exit 0;
}
# read in program arguments
my $expiry_list_filename = $ARGV[0];
my $tile_dir = $ARGV[1];
my $debug = $ARGV[2];
# check arguments given and valid file and directory
if (!(defined $expiry_list_filename &&
defined $tile_dir)) {
print("Usage: $0 expired-tiles-list tilecache-cache-directory [debug]\n");
exit 1;
}
if (!(-r $expiry_list_filename)) {
print("$expiry_list_filename not readable\n");
exit 2;
}
if (!(-d $tile_dir )) {
print ("$tile_dir not found\n");
exit 3;
}
if (defined $debug) {
print("Debug mode: on\n\n");
}
# if directory doesn't have a trailing / add one
if ($tile_dir !~ /\/$/) {
$tile_dir .= '/';
}
my $num_dirty = 0;
my @files_to_unlink;
# try to open the expired tiles list
open EXPIRED_TILES, "<$expiry_list_filename" or die;
while ( my $line = <EXPIRED_TILES>) {
# for lines that match the expected format
if ($line =~ /^[0-9]+\/[0-9]+\/[0-9]+$/) {
# convert xyz to the TileCache disk cache relative tile location
my ($z, $x, $y) = split /\//, $line;
my $ondisk_filename = $tile_dir . xyz_to_diskcache($x, $y, $z);
push @files_to_unlink, $ondisk_filename;
if (defined $debug) {
print "$line => $ondisk_filename\n";
}
$num_dirty++;
}else{
die("We found an entry in $expiry_list_filename which doesn't look right:\n$line\n");
}
}
close EXPIRED_TILES;
# now actually remove the files from disk and print some statistics
print "Marking up to $num_dirty tiles as dirty...";
my $num_cleaned = unlink @files_to_unlink;
if ($num_cleaned > 0) {
print " cleaned $num_cleaned (".sprintf("%.2f", $num_cleaned * 100 / $num_dirty)."%).\n";
}else{
print " 0 cleaned.\n";
}
# try to remove the expired-tiles-list as we have now cleaned these tiles
if (!defined $debug) {
print "Removing expired-tiles-list...";
if ((unlink $expiry_list_filename) == 1) {
print " ok.\n";
}else{
print " fail.\n";
}
}
# function to convert from OSM Z/X/Y.png to TileCache Disk Cache file
sub xyz_to_diskcache($$$) {
my ($x, $y, $z) = @_;
# flip y as TileCache stores on disk in TMS scheme
$y = (2**$z) - $y - 1;
# the formula is from http://svn.osgeo.org/tilecache/trunk/tilecache/TileCache/Caches/Disk.py
my @components = (
sprintf("%02d", $z),
sprintf("%03d", int($x / 1000000)),
sprintf("%03d", int($x / 1000) % 1000),
sprintf("%03d", int($x) % 1000),
sprintf("%03d", int($y / 1000000)),
sprintf("%03d", int($y / 1000) % 1000),
sprintf("%03d", int($y) % 1000));
my $filename = join('/', @components) . ".png";
return $filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment