11 # projection object to go from latlon -> spherical mercator
12 PROJ = Proj4::Projection.new(["+proj=merc", "+a=6378137", "+b=6378137",
13 "+lat_ts=0.0", "+lon_0=0.0", "+x_0=0.0",
14 "+y_0=0", "+k=1.0", "+units=m",
15 "+nadgrids=@null", "+no_defs +over"])
17 # width/height of the spherical mercator projection
19 # the size of the meta tile blocks
21 # the directory root for meta tiles
22 HASH_ROOT = "/tiles/default/"
23 # lowest zoom that we want to expire
26 # highest zoom that we want to expire
33 DBTABLE="planet_osm_nodes"
35 # turns a spherical mercator coord into a tile coord
36 def Expire.tile_from_merc(point, zoom)
37 # renormalise into unit space [0,1]
38 point.x = 0.5 + point.x / SIZE
39 point.y = 0.5 - point.y / SIZE
40 # transform into tile space
41 point.x = point.x * 2 ** zoom
42 point.y = point.y * 2 ** zoom
43 # chop of the fractional parts
44 [point.x.to_int, point.y.to_int, zoom]
47 # turns a latlon -> tile x,y given a zoom level
48 def Expire.tile_from_latlon(latlon, zoom)
49 # first convert to spherical mercator
50 point = PROJ.forward(latlon)
51 tile_from_merc(point, zoom)
54 # this must match the definition of xyz_to_meta in mod_tile
55 def Expire.xyz_to_meta(root, x, y, z)
56 # mask off the final few bits
60 hash_path = (0..4).collect { |i|
61 (((x >> 4*i) & 0xf) << 4) | ((y >> 4*i) & 0xf)
63 root + '/' + z.to_s + '/' + hash_path + ".meta"
66 # time to reset to, some very stupidly early time, before OSM started
67 EXPIRY_TIME = Time.parse("2000-01-01 00:00:00")
69 # expire the meta tile by setting the modified time back
70 def Expire.expire_meta(meta)
71 puts "Expiring #{meta}"
72 File.utime(EXPIRY_TIME, EXPIRY_TIME, meta)
75 def Expire.expire(change_file)
76 do_expire(change_file) do |set|
80 # turn all the tiles into expires, putting them in the set
81 # so that we don't expire things multiple times
83 # this has to match the routine in mod_tile
84 meta = xyz_to_meta(HASH_ROOT, xy[0], xy[1], xy[2])
86 meta_set.add(meta) if File.exist? meta
88 # add the parent into the set for the next round
89 new_set.add([xy[0] / 2, xy[1] / 2, xy[2] - 1])
92 # expire all meta tiles
93 meta_set.each do |meta|
97 # return the new set, consisting of all the parents
102 def Expire.do_expire(change_file, &block)
103 # read in the osm change file
104 doc = XML::Document.file(change_file)
106 # hash map to contain all the nodes
109 # we put all the nodes into the hash, as it doesn't matter whether the node was
110 # added, deleted or modified - the tile will need updating anyway.
111 doc.find('//node').each do |node|
112 lat = node['lat'].to_f
119 point = Proj4::Point.new(Math::PI * node['lon'].to_f / 180,
120 Math::PI * lat / 180)
121 nodes[node['id'].to_i] = tile_from_latlon(point, MAX_ZOOM)
124 # now we look for all the ways that have changed and put all of their nodes into
125 # the hash too. this will add too many nodes, as it is possible a long way will be
126 # changed at only a portion of its length. however, due to the non-local way that
127 # mapnik does text placement, it may stil not be enough.
129 # also, we miss cases where nodes are deleted from ways where that node is not
130 # itself deleted and the coverage of the point set isn't enough to encompass the
132 conn = PG::Connection.new(:host => DBHOST, :port => DBPORT, :dbname => DBNAME)
133 doc.find('//way/nd').each do |node|
134 node_id = node['ref'].to_i
135 unless nodes.include? node_id
136 # this is a node referenced but not added, modified or deleted, so it should
137 # still be in the postgis DB.
138 res = conn.query("select lon, lat from #{DBTABLE} where id=#{node_id};")
140 # loop over results, adding tiles to the change set
142 point = Proj4::Point.new(row[0].to_f / 100.0, row[1].to_f / 100.0)
143 nodes[node_id] = tile_from_merc(point, MAX_ZOOM)
151 # create a set of all the tiles at the maximum zoom level which are touched by
152 # any of the nodes we've collected. we'll create the tiles at other zoom levels
153 # by a simple recursion.
154 set = Set.new nodes.values
156 # expire tiles and shrink to the set of parents
157 (MAX_ZOOM).downto(MIN_ZOOM) do |z|
158 # allow the block to work on the set, returning the set at the next