Class: WhereWasI::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/where_was_i/track.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrack


18
19
20
# File 'lib/where_was_i/track.rb', line 18

def initialize
  @points = {}
end

Instance Attribute Details

#end_locationObject (readonly)

Returns the value of attribute end_location


8
9
10
# File 'lib/where_was_i/track.rb', line 8

def end_location
  @end_location
end

#end_timeObject (readonly)

Returns the value of attribute end_time


8
9
10
# File 'lib/where_was_i/track.rb', line 8

def end_time
  @end_time
end

#start_locationObject (readonly)

Returns the value of attribute start_location


8
9
10
# File 'lib/where_was_i/track.rb', line 8

def start_location
  @start_location
end

#start_timeObject (readonly)

Returns the value of attribute start_time


8
9
10
# File 'lib/where_was_i/track.rb', line 8

def start_time
  @start_time
end

Class Method Details

.array_to_hash(a) ⇒ Object


10
11
12
13
14
15
16
# 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

Instance Method Details

#add_point(lat:, lon:, elevation:, time:) ⇒ Object

add a point to the track


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Hash?

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

Examples:

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

Raises:

  • (ArgumentError)

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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?(Fixnum)
    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) ⇒ Boolean

is the supplied time covered by this track?


65
66
67
68
# 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

#pointsObject


22
23
24
25
26
# 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_rangeObject

the time range covered by this track


57
58
59
# File 'lib/where_was_i/track.rb', line 57

def time_range
  start_time..end_time
end