Class: GpxDoctor::SegmentSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_doctor/segment_splitter.rb

Instance Method Summary collapse

Instance Method Details

#split(points, max_distance) ⇒ Object

Splits a sequence of waypoints so that no two consecutive points are farther apart than max_distance meters. Pairs that are already within the limit are left untouched. When a pair exceeds the limit, evenly spaced intermediate points are interpolated between them (lat/lon always; ele and time only when both endpoints have values).

Returns a new array; the original is not mutated.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gpx_doctor/segment_splitter.rb', line 12

def split(points, max_distance)
  return points if points.nil? || points.size < 2

  result = [points.first]

  points.each_cons(2) do |current, nxt|
    dist = DistanceCalculator.distance(current, nxt)

    if dist > max_distance
      n_segments = (dist / max_distance).ceil
      (1...n_segments).each do |i|
        fraction = i.to_f / n_segments
        result << interpolate(current, nxt, fraction)
      end
    end

    result << nxt
  end

  result
end