]> git.openstreetmap.org Git - rails.git/blob - app/views/api/tracepoints/index.gpx.builder
Merge remote-tracking branch 'upstream/pull/5375'
[rails.git] / app / views / api / tracepoints / index.gpx.builder
1 xml.instruct!
2
3 xml.gpx("version" => "1.0",
4         "creator" => "OpenStreetMap.org",
5         "xmlns" => "http://www.topografix.com/GPX/1/0") do
6   # initialise these variables outside of the loop so that they
7   # stay in scope and don't get free'd up by the GC during the
8   # loop.
9   gpx_id = -1
10   trackid = -1
11   tracks = []
12   track = nil
13   trkseg = nil
14   anon_track = nil
15   anon_trkseg = nil
16
17   @points.each do |point|
18     if gpx_id != point.gpx_id
19       gpx_id = point.gpx_id
20       trackid = -1
21
22       if point.trace.trackable?
23         track = {}
24         track["trksegs"] = []
25         tracks << track
26
27         if point.trace.identifiable?
28           track["name"] = point.trace.name
29           track["desc"] = point.trace.description
30           track["url"] = url_for(:controller => "/traces", :action => "show", :display_name => point.trace.user.display_name, :id => point.trace.id)
31         end
32       else
33         # use the anonymous track segment if the user hasn't allowed
34         # their GPX points to be tracked.
35         if anon_track.nil?
36           anon_track = {}
37           anon_track["trksegs"] = []
38           tracks << anon_track
39         end
40         track = anon_track
41       end
42     end
43
44     if trackid != point.trackid
45       if point.trace.trackable?
46         trkseg = []
47         track["trksegs"] << trkseg
48         trackid = point.trackid
49       else
50         if anon_trkseg.nil?
51           anon_trkseg = []
52           anon_track["trksegs"] << anon_trkseg
53         end
54         trkseg = anon_trkseg
55       end
56     end
57
58     trkseg << point
59   end
60
61   tracks.each do |trk|
62     xml.trk do
63       if trk.key?("name")
64         xml.name trk["name"]
65         xml.desc trk["desc"]
66         xml.url trk["url"]
67       end
68       trk["trksegs"].each do |trksg|
69         xml.trkseg do
70           trksg.each do |tracepoint|
71             xml.trkpt("lat" => tracepoint.lat.to_s, "lon" => tracepoint.lon.to_s) do
72               xml.time tracepoint.timestamp.xmlschema if tracepoint.trace.trackable?
73             end
74           end
75         end
76       end
77     end
78   end
79 end