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
-
#hilliness ⇒ Object
Public : Measure of how hilly the route is.
-
#initialize(args = {}) ⇒ GeoRoute
constructor
A new instance of 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 |
#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 |
#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 |