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 copmrise 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 REXML::Element, or, if no :element option is passed, initialize a blank Track object.



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

def initialize(opts = {})
   @gpx_file = opts[:gpx_file]
   @segments = []
   @points = []
   
   if(opts[:element]) 
      trk_element = opts[:element]
      @name = (trk_element.elements["child::name"].text rescue "")
      XPath.each(trk_element, "child::trkseg") do |seg_element|
         seg = Segment.new(:element => seg_element, :track => self)
         (seg)
         @segments << seg
         @points.concat(seg.points) unless seg.nil?
      end
   end
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



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

def bounds
  @bounds
end

#distanceObject (readonly)

Returns the value of attribute distance.



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

def distance
  @distance
end

#gpx_fileObject

Returns the value of attribute gpx_file.



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

def gpx_file
  @gpx_file
end

#highest_pointObject (readonly)

Returns the value of attribute highest_point.



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

def highest_point
  @highest_point
end

#lowest_pointObject (readonly)

Returns the value of attribute lowest_point.



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

def lowest_point
  @lowest_point
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

#segmentsObject

Returns the value of attribute segments.



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

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.find { |s| s.contains_time?(time) }
   segment.closest_point(time)
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

#to_sObject

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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gpx/track.rb', line 118

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

#to_xmlObject

Creates a new REXML::Element from the contents of this instance.



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

def to_xml
   trk= Element.new('trk')
   name_elem = Element.new('name')
   name_elem.text = name
   trk.elements << name_elem
   segments.each do |seg|
      trk.elements << seg.to_xml
   end
   trk
end