Class: Broutes::GeoRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/broutes/geo_route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ GeoRoute

Returns a new instance of GeoRoute.



12
13
14
15
16
17
18
19
20
# File 'lib/broutes/geo_route.rb', line 12

def initialize(args={})
  args.each_pair do |key, value|
    if key.to_sym == :points
      value.each { |p| add_point(p) }
    else
      send("#{key}=", value) if respond_to?("#{key}=")
    end
  end
end

Instance Attribute Details

#end_pointObject (readonly)

Returns the value of attribute end_point.



4
5
6
# File 'lib/broutes/geo_route.rb', line 4

def end_point
  @end_point
end

#ended_atObject (readonly)

Returns the value of attribute ended_at.



4
5
6
# File 'lib/broutes/geo_route.rb', line 4

def ended_at
  @ended_at
end

#start_pointObject (readonly)

Returns the value of attribute start_point.



4
5
6
# File 'lib/broutes/geo_route.rb', line 4

def start_point
  @start_point
end

#started_atObject

Returns the value of attribute started_at.



4
5
6
# File 'lib/broutes/geo_route.rb', line 4

def started_at
  @started_at
end

#total_distanceObject

Public : Total distance measured between points in whole metres

Returns Float distance in m



92
93
94
# File 'lib/broutes/geo_route.rb', line 92

def total_distance
  @total_distance.round if @total_distance
end

#total_timeObject

Returns the value of attribute total_time.



4
5
6
# File 'lib/broutes/geo_route.rb', line 4

def total_time
  @total_time
end

Class Method Details

.from_hash(h) ⇒ Object



22
23
24
# File 'lib/broutes/geo_route.rb', line 22

def self.from_hash(h)
  route = GeoRoute.new h
end

Instance Method Details

#add_point(args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/broutes/geo_route.rb', line 40

def add_point(args)
  point = GeoPoint.new(args)
  if @start_point
    if point.distance
      @total_distance = point.distance
    else
      if distance = Maths.haversine_distance(@end_point, point)
        @total_distance += distance
      end
    end

    @total_time = point.time - @start_point.time if point.time
  else
    @start_point = point
    @total_distance = point.distance || 0
  end

  point.distance = @total_distance
  process_elevation_delta(@end_point, point)

  @end_point = point
  get_points << point
end

#average_heart_rateObject

Public: Get average heart_rate for whole GeoRoute.

Examples

@route.average_heart_rate
# => 12

Returns Integer average, or 0 if no heart_rate on points.



110
111
112
113
# File 'lib/broutes/geo_route.rb', line 110

def average_heart_rate
  points = @_points
  points.map { |p| p.heart_rate || 0 }.inject { |sum, hr| sum + hr } / points.count
end

#average_powerObject

Public: Get average power for whole GeoRoute.

Examples

@route.average_power
# => 250

Returns Float average, or 0 if no power on points.



122
123
124
125
# File 'lib/broutes/geo_route.rb', line 122

def average_power
  points = @_points
  points.map { |p| p.power || 0 }.inject { |sum, p| sum + p } / points.count
end

#hillinessObject

Public : Measure of how hilly the route is. Measured as total ascent (m) / distance (km)

Returns Float measure



99
100
101
# File 'lib/broutes/geo_route.rb', line 99

def hilliness
  (total_distance > 0) ? (total_ascent * 1000 / total_distance) : 0
end

#maximum_elevationObject

Public: Get maximum elevation for whole GeoRoute.

Examples

@route.maximum_elevation
# => 1000

Returns Integer maximum, or 0 if no elevation on points.



158
159
160
161
# File 'lib/broutes/geo_route.rb', line 158

def maximum_elevation
  points = @_points
  points.map { |p| p.elevation }.compact.max || 0
end

#maximum_heart_rateObject

Public: Get maximum heart rate for whole GeoRoute.

Examples

@route.maximum_heart_rate
# => 180

Returns Integer maximum, or 0 if no heart rate on points.



134
135
136
137
# File 'lib/broutes/geo_route.rb', line 134

def maximum_heart_rate
  points = @_points
  points.map { |p| p.heart_rate }.compact.max || 0
end

#minimum_elevationObject

Public: Get minimum elevation for whole GeoRoute.

Examples

@route.minimum_elevation
# => 10

Returns Integer minimum, or 0 if no elevation on points.



170
171
172
173
# File 'lib/broutes/geo_route.rb', line 170

def minimum_elevation
  points = @_points
  points.map { |p| p.elevation }.compact.min || 0
end

#minimum_heart_rateObject

Public: Get minimum heart rate for whole GeoRoute.

Examples

@route.minimum_heart_rate
# => 100

Returns Integer minimum, or 0 if no heart rate on points.



146
147
148
149
# File 'lib/broutes/geo_route.rb', line 146

def minimum_heart_rate
  points = @_points
  points.map { |p| p.heart_rate }.compact.min || 0
end

#pointsObject



8
9
10
# File 'lib/broutes/geo_route.rb', line 8

def points
  get_points.to_enum
end

#process_elevation_delta(last_point, new_point) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/broutes/geo_route.rb', line 64

def process_elevation_delta(last_point, new_point)
  if last_point && new_point && last_point.elevation && new_point.elevation
    delta = new_point.elevation - last_point.elevation
    @_total_ascent = self.total_ascent + (delta > 0 ? delta : 0)
    @_total_descent = self.total_descent - (delta < 0 ? delta : 0)
  end
end

#to_hashObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/broutes/geo_route.rb', line 26

def to_hash
  h = {
    'total_distance' => total_distance,
    'total_time' => total_time,
    'total_ascent' => total_ascent,
    'total_descent' => total_ascent,
    'points' => points.collect { |p| p.to_hash }
  }
  h['start_point'] = start_point.to_hash if start_point
  h['end_point'] = end_point.to_hash if end_point
  h['started_at'] = @started_at if @started_at
  h
end

#total_ascentObject



81
82
83
# File 'lib/broutes/geo_route.rb', line 81

def total_ascent
  @_total_ascent ||= 0
end

#total_descentObject



85
86
87
# File 'lib/broutes/geo_route.rb', line 85

def total_descent
  @_total_descent ||= 0
end