class WhereWasI::Track

a series of sequential [lat, lon, elevation] points

Attributes

end_location[R]
end_time[R]
start_location[R]
start_time[R]

Public Class Methods

array_to_hash(a) click to toggle source
# File lib/where_was_i/track.rb, line 10
def self.array_to_hash(a)
  {
    lat: a[0],
    lon: a[1],
    elevation: a[2]
  }
end
new() click to toggle source
# File lib/where_was_i/track.rb, line 18
def initialize
  @points = {}
end

Public Instance Methods

add_point(lat:, lon:, elevation:, time:) click to toggle source

add a point to the track

@param [Float] lat latitude @param [Float] lon longitude @param [Float] elevation elevation @param [Time] time time at the given location

# File lib/where_was_i/track.rb, line 34
def add_point(lat:, lon:, elevation:, time:)
  time = Time.parse(time) if ! time.is_a?(Time)

  current = [lat, lon, elevation]

  if @start_time.nil? || time < @start_time
    @start_time     = time
    @start_location = current
  end

  if @end_time.nil?   || time > @end_time
    @end_time     = time
    @end_location = current
  end

  @points[time.to_i] = current

  true
end
at(time) click to toggle source

return the interpolated location for the given time or nil if time is outside the track's start..end

@example

track.at(time) => {lat:48, lon:98, elevation: 2100}

@param time [String,Time,Fixnum] @return [Hash,nil]

# File lib/where_was_i/track.rb, line 77
def at(time)
  if time.is_a?(String)
    time = Time.parse(time)
  end
  if time.is_a?(Integer)
    time = Time.at(time)
  end
  raise ArgumentError, "time must be a Time,String, or Fixnum" if ! time.is_a?(Time)

  return nil if ! in_time_range?(time)

  @interp ||= Interpolate::Points.new(@points)
  data = @interp.at(time.to_i)

  self.class.array_to_hash(data)
end
in_time_range?(time) click to toggle source

is the supplied time covered by this track?

@param time [Time] @return Boolean

# File lib/where_was_i/track.rb, line 65
def in_time_range?(time)
  time = Time.parse(time) if ! time.is_a?(Time)
  time_range.cover?(time)
end
points() click to toggle source
# File lib/where_was_i/track.rb, line 22
def points
  @points.map do |time,location|
    self.class.array_to_hash(location)
  end
end
time_range() click to toggle source

the time range covered by this track

@return Range

# File lib/where_was_i/track.rb, line 57
def time_range
  start_time..end_time
end