Class: GoogleDistanceMatrix::PolylineEncoder::ValueEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_distance_matrix/polyline_encoder/value_encoder.rb

Overview

Encodes a single value, like 17998321, in to encoded polyline value, as described in Google’s documentation developers.google.com/maps/documentation/utilities/polylinealgorithm

This is an internal helper class for PolylineEncoder. This encoder expects that the value is rounded.

Instance Method Summary collapse

Instance Method Details

#encode(value) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google_distance_matrix/polyline_encoder/value_encoder.rb', line 17

def encode(value)
  negative = value < 0
  value = value.abs

  # Step 3: Two's complement when negative
  value = ~value + 1 if negative

  # Step 4: Left shift one bit
  value = value << 1

  # Step 5: Invert if value was negative
  value = ~value if negative

  # Step 6 and 7: 5-bit chunks in reverse order
  # We AND 5 first bits and push them on to chunks array.
  # Right shift bits to get rid of the ones we just put on the array.
  # Bits will end up in reverse order.
  chunks_of_5_bits = []
  while value > 0
    chunks_of_5_bits.push(value & 0x1f)
    value >>= 5
  end

  chunks_of_5_bits << 0 if chunks_of_5_bits.empty?

  # Step 8, 9 and 10: OR each value with 0x20, unless last one. Add 63 to all values
  last_index = chunks_of_5_bits.length - 1
  chunks_of_5_bits.each_with_index do |chunk, index|
    chunks_of_5_bits[index] = chunk | 0x20 unless index == last_index
    chunks_of_5_bits[index] += 63
  end

  # step 11: Convert to ASCII
  chunks_of_5_bits.map(&:chr)
end