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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gpx/track.rb', line 36

def initialize(opts = {})
  @gpx_file = opts[:gpx_file]
  @segments = []
  @points = []
  
  if(opts[:element])
    trk_element = opts[:element]
    @name = (trk_element.at("name").inner_text rescue "")
    @comment = (trk_element.at('cmt').inner_text rescue '')
    @description = (trk_element.at('desc').inner_text rescue '')
    trk_element.search("trkseg").each do |seg_element|
      seg = Segment.new(:element => seg_element, :track => self, :gpx_file => @gpx_file)
      (seg)
      @segments << seg
    end
  end
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



31
32
33
# File 'lib/gpx/track.rb', line 31

def bounds
  @bounds
end

#commentObject

Returns the value of attribute comment.



32
33
34
# File 'lib/gpx/track.rb', line 32

def comment
  @comment
end

#descriptionObject

Returns the value of attribute description.



32
33
34
# File 'lib/gpx/track.rb', line 32

def description
  @description
end

#distanceObject (readonly)

Returns the value of attribute distance.



31
32
33
# File 'lib/gpx/track.rb', line 31

def distance
  @distance
end

#gpx_fileObject

Returns the value of attribute gpx_file.



32
33
34
# File 'lib/gpx/track.rb', line 32

def gpx_file
  @gpx_file
end

#highest_pointObject (readonly)

Returns the value of attribute highest_point.



31
32
33
# File 'lib/gpx/track.rb', line 31

def highest_point
  @highest_point
end

#lowest_pointObject (readonly)

Returns the value of attribute lowest_point.



31
32
33
# File 'lib/gpx/track.rb', line 31

def lowest_point
  @lowest_point
end

#moving_durationObject (readonly)

Returns the value of attribute moving_duration.



31
32
33
# File 'lib/gpx/track.rb', line 31

def moving_duration
  @moving_duration
end

#nameObject

Returns the value of attribute name.



32
33
34
# File 'lib/gpx/track.rb', line 32

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



31
32
33
# File 'lib/gpx/track.rb', line 31

def points
  @points
end

#segmentsObject

Returns the value of attribute segments.



32
33
34
# File 'lib/gpx/track.rb', line 32

def segments
  @segments
end

Instance Method Details

#append_segment(seg) ⇒ Object

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



55
56
57
58
59
# File 'lib/gpx/track.rb', line 55

def append_segment(seg)
  (seg)
  @segments << seg
  @points.concat(seg.points) unless seg.nil?
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.



72
73
74
75
# File 'lib/gpx/track.rb', line 72

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)


62
63
64
65
66
67
# File 'lib/gpx/track.rb', line 62

def contains_time?(time)
  segments.each do |seg|
    return true if seg.contains_time?(time)
  end
  return 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.



79
80
81
82
83
84
85
86
# File 'lib/gpx/track.rb', line 79

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

#delete_area(area) ⇒ Object

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



89
90
91
92
93
94
95
96
# File 'lib/gpx/track.rb', line 89

def delete_area(area)
  
  segments.each do |seg|
    seg.delete_area(area)
    (seg) unless seg.empty?
  end
  segments.delete_if { |seg| seg.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)


100
101
102
# File 'lib/gpx/track.rb', line 100

def empty?
  (points.nil? or points.size.zero?)
end

#recalculate_distanceObject



122
123
124
125
126
127
# File 'lib/gpx/track.rb', line 122

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gpx/track.rb', line 107

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.to_s}"
  result
end