2 class TracepointsController < ApiController
5 before_action :check_api_readable
6 around_action :api_call_handle_error, :api_call_timeout
8 # Get an XML response containing a list of tracepoints that have been uploaded
9 # within the specified bounding box, and in the specified page.
11 # retrieve the page number
12 page = params["page"].to_s.to_i
15 report_error("Page number must be greater than or equal to 0")
19 offset = page * Settings.tracepoints_per_page
22 # check boundary is sane and area within defined
23 # see /config/application.yml
25 bbox = BoundingBox.from_bbox_params(params)
28 rescue StandardError => e
29 report_error(e.message)
34 ordered_points = Tracepoint.bbox(bbox).joins(:trace).where(:gpx_files => { :visibility => %w[trackable identifiable] }).order("gpx_id DESC, trackid ASC, timestamp ASC")
35 unordered_points = Tracepoint.bbox(bbox).joins(:trace).where(:gpx_files => { :visibility => %w[public private] }).order("gps_points.latitude", "gps_points.longitude", "gps_points.timestamp")
36 points = ordered_points.union_all(unordered_points).offset(offset).limit(Settings.tracepoints_per_page)
38 doc = XML::Document.new
39 doc.encoding = XML::Encoding::UTF_8
40 root = XML::Node.new "gpx"
41 root["version"] = "1.0"
42 root["creator"] = "OpenStreetMap.org"
43 root["xmlns"] = "http://www.topografix.com/GPX/1/0"
47 # initialise these variables outside of the loop so that they
48 # stay in scope and don't get free'd up by the GC during the
59 points.each do |point|
60 if gpx_id != point.gpx_id
63 gpx_file = Trace.find(gpx_id)
65 if gpx_file.trackable?
66 track = XML::Node.new "trk"
70 if gpx_file.identifiable?
71 track << (XML::Node.new("name") << gpx_file.name)
72 track << (XML::Node.new("desc") << gpx_file.description)
73 track << (XML::Node.new("url") << url_for(:controller => "/traces", :action => "show", :display_name => gpx_file.user.display_name, :id => gpx_file.id))
76 # use the anonymous track segment if the user hasn't allowed
77 # their GPX points to be tracked.
80 anon_track = XML::Node.new "trk"
81 doc.root << anon_track
87 if trackid != point.trackid
88 if gpx_file.trackable?
89 trkseg = XML::Node.new "trkseg"
91 trackid = point.trackid
94 anon_trkseg = XML::Node.new "trkseg"
95 anon_track << anon_trkseg
101 trkseg << point.to_xml_node(:print_timestamp => timestamps)
104 response.headers["Content-Disposition"] = "attachment; filename=\"tracks.gpx\""
106 render :xml => doc.to_s