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 if os.path.exists(meta):
84 print("Expiring " + meta)
85 os.utime(meta, (EXPIRY_TIME, EXPIRY_TIME))
88 def expire_meta_tiles(options):
89 proc = TileCollector(options.node_cache, options.max_zoom)
90 proc.apply_file(options.inputfile)
92 tile_set = proc.tile_set
94 # turn all the tiles into expires, putting them in the set
95 # so that we don't expire things multiple times
96 for z in range(options.min_zoom, options.max_zoom + 1):
100 meta = xyz_to_meta(xy[0], xy[1], xy[2], options.meta_size)
102 for tile_dir in options.tile_dir:
103 meta_set.add(os.path.join(tile_dir, meta))
105 # add the parent into the set for the next round
106 new_set.add((int(xy[0]/2), int(xy[1]/2), xy[2] - 1))
108 # expire all meta tiles
109 for meta in meta_set:
112 # continue with parent tiles
115 if __name__ == '__main__':
117 parser = argparse.ArgumentParser(description=__doc__,
118 formatter_class=argparse.RawDescriptionHelpFormatter,
119 usage='%(prog)s [options] <inputfile>')
120 parser.add_argument('--min', action='store', dest='min_zoom', default=13,
122 help='Minimum zoom for expiry.')
123 parser.add_argument('--max', action='store', dest='max_zoom', default=20,
125 help='Maximum zoom for expiry.')
126 parser.add_argument('-t', action='append', dest='tile_dir', default=None,
128 help='Tile directory (repeat for multiple directories).')
129 parser.add_argument('--meta-tile-size', action='store', dest='meta_size',
131 help='The size of the meta tile blocks.')
132 parser.add_argument('--node-cache', action='store', dest='node_cache',
133 default='/store/database/nodes',
134 help='osm2pgsql flatnode file.')
135 parser.add_argument('inputfile',
136 help='OSC input file.')
138 options = parser.parse_args()
140 expire_meta_tiles(options)