Class: Hillpace::TrackPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/hillpace/track_point.rb

Overview

Represents a geographic track point in the Earth.

Constant Summary collapse

MINIMUM_LONGITUDE =
-180
MAXIMUM_LONGITUDE =
180
MINIMUM_LATITUDE =
-90
MAXIMUM_LATITUDE =
90
METERS_PER_KILOMETER =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(longitude, latitude, elevation, time = nil) ⇒ TrackPoint

Initializes a TrackPoint object.

Parameters:

  • longitude (Number)

    The geographic longitude of the track point.

  • latitude (Number)

    The geographic latitude of the track point.

  • elevation (Number)

    The elevation of the track point relative to sea level.

  • time (Time) (defaults to: nil)

    The time when the track point was registered.



20
21
22
23
24
25
# File 'lib/hillpace/track_point.rb', line 20

def initialize(longitude, latitude, elevation, time = nil)
  self.longitude = longitude
  self.latitude = latitude
  self.elevation = elevation
  self.time = time
end

Instance Attribute Details

#elevationObject

Returns the value of attribute elevation.



6
7
8
# File 'lib/hillpace/track_point.rb', line 6

def elevation
  @elevation
end

#latitudeObject

Returns the value of attribute latitude.



6
7
8
# File 'lib/hillpace/track_point.rb', line 6

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



6
7
8
# File 'lib/hillpace/track_point.rb', line 6

def longitude
  @longitude
end

#timeObject

Returns the value of attribute time.



6
7
8
# File 'lib/hillpace/track_point.rb', line 6

def time
  @time
end

Instance Method Details

#==(other) ⇒ boolean

Overwrites the #== operator to be able to use custom getters.

Parameters:

  • other (TrackPoint)

    The other track point to be compared.

Returns:

  • (boolean)


30
31
32
33
34
35
36
# File 'lib/hillpace/track_point.rb', line 30

def ==(other)
  self.class  == other.class &&
      longitude   == other.longitude &&
      latitude    == other.latitude &&
      elevation   == other.elevation &&
      time        == other.time
end

#climb_to(other) ⇒ Number

Measures the elevation difference to other track point, in meters.

Parameters:

  • other (TrackPoint)

    The other track point to compare.

Returns:

  • (Number)

Raises:

  • (RuntimeError)

    if other is not a TrackPoint object



91
92
93
94
# File 'lib/hillpace/track_point.rb', line 91

def climb_to(other)
  raise 'Invalid track point' unless other.is_a? TrackPoint
  other.elevation - elevation
end

#distance_meters_to(other) ⇒ Number

Measures the distance to other track point, in meters.

Parameters:

  • other (TrackPoint)

    The other track point to compare.

Returns:

  • (Number)

Raises:

  • (RuntimeError)

    if other is not a TrackPoint object



80
81
82
83
84
85
# File 'lib/hillpace/track_point.rb', line 80

def distance_meters_to(other)
  raise 'Invalid track point' unless other.is_a? TrackPoint
  a = Geokit::GeoLoc.new({:lat => latitude, :lng => longitude})
  b = Geokit::GeoLoc.new({:lat => other.latitude, :lng => other.longitude})
  METERS_PER_KILOMETER * (a.distance_to b, {:units => :kms})
end

#get_linear_interpolation_with(other, bias = 0.5) ⇒ Number

Note:

For small distances between track points, a linear interpolation should be accurate enough.

Returns a linear interpolation between self and the one provided.

Parameters:

  • other (TrackPoint)

    The other track point to compare.

  • bias (Number) (defaults to: 0.5)

    Value which will determine the position in that line. 0 would be the position of self, 1 would be other position.

Returns:

  • (Number)

Raises:

  • (Runtimeerror)

    if other is not a TrackPoint object



115
116
117
118
119
120
121
# File 'lib/hillpace/track_point.rb', line 115

def get_linear_interpolation_with(other, bias = 0.5)
  raise 'Invalid track point' unless other.is_a? TrackPoint
  TrackPoint.new longitude * (1.0 - bias) + other.longitude * bias,
                 latitude * (1.0 - bias) + other.latitude * bias,
                 elevation * (1.0 - bias) + other.elevation * bias,
                 time.nil? || other.time.nil? ? nil : Time.at(time.to_f * (1.0 - bias) + other.time.to_f * bias)
end

#incline_to(other) ⇒ Number

Measures the elevation difference to other track point relative to the distance to it.

Parameters:

  • other (TrackPoint)

    The other track point to compare.

Returns:

  • (Number)

Raises:

  • (RuntimeError)

    if other is not a TrackPoint object



100
101
102
103
104
105
106
# File 'lib/hillpace/track_point.rb', line 100

def incline_to(other)
  raise 'Invalid track point' unless other.is_a? TrackPoint
  distance_meters = self.distance_meters_to other
  return 0 if distance_meters == 0

  (self.climb_to other) / distance_meters
end