Algorithm-GooglePolylineEncoding
view release on metacpan or search on metacpan
GooglePolylineEncoding.pm view on Meta::CPAN
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};
}
@points;
}
1;
__END__
=head1 NAME
Algorithm::GooglePolylineEncoding - Google's Encoded Polyline Algorithm Format
=head1 SYNOPSIS
use Algorithm::GooglePolylineEncoding;
@polyline = ({lat => 52.5, lon => 13.4}, ...);
$encoded_polyline = Algorithm::GooglePolylineEncoding::encode_polyline(@polyline);
=head1 DESCRIPTION
B<Algorithm::GooglePolylineEncoding> implements the encoded polyline
algorithm format which is used in some parts of the Google Maps API.
The algorithm is described in
L<https://developers.google.com/maps/documentation/utilities/polylinealgorithm>.
This module is a light-weight version of
L<Geo::Google::PolylineEncoder>, essentially just doing the encoding
part without any line simplification, and implemented without any CPAN
dependencies.
=head2 FUNCTIONS
=over
=item encode_polyline(@polyline)
Take an array of C<< {lat => ..., lon => ...} >> hashrefs and return
an encoded polyline string. Latitudes and longitudes should be
expressed as decimal degrees (DD;
L<http://en.wikipedia.org/wiki/Decimal_degrees>).
( run in 0.610 second using v1.01-cache-2.11-cpan-f5b5a18a01a )