]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tile/files/default/bin/expire-tiles-single
kitchen: pre-cache frequent apt packages in container
[chef.git] / cookbooks / tile / files / default / bin / expire-tiles-single
1 #!/usr/bin/python3
2 """
3 Expire meta tiles from a OSM change file by resetting their modified time.
4 """
5
6 import argparse
7 import os
8 import osmium as o
9 import pyproj
10
11 EXPIRY_TIME = 946681200 # 2000-01-01 00:00:00
12 # width/height of the spherical mercator projection
13 SIZE = 40075016.6855784
14
15 proj_transformer = pyproj.Transformer.from_crs('epsg:4326', 'epsg:3857', always_xy = True)
16
17 class TileCollector(o.SimpleHandler):
18
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()
23         self.tile_set = set()
24         self.zoom = zoom
25
26     def add_tile_from_node(self, location):
27         if not location.valid():
28             return
29
30         lat = max(-85, min(85.0, location.lat))
31         x, y = proj_transformer.transform(location.lon, lat)
32
33         # renormalise into unit space [0,1]
34         x = 0.5 + x / SIZE
35         y = 0.5 - y / SIZE
36         # transform into tile space
37         x = x * 2**self.zoom
38         y = y * 2**self.zoom
39         # chop of the fractional parts
40         self.tile_set.add((int(x), int(y), self.zoom))
41
42     def node(self, node):
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)
47
48     def way(self, way):
49         for n in way.nodes:
50             if not n.ref in self.done_nodes:
51                 self.done_nodes.add(n.ref)
52                 try:
53                     self.add_tile_from_node(self.node_cache.get(n.ref))
54                 except KeyError:
55                     pass # no coordinate
56
57
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.
61     """
62     # mask off the final few bits
63     x = x & ~(meta_size - 1)
64     y = y & ~(meta_size - 1)
65
66     # generate the path
67     path = None
68     for i in range(0, 5):
69         part = str(((x & 0x0f) << 4) | (y & 0x0f))
70         x = x >> 4
71         y = y >> 4
72         if path is None:
73             path = (part + ".meta")
74         else:
75             path = os.path.join(part, path)
76
77     return os.path.join(str(z), path)
78
79
80 def expire_meta(meta):
81     """Expire the meta tile by setting the modified time back.
82     """
83     if os.path.exists(meta):
84         print("Expiring " + meta)
85         os.utime(meta, (EXPIRY_TIME, EXPIRY_TIME))
86
87
88 def expire_meta_tiles(options):
89     proc = TileCollector(options.node_cache, options.max_zoom)
90     proc.apply_file(options.inputfile)
91
92     tile_set = proc.tile_set
93
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):
97         meta_set = set()
98         new_set = set()
99         for xy in tile_set:
100             meta = xyz_to_meta(xy[0], xy[1], xy[2], options.meta_size)
101
102             for tile_dir in options.tile_dir:
103                 meta_set.add(os.path.join(tile_dir, meta))
104
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))
107
108         # expire all meta tiles
109         for meta in meta_set:
110             expire_meta(meta)
111
112         # continue with parent tiles
113         tile_set = new_set
114
115 if __name__ == '__main__':
116
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,
121                         type=int,
122                         help='Minimum zoom for expiry.')
123     parser.add_argument('--max', action='store', dest='max_zoom', default=20,
124                         type=int,
125                         help='Maximum zoom for expiry.')
126     parser.add_argument('-t', action='append', dest='tile_dir', default=None,
127                         required=True,
128                         help='Tile directory (repeat for multiple directories).')
129     parser.add_argument('--meta-tile-size', action='store', dest='meta_size',
130                         default=8, type=int,
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.')
137
138     options = parser.parse_args()
139
140     expire_meta_tiles(options)