]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tile/files/default/ruby/expire.rb
Tile: add missing crossdomain.xml + robots.txt
[chef.git] / cookbooks / tile / files / default / ruby / expire.rb
1 #!/usr/bin/ruby
2
3 require 'rubygems'
4 require 'proj4'
5 require 'xml/libxml'
6 require 'set'
7 require 'pg'
8 require 'time'
9
10 module Expire
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"])
16   
17   # width/height of the spherical mercator projection
18   SIZE=40075016.6855784
19   # the size of the meta tile blocks
20   METATILE = 8
21   # the directory root for meta tiles
22   HASH_ROOT = "/tiles/default/"
23   # database parameters
24   DBNAME="gis"
25   DBHOST=""
26   #DBPORT=5432
27   DBPORT=5432
28   DBTABLE="planet_osm_nodes"
29   
30   # turns a spherical mercator coord into a tile coord
31   def Expire.tile_from_merc(point, zoom)
32     # renormalise into unit space [0,1]
33     point.x = 0.5 + point.x / SIZE
34     point.y = 0.5 - point.y / SIZE
35     # transform into tile space
36     point.x = point.x * 2 ** zoom
37     point.y = point.y * 2 ** zoom
38     # chop of the fractional parts
39     [point.x.to_int, point.y.to_int, zoom]
40   end
41   
42   # turns a latlon -> tile x,y given a zoom level
43   def Expire.tile_from_latlon(latlon, zoom)
44     # first convert to spherical mercator
45     point = PROJ.forward(latlon)
46     tile_from_merc(point, zoom)
47   end
48   
49   # this must match the definition of xyz_to_meta in mod_tile
50   def Expire.xyz_to_meta(x, y, z)
51     # mask off the final few bits
52     x &= ~(METATILE - 1)
53     y &= ~(METATILE - 1)
54     # generate the path
55     hash_path = (0..4).collect { |i| 
56       (((x >> 4*i) & 0xf) << 4) | ((y >> 4*i) & 0xf) 
57     }.reverse.join('/')
58     z.to_s + '/' + hash_path + ".meta"
59   end
60   
61   # time to reset to, some very stupidly early time, before OSM started
62   EXPIRY_TIME = Time.parse("2000-01-01 00:00:00")
63
64   # expire the meta tile by setting the modified time back 
65   def Expire.expire_meta(meta)
66     puts "Expiring #{meta}"
67     File.utime(EXPIRY_TIME, EXPIRY_TIME, meta)
68   end
69   
70   def Expire.expire(change_file, min_zoom, max_zoom, tile_dirs)
71     do_expire(change_file, min_zoom, max_zoom) do |set|
72       new_set = Set.new
73       meta_set = Set.new
74
75       # turn all the tiles into expires, putting them in the set
76       # so that we don't expire things multiple times
77       set.each do |xy|
78         # this has to match the routine in mod_tile
79         meta = xyz_to_meta(xy[0], xy[1], xy[2])
80
81         # check each style working out what needs expiring
82         tile_dirs.each do |tile_dir|
83           meta_set.add(tile_dir + "/" + meta) if File.exist?(tile_dir + "/" + meta)
84         end
85         
86         # add the parent into the set for the next round
87         new_set.add([xy[0] / 2, xy[1] / 2, xy[2] - 1])
88       end
89       
90       # expire all meta tiles
91       meta_set.each do |meta|
92         expire_meta(meta)
93       end
94
95       # return the new set, consisting of all the parents
96       new_set
97     end
98   end
99
100   def Expire.do_expire(change_file, min_zoom, max_zoom, &block)
101     # read in the osm change file
102     doc = XML::Document.file(change_file)
103     
104     # hash map to contain all the nodes
105     nodes = Hash.new
106     
107     # we put all the nodes into the hash, as it doesn't matter whether the node was
108     # added, deleted or modified - the tile will need updating anyway.
109     doc.find('//node').each do |node|
110       lat = node['lat'].to_f
111       if lat < -85
112         lat = -85
113       end
114       if lat > 85
115         lat = 85
116       end
117       point = Proj4::Point.new(Math::PI * node['lon'].to_f / 180, 
118                                Math::PI * lat / 180)
119       nodes[node['id'].to_i] = tile_from_latlon(point, max_zoom)
120     end
121     
122     # now we look for all the ways that have changed and put all of their nodes into
123     # the hash too. this will add too many nodes, as it is possible a long way will be
124     # changed at only a portion of its length. however, due to the non-local way that
125     # mapnik does text placement, it may stil not be enough.
126     #
127     # also, we miss cases where nodes are deleted from ways where that node is not 
128     # itself deleted and the coverage of the point set isn't enough to encompass the
129     # change.
130     conn = PG::Connection.new(:host => DBHOST, :port => DBPORT, :dbname => DBNAME)
131     doc.find('//way/nd').each do |node|
132       node_id = node['ref'].to_i
133       unless nodes.include? node_id
134         # this is a node referenced but not added, modified or deleted, so it should
135         # still be in the postgis DB.
136         res = conn.query("select lon, lat from #{DBTABLE} where id=#{node_id};")
137         
138         # loop over results, adding tiles to the change set
139         res.each do |row|
140           point = Proj4::Point.new(row[0].to_f / 100.0, row[1].to_f / 100.0)
141           nodes[node_id] = tile_from_merc(point, max_zoom)
142         end
143
144         # Discard results
145         res.clear
146       end
147     end
148     
149     # create a set of all the tiles at the maximum zoom level which are touched by 
150     # any of the nodes we've collected. we'll create the tiles at other zoom levels
151     # by a simple recursion.
152     set = Set.new nodes.values
153     
154     # expire tiles and shrink to the set of parents
155     (max_zoom).downto(min_zoom) do |z|
156       # allow the block to work on the set, returning the set at the next
157       # zoom level
158       set = yield set
159     end
160   end
161 end