class GoogleDistanceMatrix::PolylineEncoder::Delta

Calculates deltas between lat_lng values, internal helper class for PolylineEncoder.

According to the Google's polyline encoding spec:

"Additionally, to conserve space, points only include the offset
from the previous point (except of course for the first point)"

@see GoogleDistanceMatrix::PolylineEncoder

Public Class Methods

new(precision = 1e5) click to toggle source
# File lib/google_distance_matrix/polyline_encoder/delta.rb, line 13
def initialize(precision = 1e5)
  @precision = precision
end

Public Instance Methods

deltas_rounded(array_of_lat_lng_pairs) click to toggle source

Takes a set of lat/lng pairs and calculates delta

Returns a flatten array where each lat/lng delta pair is put in order.

# File lib/google_distance_matrix/polyline_encoder/delta.rb, line 20
def deltas_rounded(array_of_lat_lng_pairs)
  rounded = round_to_precision array_of_lat_lng_pairs
  calculate_deltas rounded
end

Private Instance Methods

calculate_deltas(rounded) click to toggle source
# File lib/google_distance_matrix/polyline_encoder/delta.rb, line 36
def calculate_deltas(rounded)
  deltas = []

  delta_lat = 0
  delta_lng = 0

  rounded.each do |(lat, lng)|
    deltas << lat - delta_lat
    deltas << lng - delta_lng

    delta_lat = lat
    delta_lng = lng
  end

  deltas
end
round_to_precision(array_of_lat_lng_pairs) click to toggle source
# File lib/google_distance_matrix/polyline_encoder/delta.rb, line 27
def round_to_precision(array_of_lat_lng_pairs)
  array_of_lat_lng_pairs.map do |(lat, lng)|
    [
      (lat * @precision).round,
      (lng * @precision).round
    ]
  end
end