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

#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

#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