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