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