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