class Manipulation::Enricher

Attributes

enriched_points[R]

Public Class Methods

new(points, distance_limit=nil) click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 7
def initialize(points, distance_limit=nil)
  @points = points
  @distance_limit = distance_limit
  @counter = 3
end

Public Instance Methods

enrich() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 13
def enrich
  add_points_where_big_gaps
  recalculate_distances
  add_total_distance
  add_km_markers

  @enriched_points = @points
end

Private Instance Methods

add_km_markers() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 75
def add_km_markers
  total_distance = @points[-1][:total_distance].floor - 1
  total_distance.times do |i|
    find_and_enrich_first_occurence(i+1)
  end
end
add_points_where_big_gaps() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 24
def add_points_where_big_gaps
  detect_gaps
  @gaps.any? ? points_with_gaps_field : @points
end
add_total_distance() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 64
def add_total_distance
  @points.each_with_index do |point, i|
    new_total = if i.zero?
                  point[:distance]
                else !point[:distance].nil? && !@points[i-1][:total_distance].nil?
                  point[:distance] + @points[i-1][:total_distance]
                end
    @points[i].merge!(total_distance: new_total)
  end
end
calculate_distance(loc1, loc2) click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 87
def calculate_distance(loc1, loc2)
  return 0.0 if loc2.nil?
  latlan(loc1).distance_to(latlan(loc2))
rescue
end
calculate_middle_point(loc1, loc2) click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 43
def calculate_middle_point(loc1, loc2)
  latlan(loc1).midpoint_to(latlan(loc2))
end
detect_gaps() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 47
def detect_gaps
  gap_limit = @distance_limit || 0.1
  @gaps = []
  @points.each_cons(2).with_index do |pair, i|
    distance = calculate_distance(pair[0], pair[1])
    @gaps << {origin: i, destination: i+1, distance: distance} if distance > gap_limit
  end
end
find_and_enrich_first_occurence(i) click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 82
def find_and_enrich_first_occurence(i)
  index = @points.find_index{|point| point[:total_distance] > i}
  @points[index].merge!(label: i)
end
latlan(location) click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 93
def latlan(location)
  Geokit::LatLng.new(location[:lat] || location['lat'], location[:lon] || location['lon'])
rescue
end
points_with_gaps_field() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 29
def points_with_gaps_field
  return @points if @gaps.none? || @counter.zero?

  offset = 0
  @gaps.each do |gap|
    middle_point = calculate_middle_point(@points[gap[:origin] + offset], @points[gap[:destination] + offset])
    @points = @points.insert(gap[:destination] + offset, {lat: middle_point.lat, lon: middle_point.lng })

    offset += 1
  end
  @counter -= 1
  add_points_where_big_gaps
end
recalculate_distances() click to toggle source
# File lib/simple_trail/manipulation/enricher.rb, line 56
def recalculate_distances
  @points.each_cons(2).with_index do |pair, i|
    distance = calculate_distance(pair[0], pair[1])
    @points[i+1].merge!(distance: distance)
  end
  @points[0].merge!(distance: 0.0)
end