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