Algorithm-GooglePolylineEncoding
view release on metacpan or search on metacpan
GooglePolylineEncoding.pm view on Meta::CPAN
my $is_negative = $number < 0;
# 3. Convert the decimal value to binary. Note that a negative value must be calculated using its two's complement by inverting the binary value and adding one to the result:
# 00000001 00010010 10100001 11110001
# 11111110 11101101 01011110 00001110
# 11111110 11101101 01011110 00001111
# nothing to do here, we don't calculate with binary strings...
# 4. Left-shift the binary value one bit:
# 11111101 11011010 10111100 00011110
$number <<= 1;
$number &= 0xffffffff; # to assure 32 bit
# 5. If the original decimal value is negative, invert this encoding:
# 00000010 00100101 01000011 11100001
if ($is_negative) {
$number = (~$number);
$number &= 0xffffffff;
}
# 6. Break the binary value out into 5-bit chunks (starting from the right hand side):
# 00001 00010 01010 10000 11111 00001
my $bin = sprintf '%b', $number;
$bin = '0'x(5-length($bin)%5) . $bin if length($bin)%5 != 0; # pad
my @chunks;
GooglePolylineEncoding.pm view on Meta::CPAN
# by a longitude value. They should always come in pairs. Read
# the latitude value first.
for my $val (\$lat, \$lng) {
my $shift = 0;
my $result = 0;
# Temporary variable to hold each ASCII byte.
my $b;
do {
# The `ord(substr($encoded, $index++))` statement returns
# the ASCII code for the character at $index. Subtract 63
# to get the original value. (63 was added to ensure
# proper ASCII characters are displayed in the encoded
# polyline string, which is `human` readable)
$b = ord(substr($encoded, $index++, 1)) - 63;
# AND the bits of the byte with 0x1f to get the original
# 5-bit `chunk. Then left shift the bits by the required
# amount, which increases by 5 bits each time. OR the
# value into $results, which sums up the individual 5-bit
# chunks into the original value. Since the 5-bit chunks
# were reversed in order during encoding, reading them in
# this way ensures proper summation.
$result |= ($b & 0x1f) << $shift;
$shift += 5;
}
# Continue while the read byte is >= 0x20 since the last
# `chunk` was not OR'd with 0x20 during the conversion
# process. (Signals the end)
while ($b >= 0x20);
GooglePolylineEncoding.pm view on Meta::CPAN
my $dtmp = (($result & 1) ? ~($result >> 1) : ($result >> 1));
# Compute actual latitude (resp. longitude) since value is
# offset from previous value.
$$val += $dtmp;
}
# The actual latitude and longitude values were multiplied by
# 1e5 before encoding so that they could be converted to a 32-bit
# integer representation. (With a decimal accuracy of 5 places)
# Convert back to original values.
push @points, {lat => $lat * 1e-5, lon => $lng * 1e-5};
}
@points;
}
1;
__END__
xt/rt74303.t view on Meta::CPAN
return $encoded_points;
}
# 3. Convert the decimal value to binary. Note that a negative value must be inverted
# and provide padded values toward the byte boundary:
# 00000001 00010010 10100001 11110001
# 11111110 11101101 10100001 00001110
# 11111110 11101101 01011110 00001111
# 4. Shift the binary value:
# 11111110 11101101 01011110 00001111 0
# 5. If the original decimal value is negative, invert this encoding:
# 00000001 00010010 10100001 11110000 1
# 6. Break the binary value out into 5-bit chunks (starting from the right hand side):
# 00001 00010 01010 10000 11111 00001
# 7. Place the 5-bit chunks into reverse order:
# 00001 11111 10000 01010 00010 00001
# 8. OR each value with 0x20 if another bit chunk follows:
# 100001 111111 110000 101010 100010 000001
# 9. Convert each value to decimal:
# 33 63 48 42 34 1
# 10. Add 63 to each value:
( run in 0.219 second using v1.01-cache-2.11-cpan-1c8d708658b )