Class: Hillpace::Route

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

Overview

Represents a geographic route in the Earth, made out of consecutive segments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments) ⇒ Route

Initializes a Route object.

Parameters:

  • segments (Array<Segment>)

    The segments of the route.

Raises:

  • (RuntimeError)

    if segments is not a collection of [Segment] objects.

  • (RuntimeError)

    if any of the segments start is not the same point of the previous segment end.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hillpace/route.rb', line 11

def initialize(segments)
  raise 'Invalid segment array to initialize Route' unless segments.respond_to?('each') &&
      segments.all? {|segment| segment.is_a? Segment}

  unless segments.empty?
    last_track_point = segments.first.track_points.first
    segments.each do |segment|
      raise 'Segments must be consecutive' unless last_track_point == segment.track_points.first
      last_track_point = segment.track_points.last
    end
  end

  @segments = segments
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



4
5
6
# File 'lib/hillpace/route.rb', line 4

def segments
  @segments
end

Instance Method Details

#climbNumber

Measures the elevation difference from the start to the end of the route, in meters.

Returns:

  • (Number)


34
35
36
# File 'lib/hillpace/route.rb', line 34

def climb
  segments.inject(0) {|result, segment| result + segment.climb}
end

#distance_metersNumber

Measures the distance of the route, in meters.

Returns:

  • (Number)


28
29
30
# File 'lib/hillpace/route.rb', line 28

def distance_meters
  segments.inject(0) {|result, segment| result + segment.distance_meters}
end

#durationNumber

Measures the duration of the route, in seconds.

Returns:

  • (Number)


58
59
60
# File 'lib/hillpace/route.rb', line 58

def duration
  segments.inject(0) {|result, segment| result + segment.duration}
end

#inclineNumber

Measures the climb of the route relative to its distance.

Returns:

  • (Number)


40
41
42
# File 'lib/hillpace/route.rb', line 40

def incline
  self.climb / self.distance_meters
end

#pacePace

Measures the pace of the route.

Returns:



64
65
66
# File 'lib/hillpace/route.rb', line 64

def pace
  Pace.from_meters_per_second(distance_meters / duration)
end

#split(split_distances) ⇒ Route

Splits the segments in the route in the distances indicated.

Parameters:

  • split_distances (Array<Number>)

    Distances where the segments of the route should be splitted.

Returns:

  • (Route)

    A copy of this route, with splitted segments



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hillpace/route.rb', line 71

def split(split_distances)
  split_distances_enumerator = split_distances.sort.each
  next_split_distance = split_distances_enumerator.next
  accumulated_distance = 0
  result = []

  segments.lazy.each do |segment|
    while next_split_distance < accumulated_distance + segment.distance_meters
      subsegments = segment.split (next_split_distance - accumulated_distance)
      result << subsegments.first
      accumulated_distance += subsegments.first.distance_meters
      if subsegments.length > 1
        segment = subsegments.last
        next_split_distance = split_distances_enumerator.next rescue Float::INFINITY
      end
    end
    result << segment.clone
    accumulated_distance += segment.distance_meters
  end

  Route.new result
end

#total_downhillsNumber

Measures the sum of downhills between segments of the route.

Returns:

  • (Number)


52
53
54
# File 'lib/hillpace/route.rb', line 52

def total_downhills
  segments.inject(0) {|result, segment| result + segment.total_downhills}
end

#total_uphillsNumber

Measures the sum of uphills between segments of the route.

Returns:

  • (Number)


46
47
48
# File 'lib/hillpace/route.rb', line 46

def total_uphills
  segments.inject(0) {|result, segment| result + segment.total_uphills}
end