Algorithm-GooglePolylineEncoding
view release on metacpan or search on metacpan
GooglePolylineEncoding.pm view on Meta::CPAN
# 11. Convert each value to its ASCII equivalent:
# `~oia@
@chunks = map { chr } @chunks;
join '', @chunks;
}
sub encode_polyline {
my(@path) = @_;
my @res;
my($curr_lat_e5,$curr_lon_e5) = (0,0);
for my $lat_lon (@path) {
my($lat_e5,$lon_e5) = map { sprintf("%.0f", $_*1e5) } ($lat_lon->{lat}, $lat_lon->{lon});
my $deltay = ($lat_e5 - $curr_lat_e5) / 1e5;
my $deltax = ($lon_e5 - $curr_lon_e5) / 1e5;
push @res, encode_number($deltay), encode_number($deltax);
($curr_lat_e5,$curr_lon_e5) = ($lat_e5,$lon_e5);
}
join '', @res;
}
sub encode_level {
# 1. Take the initial unsigned value:
# 174
my $number = shift;
# 2. Convert the decimal value to a binary value:
# 10101110
my $bin;
if ($number > ~0) {
# sprintf '%b' works only for integers
require Math::BigInt;
$bin = Math::BigInt->new($number)->as_bin;
$bin =~ s{^0b}{};
} else {
$bin = sprintf '%b', $number;
}
# 3. Break the binary value out into 5-bit chunks (starting from the right hand side):
# 101 01110
$bin = '0'x(5-length($bin)%5) . $bin if length($bin)%5 != 0; # pad
my @chunks;
my $revbin = reverse $bin;
push @chunks, scalar reverse($1) while $revbin =~ m{(.....)}g;
# 4. Place the 5-bit chunks into reverse order:
# 01110 101
# It's already reversed
# 5. OR each value with 0x20 if another bit chunk follows:
# 101110 00101
@chunks = ((map { oct("0b$_") | 0x20 } @chunks[0 .. $#chunks-1]), oct("0b".$chunks[-1])); # and also decode to decimal on the fly
# 6. Convert each value to decimal:
# 46 5
# Done above
# 7. Add 63 to each value:
# 109 68
@chunks = map { $_+63 } @chunks;
# 8. Convert each value to its ASCII equivalent:
# mD
@chunks = map { chr } @chunks;
join '', @chunks;
}
# Translated this php script
# <http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/>
# to perl
sub decode_polyline {
my($encoded) = @_;
my $length = length $encoded;
my $index = 0;
my @points;
my $lat = 0;
my $lng = 0;
while ($index < $length) {
# The encoded polyline consists of a latitude value followed
# 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);
use integer; # see last paragraph of "Integer Arithmetic" in perlop.pod
# Check if negative, and convert. (All negative values have the last bit
# set)
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};
}
( run in 1.045 second using v1.01-cache-2.11-cpan-df04353d9ac )