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_transformer = pyproj.Transformer.from_crs('epsg:4326', 'epsg:3857', always_xy = True)
17 class TileCollector(o.SimpleHandler):
19 def __init__(self, node_cache, zoom):
20 super(TileCollector, self).__init__()
21 self.node_cache = o.index.create_map("dense_file_array," + node_cache)
22 self.done_nodes = set()
26 def add_tile_from_node(self, location):
27 if not location.valid():
30 lat = max(-85, min(85.0, location.lat))
31 x, y = proj_transformer.transform(location.lon, lat)
33 # renormalise into unit space [0,1]
36 # transform into tile space
39 # chop of the fractional parts
40 self.tile_set.add((int(x), int(y), self.zoom))
43 # we put all the nodes into the hash, as it doesn't matter whether the node was
44 # added, deleted or modified - the tile will need updating anyway.
45 self.done_nodes.add(node.id)
46 self.add_tile_from_node(node.location)
50 if not n.ref in self.done_nodes:
51 self.done_nodes.add(n.ref)
53 self.add_tile_from_node(self.node_cache.get(n.ref))
58 def xyz_to_meta(x, y, z, meta_size):
59 """ Return the file name of a meta tile.
60 This must match the definition of xyz to meta in mod_tile.
62 # mask off the final few bits
63 x = x & ~(meta_size - 1)
64 y = y & ~(meta_size - 1)
69 part = str(((x & 0x0f) << 4) | (y & 0x0f))
73 path = (part + ".meta")
75 path = os.path.join(part, path)
77 return os.path.join(str(z), path)
80 def expire_meta(meta):
81 """Expire the meta tile by setting the modified time back.
83 exists = os.path.exists(meta)
85 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
111 for meta in meta_set:
112 if expire_meta(meta):
114 print("Expired {0} tiles at zoom {1}".format(expired, z))
116 # continue with parent tiles
119 if __name__ == '__main__':
121 parser = argparse.ArgumentParser(description=__doc__,
122 formatter_class=argparse.RawDescriptionHelpFormatter,
123 usage='%(prog)s [options] <inputfile>')
124 parser.add_argument('--min', action='store', dest='min_zoom', default=13,
126 help='Minimum zoom for expiry.')
127 parser.add_argument('--max', action='store', dest='max_zoom', default=20,
129 help='Maximum zoom for expiry.')
130 parser.add_argument('-t', action='append', dest='tile_dir', default=None,
132 help='Tile directory (repeat for multiple directories).')
133 parser.add_argument('--meta-tile-size', action='store', dest='meta_size',
135 help='The size of the meta tile blocks.')
136 parser.add_argument('--node-cache', action='store', dest='node_cache',
137 default='/store/database/nodes',
138 help='osm2pgsql flatnode file.')
139 parser.add_argument('inputfile',
140 help='OSC input file.')
142 options = parser.parse_args()
144 expire_meta_tiles(options)