3 Expire meta tiles from a OSM change file by resetting their modified time.
11 EXPIRY_TIME = 946681200 # 2000-01-01 00:00:00
12 # width/height of the spherical mercator projection
13 SIZE = 40075016.6855784
15 proj_wsg84 = pyproj.Proj(init='epsg:4326')
16 proj_merc = pyproj.Proj(init='epsg:3857')
18 class TileCollector(o.SimpleHandler):
20 def __init__(self, node_cache, zoom):
21 super(TileCollector, self).__init__()
22 self.node_cache = o.index.create_map("dense_file_array," + node_cache)
23 self.done_nodes = set()
27 def add_tile_from_node(self, location):
28 if not location.valid():
31 lat = max(-85, min(85.0, location.lat))
32 x, y = pyproj.transform(proj_wsg84, proj_merc, location.lon, lat)
34 # renormalise into unit space [0,1]
37 # transform into tile space
40 # chop of the fractional parts
41 self.tile_set.add((int(x), int(y), self.zoom))
44 # we put all the nodes into the hash, as it doesn't matter whether the node was
45 # added, deleted or modified - the tile will need updating anyway.
46 self.done_nodes.add(node.id)
47 self.add_tile_from_node(node.location)
51 if not n.ref in self.done_nodes:
52 self.done_nodes.add(n.ref)
54 self.add_tile_from_node(self.node_cache.get(n.ref))
55 except o.NotFoundError:
59 def xyz_to_meta(x, y, z, meta_size):
60 """ Return the file name of a meta tile.
61 This must match the definition of xyz to meta in mod_tile.
63 # mask off the final few bits
64 x = x & ~(meta_size - 1)
65 y = y & ~(meta_size - 1)
70 part = str(((x & 0x0f) << 4) | (y & 0x0f))
74 path = (part + ".meta")
76 path = os.path.join(part, path)
78 return os.path.join(str(z), path)
81 def expire_meta(meta):
82 """Expire the meta tile by setting the modified time back.
84 if os.path.exists(meta):
85 print("Expiring " + meta)
86 os.utime(meta, (EXPIRY_TIME, EXPIRY_TIME))
89 def expire_meta_tiles(options):
90 proc = TileCollector(options.node_cache, options.max_zoom)
91 proc.apply_file(options.inputfile)
93 tile_set = proc.tile_set
95 # turn all the tiles into expires, putting them in the set
96 # so that we don't expire things multiple times
97 for z in range(options.min_zoom, options.max_zoom + 1):
101 meta = xyz_to_meta(xy[0], xy[1], xy[2], options.meta_size)
103 for tile_dir in options.tile_dir:
104 meta_set.add(os.path.join(tile_dir, meta))
106 # add the parent into the set for the next round
107 new_set.add((int(xy[0]/2), int(xy[1]/2), xy[2] - 1))
109 # expire all meta tiles
110 for meta in meta_set:
113 # continue with parent tiles
116 if __name__ == '__main__':
118 parser = argparse.ArgumentParser(description=__doc__,
119 formatter_class=argparse.RawDescriptionHelpFormatter,
120 usage='%(prog)s [options] <inputfile>')
121 parser.add_argument('--min', action='store', dest='min_zoom', default=13,
123 help='Minimum zoom for expiry.')
124 parser.add_argument('--max', action='store', dest='max_zoom', default=20,
126 help='Maximum zoom for expiry.')
127 parser.add_argument('-t', action='append', dest='tile_dir', default=None,
129 help='Tile directory (repeat for multiple directories).')
130 parser.add_argument('--meta-tile-size', action='store', dest='meta_size',
132 help='The size of the meta tile blocks.')
133 parser.add_argument('--node-cache', action='store', dest='node_cache',
134 default='/store/database/nodes',
135 help='osm2pgsql flatnode file.')
136 parser.add_argument('inputfile',
137 help='OSC input file.')
139 options = parser.parse_args()
141 expire_meta_tiles(options)