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

Returns a new instance of Track.



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

Parameters:

  • lat (Float)

    latitude

  • lon (Float)

    longitude

  • elevation (Float)

    elevation

  • time (Time)

    time at the given location



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/where_was_i/track.rb', line 28

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}

Parameters:

  • time (String, Time, Fixnum)

Returns:

  • (Hash, nil)

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/where_was_i/track.rb', line 71

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?

Parameters:

  • time (Time)

Returns:

  • (Boolean)

    Boolean



59
60
61
62
# File 'lib/where_was_i/track.rb', line 59

def in_time_range?(time)
  time = Time.parse(time) if ! time.is_a?(Time)
  time_range.cover?(time)
end

#time_rangeObject

the time range covered by this track

Returns:

  • Range



51
52
53
# File 'lib/where_was_i/track.rb', line 51

def time_range
  start_time..end_time
end