Class: GpxDoctor::CumulativeDistanceEnhancer
- Inherits:
-
Object
- Object
- GpxDoctor::CumulativeDistanceEnhancer
- Defined in:
- lib/gpx_doctor/cumulative_distance_enhancer.rb
Instance Method Summary collapse
-
#enhance(waypoints) ⇒ Object
Enhances each waypoint with cumulative distance from the start of the segment/route.
Instance Method Details
#enhance(waypoints) ⇒ Object
Enhances each waypoint with cumulative distance from the start of the segment/route. The first point receives cumulative_distance = 0.0 Each subsequent point receives cumulative_distance = previous.cumulative_distance + distance from previous Distance is stored in kilometers for metric and miles for imperial.
Mutates waypoints in place.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gpx_doctor/cumulative_distance_enhancer.rb', line 11 def enhance(waypoints) return if waypoints.nil? || waypoints.empty? unit_system = GpxDoctor.configuration.unit_system cumulative = 0.0 waypoints.first.cumulative_distance = cumulative waypoints.each_cons(2) do |current, nxt| distance_meters = DistanceCalculator.distance(current, nxt) distance_km = distance_meters / 1000.0 distance_converted = UnitConverter.convert_cumulative_distance(distance_km, unit_system) cumulative += distance_converted nxt.cumulative_distance = cumulative end end |