Skip to content

Commit

Permalink
Merge pull request #2 from spurkis/validation
Browse files Browse the repository at this point in the history
Added validation method
  • Loading branch information
robrwo committed Sep 11, 2013
2 parents a2e5870 + 2655a66 commit 92a8197
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Revision history for 'Geo-Google-PolylineEncoder' Perl module

All changes by Steve Purkis, unless otherwise noted.

+ Added validate_encoded_points method for superficially
checking the encoded points string [Robert Rothenberg].

0.05
+ Now handles points as arrayrefs [Request by Lee Goddard]. Introduced
use_geographic_order() to decide whether it's lons or lats first.
Expand Down
20 changes: 18 additions & 2 deletions 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 Expand Up @@ -754,7 +768,9 @@ L<https://rt.cpan.org/Dist/Display.html?Queue=Geo-Google-PolylineEncoder>
More optimization: encoding big files is *slow*. Maybe XS implementation if
there's enough demand for it?
=head1 AUTHOR
=head1 AUTHORS
Robert Rothenberg <[email protected]>
Steve Purkis <[email protected]>
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 92a8197

Please sign in to comment.