Class: GpxRuby::Gpx::Parsers::TrackParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_ruby/gpx/parsers/track_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(gpx_node) ⇒ TrackParser

Returns a new instance of TrackParser.



9
10
11
# File 'lib/gpx_ruby/gpx/parsers/track_parser.rb', line 9

def initialize(gpx_node)
  @gpx_node = gpx_node
end

Instance Method Details

#parseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gpx_ruby/gpx/parsers/track_parser.rb', line 13

def parse
  tracks = []
  if track_nodes = @gpx_node.xpath('./xmlns:trk')
    # iterate over all tracks
    track_nodes.each do |trk|
      trk_segments = trk.xpath('./xmlns:trkseg')

      # map track segments for this track
      track_segments = trk_segments.map do |trkseg|

        # map all track points for this track segment
        track_points = trkseg.xpath('./xmlns:trkpt').map do |trkpt|

          lat = trkpt.at_xpath('./@lat').value.to_f
          lon = trkpt.at_xpath('./@lon').value.to_f

          time = begin
            DateTime.xmlschema(trkpt.at_xpath('./xmlns:time').text).to_time
          rescue
            nil
          end

          elevation =  begin
            trkpt.at_xpath('./xmlns:ele').text.to_f
          rescue
            nil
          end

          Track::Point.new lat: lat, lon: lon, time: time, elevation: elevation
        end

        Track::Segment.new track_points
      end

      trk_name = begin
        trk.at_xpath('./xmlns:name').text
      rescue
        nil
      end

      trk_cmt = begin
        trk.at_xpath('./xmlns:cmt').text
      rescue
        nil
      end

      trk_src = begin
        trk.at_xpath('./xmlns:src').text
      rescue
        nil
      end

      trk_desc = begin
        trk.at_xpath('./xmlns:desc').text
      rescue
        nil
      end

      trk_number = begin
        trk.at_xpath('./xmlns:number').text
      rescue
        nil
      end

      trk_url = begin
        trk.at_xpath('./xmlns:url').text
      rescue
        nil
      end

      trk_url_name = begin
        trk.at_xpath('./xmlns:urlname').text
      rescue
        nil
      end

      trk_time = begin
        DateTime.xmlschema(trk.at_xpath('./xmlns:time').text).to_time
      rescue
        nil
      end

      track_hash = {
          segments: track_segments,
          name: trk_name,
          time: trk_time,
          source: trk_src,
          description: trk_desc,
          number: trk_number,
          comment: trk_cmt,
          url: trk_url,
          url_name: trk_url_name
      }

      tracks << Track.new(track_hash)
    end

  end

  tracks
end