class Manipulation::Unifier

Constants

APPROXIMATION_DISTANCE
SAMPLE_LENGTH

Attributes

unified_points[R]

Public Class Methods

new(points, old_points) click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 7
def initialize(points, old_points)
  @points = points
  @old_points = old_points
end

Public Instance Methods

unify() click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 12
def unify
  @unified_points = []
  average_point_location.each do |apl|
    possible_match = match_with_previous(apl)
    @unified_points << possible_match.nil? ? apl : possible_match
  end
end

Private Instance Methods

average_point_location() click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 22
def average_point_location
  @result = []
  calculate_points_per_km
  @points.each_slice(@points_ratio) do |slice|
    @result << averaged_slice(slice)
  end
  @result
end
averaged_slice(slice) click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 50
def averaged_slice(slice)
  {
    lat: avg(slice, :lat),
    lon: avg(slice, :lon),
    ele: avg(slice, :ele),
    time: avg_time(slice)
  }
end
avg(slice, key) click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 59
def avg(slice, key)
  return unless slice.first[key]

  slice.map { |point| point[key].to_f }.inject(:+) / slice.size
end
avg_time(slice) click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 65
def avg_time(slice)
  return unless slice.first[:time]

  start_time = Time.parse slice.first[:time]
  end_time = Time.parse slice.last[:time]
  diff = end_time - start_time

  start_time + (diff / 2)
end
calculate_points_per_km() click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 45
def calculate_points_per_km
  stats = Manipulation::Statistics.new(@points)
  @points_ratio = (@points.size * SAMPLE_LENGTH / stats.basic_statistics[:distance]).floor
end
match_with_previous(location) click to toggle source
# File lib/simple_trail/manipulation/unifier.rb, line 33
def match_with_previous(location)
  return unless @old_points

  @old_points.find do |op|
    Geokit::LatLng.new(op[:lat], op[:lon]).distance_to(
      Geokit::LatLng.new(location[:lat], location[:lon])
    ) < APPROXIMATION_DISTANCE
  end
end