]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/tracepoints_controller.rb
Merge remote-tracking branch 'upstream/pull/4222'
[rails.git] / app / controllers / api / tracepoints_controller.rb
1 module Api
2   class TracepointsController < ApiController
3     before_action :check_api_readable
4
5     authorize_resource
6
7     around_action :api_call_handle_error, :api_call_timeout
8
9     # Get an XML response containing a list of tracepoints that have been uploaded
10     # within the specified bounding box, and in the specified page.
11     def index
12       # retrieve the page number
13       page = params["page"].to_s.to_i
14
15       unless page >= 0
16         report_error("Page number must be greater than or equal to 0")
17         return
18       end
19
20       offset = page * Settings.tracepoints_per_page
21
22       # Figure out the bbox
23       # check boundary is sane and area within defined
24       # see /config/application.yml
25       begin
26         raise OSM::APIBadUserInput, "The parameter bbox is required" unless params[:bbox]
27
28         bbox = BoundingBox.from_bbox_params(params)
29         bbox.check_boundaries
30         bbox.check_size
31       rescue StandardError => e
32         report_error(e.message)
33         return
34       end
35
36       # get all the points
37       ordered_points = Tracepoint.bbox(bbox).joins(:trace).where(:gpx_files => { :visibility => %w[trackable identifiable] }).order("gpx_id DESC, trackid ASC, timestamp ASC")
38       unordered_points = Tracepoint.bbox(bbox).joins(:trace).where(:gpx_files => { :visibility => %w[public private] }).order("gps_points.latitude", "gps_points.longitude", "gps_points.timestamp")
39       @points = ordered_points.union_all(unordered_points).offset(offset).limit(Settings.tracepoints_per_page).preload(:trace)
40
41       response.headers["Content-Disposition"] = "attachment; filename=\"tracks.gpx\""
42
43       render :formats => [:gpx]
44     end
45   end
46 end