]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changes_controller.rb
Merge remote-tracking branch 'upstream/pull/1926'
[rails.git] / app / controllers / api / changes_controller.rb
1 module Api
2   class ChangesController < ApiController
3     authorize_resource :class => false
4
5     before_action :check_api_readable
6     around_action :api_call_handle_error, :api_call_timeout
7
8     # Get a list of the tiles that have changed within a specified time
9     # period
10     def index
11       zoom = (params[:zoom] || "12").to_i
12
13       if params.include?(:start) && params.include?(:end)
14         starttime = Time.parse(params[:start])
15         endtime = Time.parse(params[:end])
16       else
17         hours = (params[:hours] || "1").to_i.hours
18         endtime = Time.now.getutc
19         starttime = endtime - hours
20       end
21
22       if zoom >= 1 && zoom <= 16 &&
23          endtime > starttime && endtime - starttime <= 24.hours
24         mask = (1 << zoom) - 1
25
26         tiles = Node.where(:timestamp => starttime..endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count
27
28         doc = OSM::API.new.get_xml_doc
29         changes = XML::Node.new "changes"
30         changes["starttime"] = starttime.xmlschema
31         changes["endtime"] = endtime.xmlschema
32
33         tiles.each do |tile, count|
34           x = (tile.to_i >> zoom) & mask
35           y = tile.to_i & mask
36
37           t = XML::Node.new "tile"
38           t["x"] = x.to_s
39           t["y"] = y.to_s
40           t["z"] = zoom.to_s
41           t["changes"] = count.to_s
42
43           changes << t
44         end
45
46         doc.root << changes
47
48         render :xml => doc.to_s
49       else
50         render :plain => "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", :status => :bad_request
51       end
52     end
53   end
54 end