Class: GPX::Track
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
-
#bounds ⇒ Object
readonly
Returns the value of attribute bounds.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#description ⇒ Object
Returns the value of attribute description.
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#gpx_file ⇒ Object
Returns the value of attribute gpx_file.
-
#highest_point ⇒ Object
readonly
Returns the value of attribute highest_point.
-
#lowest_point ⇒ Object
readonly
Returns the value of attribute lowest_point.
-
#moving_duration ⇒ Object
readonly
Returns the value of attribute moving_duration.
-
#name ⇒ Object
Returns the value of attribute name.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#segments ⇒ Object
Returns the value of attribute segments.
Instance Method Summary collapse
-
#append_segment(seg) ⇒ Object
Append a segment to this track, updating its meta data along the way.
-
#closest_point(time) ⇒ Object
Finds the closest point (to “time”) within this track.
-
#contains_time?(time) ⇒ Boolean
Returns true if the given time occurs within any of the segments of this track.
-
#crop(area) ⇒ Object
Removes all points outside of a given area and updates the meta data.
-
#delete_area(area) ⇒ Object
Deletes all points within a given area and updates the meta data.
-
#empty? ⇒ Boolean
Returns true if this track has no points in it.
-
#initialize(opts = {}) ⇒ Track
constructor
Initialize a track from a XML::Node, or, if no :element option is passed, initialize a blank Track object.
- #recalculate_distance ⇒ Object
-
#to_s ⇒ Object
Prints out a friendly summary of this track (sans points).
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.
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 |
# File 'lib/gpx/track.rb', line 14 def initialize(opts = {}) @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
#bounds ⇒ Object (readonly)
Returns the value of attribute bounds.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def bounds @bounds end |
#comment ⇒ Object
Returns the value of attribute comment.
10 11 12 |
# File 'lib/gpx/track.rb', line 10 def comment @comment end |
#description ⇒ Object
Returns the value of attribute description.
10 11 12 |
# File 'lib/gpx/track.rb', line 10 def description @description end |
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def distance @distance end |
#gpx_file ⇒ Object
Returns the value of attribute gpx_file.
10 11 12 |
# File 'lib/gpx/track.rb', line 10 def gpx_file @gpx_file end |
#highest_point ⇒ Object (readonly)
Returns the value of attribute highest_point.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def highest_point @highest_point end |
#lowest_point ⇒ Object (readonly)
Returns the value of attribute lowest_point.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def lowest_point @lowest_point end |
#moving_duration ⇒ Object (readonly)
Returns the value of attribute moving_duration.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def moving_duration @moving_duration end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/gpx/track.rb', line 10 def name @name end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
9 10 11 |
# File 'lib/gpx/track.rb', line 9 def points @points end |
#segments ⇒ Object
Returns the value of attribute segments.
10 11 12 |
# File 'lib/gpx/track.rb', line 10 def segments @segments end |
Instance Method Details
#append_segment(seg) ⇒ Object
Append a segment to this track, updating its meta data along the way.
45 46 47 48 49 |
# File 'lib/gpx/track.rb', line 45 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.
62 63 64 65 |
# File 'lib/gpx/track.rb', line 62 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.
52 53 54 55 56 57 |
# File 'lib/gpx/track.rb', line 52 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.
69 70 71 72 73 74 75 76 |
# File 'lib/gpx/track.rb', line 69 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.
79 80 81 82 83 84 85 86 |
# File 'lib/gpx/track.rb', line 79 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.
90 91 92 |
# File 'lib/gpx/track.rb', line 90 def empty? (points.nil? || points.size.zero?) end |
#recalculate_distance ⇒ Object
112 113 114 115 116 117 |
# File 'lib/gpx/track.rb', line 112 def recalculate_distance @distance = 0 @segments.each do |seg| @distance += seg.distance end end |
#to_s ⇒ Object
Prints out a friendly summary of this track (sans points). Useful for debugging and sanity checks.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gpx/track.rb', line 97 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 |