MOTIS/Polyline.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 48 CC 2 SCAR 11.5
CC
2
Cov
0%
decode_polyline
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package Travel::Status::MOTIS::Polyline;
2  
3 use strict;
4 use warnings;
5 use 5.014;
6  
7 # Adapted from code by Slaven Rezic
8 #
9 # Copyright (C) 2009,2010,2012,2017,2018 Slaven Rezic. All rights reserved.
10 # This package is free software; you can redistribute it and/or
11 # modify it under the same terms as Perl itself.
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://www.rezic.de/eserte/
15  
16 use parent 'Exporter';
17  
18 our @EXPORT_OK = qw(decode_polyline);
19  
20 our $VERSION = '0.07';
21  
22 # Translated this php script
23 # <http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/>
24 # to perl
25 sub decode_polyline {
26 my ( $input ) = @_;
27  
28 my $encoded = $input->{points};
29 my $precision = $input->{precision};
30  
31 my $length = length $encoded;
32 my $index = 0;
33 my @points;
34 my $lat = 0;
35 my $lon = 0;
36  
37 while ( $index < $length ) {
38  
39 # The encoded polyline consists of a latitude value followed
40 # by a longitude value. They should always come in pairs. Read
41 # the latitude value first.
42 for my $val ( \$lat, \$lon ) {
43 my $shift = 0;
44 my $result = 0;
45  
46 # Temporary variable to hold each ASCII byte.
47 my $b;
48 do {
49 # The `ord(substr($encoded, $index++))` statement returns
50 # the ASCII code for the character at $index. Subtract 63
51 # to get the original value. (63 was added to ensure
52 # proper ASCII characters are displayed in the encoded
53 # polyline string, which is `human` readable)
54 $b = ord( substr( $encoded, $index++, 1 ) ) - 63;
55  
56 # AND the bits of the byte with 0x1f to get the original
57 # 5-bit `chunk. Then left shift the bits by the required
58 # amount, which increases by 5 bits each time. OR the
59 # value into $results, which sums up the individual 5-bit
60 # chunks into the original value. Since the 5-bit chunks
61 # were reversed in order during encoding, reading them in
62 # this way ensures proper summation.
63 $result |= ( $b & 0x1f ) << $shift;
64 $shift += 5;
65 }
66  
67 # Continue while the read byte is >= 0x20 since the last
68 # `chunk` was not OR'd with 0x20 during the conversion
69 # process. (Signals the end)
70 while ( $b >= 0x20 );
71  
72 # see last paragraph of "Integer Arithmetic" in perlop.pod
73 use integer;
74  
75 # Check if negative, and convert. (All negative values have the last bit
76 # set)
77 my $dtmp
78 = ( ( $result & 1 ) ? ~( $result >> 1 ) : ( $result >> 1 ) );
79  
80 # Compute actual latitude (resp. longitude) since value is
81 # offset from previous value.
82 $$val += $dtmp;
83 }
84  
85 # The actual latitude and longitude values were multiplied by
86 # 1e$precision before encoding so that they could be converted to a 32-bit
87 # integer representation. (With a decimal accuracy of $precision places)
88 # Convert back to original values.
89 push(
90 @points,
91 {
92 lat => $lat * (10 ** -$precision),
93 lon => $lon * (10 ** -$precision),
94 }
95 );
96 }
97  
98 return @points;
99 }
100  
101 1;