Class: GPX::Track

Inherits:
Base
  • Object
show all
Defined in:
lib/gpx/track.rb

Overview

In GPX, a single Track can hold multiple Segments, each of which hold multiple points (in this library, those points are instances of TrackPoint). Each instance of this class has its own meta-data, including low point, high point, and distance. Of course, each track references an array of the segments that comprise it, but additionally each track holds a reference to all of its points as one big array called “points”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#instantiate_with_text_elements

Constructor Details

#initialize(opts = {}) ⇒ Track

Initialize a track from a XML::Node, or, if no :element option is passed, initialize a blank Track object.



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
# File 'lib/gpx/track.rb', line 16

def initialize(opts = {})
  super()
  @gpx_file = opts[:gpx_file]
  @segments = []
  @points = []
  

  return unless opts[:element]

  trk_element = opts[:element]
  @name = (
  begin
    trk_element.at('name').inner_text
  rescue StandardError
    ''
  end)
  @comment = (
  begin
    trk_element.at('cmt').inner_text
  rescue StandardError
    ''
  end)
  @description = (
  begin
    trk_element.at('desc').inner_text
  rescue StandardError
    ''
  end)
  trk_element.search('trkseg').each do |seg_element|
    seg = Segment.new(element: seg_element, track: self, gpx_file: @gpx_file)
    append_segment(seg)
  end
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



11
12
13
# File 'lib/gpx/track.rb', line 11

def bounds
  @bounds
end

#commentObject

Returns the value of attribute comment.



12
13
14
# File 'lib/gpx/track.rb', line 12

def comment
  @comment
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/gpx/track.rb', line 12

def description
  @description
end

#distanceObject (readonly)

Returns the value of attribute distance.



11
12
13
# File 'lib/gpx/track.rb', line 11

def distance
  @distance
end

#gpx_fileObject

Returns the value of attribute gpx_file.



12
13
14
# File 'lib/gpx/track.rb', line 12

def gpx_file
  @gpx_file
end

#highest_pointObject (readonly)

Returns the value of attribute highest_point.



11
12
13
# File 'lib/gpx/track.rb', line 11

def highest_point
  @highest_point
end

#lowest_pointObject (readonly)

Returns the value of attribute lowest_point.



11
12
13
# File 'lib/gpx/track.rb', line 11

def lowest_point
  @lowest_point
end

#moving_durationObject (readonly)

Returns the value of attribute moving_duration.



11
12
13
# File 'lib/gpx/track.rb', line 11

def moving_duration
  @moving_duration
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/gpx/track.rb', line 12

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



11
12
13
# File 'lib/gpx/track.rb', line 11

def points
  @points
end

#segmentsObject

Returns the value of attribute segments.



12
13
14
# File 'lib/gpx/track.rb', line 12

def segments
  @segments
end

Instance Method Details

#append_segment(seg) ⇒ Object

Append a segment to this track, updating its meta data along the way.



51
52
53
54
55
56
# File 'lib/gpx/track.rb', line 51

def append_segment(seg)
  return if seg.points.empty?

  (seg)
  @segments << seg
end

#closest_point(time) ⇒ Object

Finds the closest point (to “time”) within this track. Useful for correlating things like pictures, video, and other events, if you are working with a timestamp.



69
70
71
72
# File 'lib/gpx/track.rb', line 69

def closest_point(time)
  segment = segments.select { |s| s.contains_time?(time) }
  segment.first
end

#contains_time?(time) ⇒ Boolean

Returns true if the given time occurs within any of the segments of this track.

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/gpx/track.rb', line 59

def contains_time?(time)
  segments.each do |seg|
    return true if seg.contains_time?(time)
  end
  false
end

#crop(area) ⇒ Object

Removes all points outside of a given area and updates the meta data. The “area” paremeter is usually a Bounds object.



76
77
78
79
80
81
82
83
# File 'lib/gpx/track.rb', line 76

def crop(area)
  
  segments.each do |seg|
    seg.crop(area)
    (seg) unless seg.empty?
  end
  segments.delete_if(&:empty?)
end

#delete_area(area) ⇒ Object

Deletes all points within a given area and updates the meta data.



86
87
88
89
90
91
92
93
# File 'lib/gpx/track.rb', line 86

def delete_area(area)
  
  segments.each do |seg|
    seg.delete_area(area)
    (seg) unless seg.empty?
  end
  segments.delete_if(&:empty?)
end

#empty?Boolean

Returns true if this track has no points in it. This should return true even when the track has empty segments.

Returns:

  • (Boolean)


97
98
99
# File 'lib/gpx/track.rb', line 97

def empty?
  points.nil? || points.empty?
end

#recalculate_distanceObject



119
120
121
122
123
124
# File 'lib/gpx/track.rb', line 119

def recalculate_distance
  @distance = 0
  @segments.each do |seg|
    @distance += seg.distance
  end
end

#to_sObject

Prints out a friendly summary of this track (sans points). Useful for debugging and sanity checks.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gpx/track.rb', line 104

def to_s
  result = "Track \n"
  result << "\tName: #{name}\n"
  result << "\tComment: #{comment}\n"
  result << "\tDescription: #{description}\n"
  result << "\tSize: #{points.size} points\n"
  result << "\tSegments: #{segments.size} \n"
  result << "\tDistance: #{distance} km\n"
  result << "\tMoving duration: #{moving_duration} km\n"
  result << "\tLowest Point: #{lowest_point.elevation} \n"
  result << "\tHighest Point: #{highest_point.elevation}\n "
  result << "\tBounds: #{bounds}"
  result
end