Skip to content

Commit

Permalink
Added validate_encoded_points method for superficial checks of the en…
Browse files Browse the repository at this point in the history
…coding
  • Loading branch information
Robert Rothenberg committed Sep 11, 2013
1 parent a2e5870 commit fee4a2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/Geo/Google/PolylineEncoder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ sub calculate_distances {
# Iy = Ay + r(By-Ay)
#
# And the distance from A to I = r*L.
# Use another parameter s to indicate the location along IP, with the
# Use another parameter s to indicate the location along IP, with the
# following meaning:
# s<0 P is left of AB
# s>0 P is right of AB
Expand Down Expand Up @@ -509,6 +509,20 @@ sub encode_number {
return $encodeString;
}

# Superficial validation of encoded points. Note that decode_points
# does not check that points are validated before decoding.
sub validate_encoded_points {
my ($class, $encoded) = @_;

return unless (defined $encoded && $encoded ne "");

my @ords = unpack "C*", $encoded;

my @out = grep { $_ < 63 || $_ > 127 } @ords;
return if @out;

return 1;
}

# Decode an encoded polyline into a list of lat/lng tuples.
# adapted from http://code.google.com/apis/maps/documentation/include/polyline.js
Expand Down
6 changes: 6 additions & 0 deletions t/01_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
is( $eline->{num_levels}, 18, 'ex1 num_levels' );
is( $eline->{zoom_factor}, 2, 'ex1 zoom_factor' );
is( $eline->{points}, '_p~iF~ps|U_ulLnnqC_mqNvxq`@', 'ex1 points' );
ok( $encoder->validate_encoded_points($eline->{points}), 'ex1 validate_encoded_points' );
is( $eline->{levels}, 'POP', 'ex1 levels' );

my $d_points = $encoder->decode_points( $eline->{points} );
Expand All @@ -73,6 +74,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
is( $eline->{num_levels}, 18, 'ex1a num_levels' );
is( $eline->{zoom_factor}, 2, 'ex1a zoom_factor' );
is( $eline->{points}, '_p~iF~ps|U_ulLnnqC', 'ex1a points' );
ok( $encoder->validate_encoded_points($eline->{points}), 'ex1a validate_encoded_points' );
is( $eline->{levels}, 'PP', 'ex1a levels' );
}

Expand All @@ -85,6 +87,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
my $encoder = Geo::Google::PolylineEncoder->new(zoom_factor => 2, num_levels => 18);
my $eline = $encoder->encode( @points );
is( $eline->{points}, '_p~iF~ps|U_ulLnnqC', 'ex1b points' );
ok( $encoder->validate_encoded_points($eline->{points}), 'ex1b validate_encoded_points' );
is( $eline->{levels}, 'PP', 'ex1b levels' );
}

Expand All @@ -97,6 +100,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
my $encoder = Geo::Google::PolylineEncoder->new(zoom_factor => 2, num_levels => 18, lons_first => 1 );
my $eline = $encoder->encode( @points );
is( $eline->{points}, '_p~iF~ps|U_ulLnnqC', 'ex1c points' );
ok( $encoder->validate_encoded_points($eline->{points}), 'ex1c validate_encoded_points' );
is( $eline->{levels}, 'PP', 'ex1c levels' );
}

Expand Down Expand Up @@ -124,6 +128,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
is( $eline->{num_levels}, 18, 'ex2 num_levels' );
is( $eline->{zoom_factor}, 2, 'ex2 zoom_factor' );
is( $eline->{points}, 'krchIwzo}@CqKa@}KaAwKwBwOgFw\\mD}SsCgO{Je`@_BqJ', 'ex2 points' ); # bootstrapped
ok( $encoder->validate_encoded_points($eline->{points}), 'ex2 validate_encoded_points' );
is( $eline->{levels}, 'PADAEA@CBP', 'ex2 levels' ); # bootstrapped

my $ipeu = 'krchIwzo}@CqKa@}KaAwKwBwOgFw\\mD}SsCgO{Je`@_BsJ'; # from google
Expand All @@ -150,6 +155,7 @@ use_ok( 'Geo::Google::PolylineEncoder' );
# now test all points & compare
$eline = $encoder->visible_threshold( 0.00000001 )->encode( \@points );
is( $eline->{points}, 'krchIwzo}@CqKa@}KaAwKwBwOyAuJmCaQmD}SsCgOgDuMsEoQ_BqJ', 'ex2 all points' ); # bootstrapped
ok( $encoder->validate_encoded_points($eline->{points}), 'ex2 validate_encoded_points' );
is( $eline->{levels}, 'PKMKOEKJMBLP', 'ex2 all levels' );

my $ipeu_all = 'krchIwzo}@CqKa@}KaAwKwBwOyAuJmCaQmD}SsCgOgDuMsEoQ_BsJ'; # from google
Expand Down

0 comments on commit fee4a2f

Please sign in to comment.