Class: Broutes::GeoRoute
- Inherits:
-
Object
- Object
- Broutes::GeoRoute
- Defined in:
- lib/broutes/geo_route.rb
Instance Attribute Summary collapse
-
#end_point ⇒ Object
readonly
Returns the value of attribute end_point.
-
#ended_at ⇒ Object
readonly
Returns the value of attribute ended_at.
-
#start_point ⇒ Object
readonly
Returns the value of attribute start_point.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#total_distance ⇒ Object
Public : Total distance measured between points in whole metres.
-
#total_time ⇒ Object
Returns the value of attribute total_time.
Class Method Summary collapse
Instance Method Summary collapse
- #add_point(args) ⇒ Object
-
#average_heart_rate ⇒ Object
Public: Get average heart_rate for whole GeoRoute.
-
#average_power ⇒ Object
Public: Get average power for whole GeoRoute.
-
#hilliness ⇒ Object
Public : Measure of how hilly the route is.
-
#initialize(args = {}) ⇒ GeoRoute
constructor
A new instance of GeoRoute.
-
#maximum_elevation ⇒ Object
Public: Get maximum elevation for whole GeoRoute.
-
#maximum_heart_rate ⇒ Object
Public: Get maximum heart rate for whole GeoRoute.
-
#minimum_elevation ⇒ Object
Public: Get minimum elevation for whole GeoRoute.
-
#minimum_heart_rate ⇒ Object
Public: Get minimum heart rate for whole GeoRoute.
- #points ⇒ Object
- #process_elevation_delta(last_point, new_point) ⇒ Object
- #to_hash ⇒ Object
- #total_ascent ⇒ Object
- #total_descent ⇒ Object
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_point ⇒ Object (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_at ⇒ Object (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_point ⇒ Object (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_at ⇒ Object
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_distance ⇒ Object
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_time ⇒ Object
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
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_rate ⇒ Object
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_power ⇒ Object
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 |
#hilliness ⇒ Object
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_elevation ⇒ Object
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_rate ⇒ Object
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_elevation ⇒ Object
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_rate ⇒ Object
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 |
#points ⇒ Object
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_hash ⇒ Object
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_ascent ⇒ Object
81 82 83 |
# File 'lib/broutes/geo_route.rb', line 81 def total_ascent @_total_ascent ||= 0 end |
#total_descent ⇒ Object
85 86 87 |
# File 'lib/broutes/geo_route.rb', line 85 def total_descent @_total_descent ||= 0 end |