| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Geo::Google::PolylineEncoder - encode lat/lons to Google Maps Polylines | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use Geo::Google::PolylineEncoder; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | my $points = [ | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # can also take points as [lat, lon] | ||||||||||||||||||||||||||||||||||||||||||
| 11 | { lat => 38.5, lon => -120.2 }, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | { lat => 40.7, lon => -120.95 }, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | { lat => 43.252, lon => -126.453 }, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | my $encoder = Geo::Google::PolylineEncoder->new; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | my $eline = $encoder->encode( $points ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | print $eline->{num_levels}; # 18 | ||||||||||||||||||||||||||||||||||||||||||
| 18 | print $eline->{zoom_factor}; # 2 | ||||||||||||||||||||||||||||||||||||||||||
| 19 | print $eline->{points}; # _p~iF~ps|U_ulLnnqC_mqNvxq`@ | ||||||||||||||||||||||||||||||||||||||||||
| 20 | print $eline->{levels}; # POP | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # in Javascript, assuming eline was encoded as JSON: | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # ... load GMap2 ... | ||||||||||||||||||||||||||||||||||||||||||
| 24 | var opts = { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | points: eline.points, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | levels: eline.levels, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | numLevels: eline.num_levels, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | zoomFactor: eline.zoom_factor, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | var line = GPolyline.fromEncoded( opts ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | package Geo::Google::PolylineEncoder; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | use accessors qw(num_levels zoom_factor visible_threshold force_endpoints | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | zoom_level_breaks escape_encoded_points lons_first | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | points dists max_dist encoded_points encoded_levels ); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use constant defaults => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | num_levels => 18, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | zoom_factor => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | force_endpoints => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | escape_encoded_points => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | visible_threshold => 0.00001, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | lons_first => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | our $VERSION = 0.06; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # The constructor | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | $self->init(@_); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 10 | foreach my $attr (keys %{ $self->defaults }) { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 60 | $self->$attr($self->defaults->{$attr}); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | foreach my $attr (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | $self->$attr($args{$attr}); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | ▶ | sub init_zoom_level_breaks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init_zoom_level_breaks: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # Cache for performance: | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | my $num_levels = $self->num_levels; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | my @zoom_level_breaks; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | for my $i (1 .. $num_levels) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 171 | push @zoom_level_breaks, | |||||||||||||||||||||||||||||||||||||||||
| 81 | $self->visible_threshold * $self->zoom_factor ** ($num_levels - $i); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | $self->zoom_level_breaks(\@zoom_level_breaks); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 10 | ▶ | sub reset_encoder { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_encoder: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | $self->points([])->dists([])->max_dist(0)->encoded_points('')->encoded_levels(''); | |||||||||||||||||||||||||||||||||||||||||
| 90 | # Note: calculate zoom level breaks here in case num_levels, etc. have | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # changed between encodes: | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | $self->init_zoom_level_breaks; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | sub set_points { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_points: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | my ($self, $points) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | die "points must be an arrayref!" unless UNIVERSAL::isa( $points, 'ARRAY' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Internally, points are stored as [lat, lon]. Although this is less | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # readable, it is more efficient than using a hash. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Make a copy of the points we were given | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 10 | my @points; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 10 | ▶ | if (UNIVERSAL::isa($points->[0], 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | my @keys = keys %{ $points->[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | my ($lat_key) = grep( /^lat$/i, @keys ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 8 | my ($lon_key) = grep( /^(?:lon)|(?:lng)$/i, @keys ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | @points = map { [$_->{$lat_key}, $_->{$lon_key}] } @$points; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif (UNIVERSAL::isa($points->[0], 'ARRAY')) { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ($self->lons_first) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | @points = map {[ $_->[1], $_->[0] ]} @$points; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | @points = map {[ $_->[0], $_->[1] ]} @$points; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | die "don't know how to handle points = $points"; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | return $self->points( \@points ); | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # The main entry point | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 10 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 10 | my ($self, $points) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 10 | $self->reset_encoder | |||||||||||||||||||||||||||||||||||||||||
| 130 | ->set_points( $points ) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ->calculate_distances | ||||||||||||||||||||||||||||||||||||||||||
| 132 | ->encode_points | ||||||||||||||||||||||||||||||||||||||||||
| 133 | ->encode_levels; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 10 | my $eline = { | |||||||||||||||||||||||||||||||||||||||||
| 136 | points => $self->encoded_points, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | levels => $self->encoded_levels, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | num_levels => $self->num_levels, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | zoom_factor => $self->zoom_factor, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 10 | ▶ | if ($self->escape_encoded_points) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | # create string literals: | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $eline->{points} =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 10 | return $eline; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # The main function. Essentially the Douglas-Peucker algorithm, adapted for | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # encoding. Rather than simply eliminating points, we record their distance | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # from the segment which occurs at that recursive step. These distances are | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # then easily converted to zoom levels. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # Note: this function has been optimized and is quite long, sorry. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # Any further optimizations should probably be done in XS. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 10 | ▶ | sub calculate_distances { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calculate_distances: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 10 | my $points = $self->points; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 10 | my @dists; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 10 | my $max_dist = 0; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 10 | ▶ | if (@$points <= 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | # no point doing distance calcs: | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | return $self->dists( \@dists )->max_dist( $max_dist ); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # cache commonly used vars: | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | my $visible_threshold = $self->visible_threshold; | |||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | # Iterate through all the points, and calculate their dists | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | # Each stack element contains the index of two points representing a line | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # seg that we calculate distances from. Start off with the first & last pt: | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 7 | my @stack = ([0, @$points - 1]); | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 7 | ▶ | while (@stack > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 2375 | my $current = pop @stack; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # cache to save array lookups: | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 2375 | my $current_0 = $current->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 2375 | my $current_1 = $current->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # Get the two points, $A & $B: | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 2375 | my ($A, $B) = ($points->[$current_0], $points->[$current_1]); | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | # Cache their lon/lats to avoid unneccessary array lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # Note: we use X/Y because it's shorter, and more math-y | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 2375 | my ($Ax, $Ay, $Bx, $By) = ($A->[1], $A->[0], $B->[1], $B->[0]); | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | # Create a line segment between $A & $B and calculate its length | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # Note: cache the square of the seg length for use in calcs later... | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 2375 | my $seg_length_squared = (($Bx - $Ax) ** 2 + ($By - $Ay) ** 2); | |||||||||||||||||||||||||||||||||||||||||
| 195 | 2375 | my $seg_length = sqrt($seg_length_squared); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 2375 | my $seg_length_is_0 = $seg_length == 0; # cache | |||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | # Cache the deltas in x/y for calcs later: | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 2375 | my ($Bx_minus_Ax, $By_minus_Ay) = ($Bx - $Ax, $By - $Ay); | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 2375 | my $current_max_dist = 0; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 2375 | my $current_max_dist_idx; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 2375 | ▶ | for (my $i = $current_0 + 1; $i < $current_1; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | # Get the current point: | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 16721 | my $P = $points->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # Cache its lon/lat to avoid unneccessary hash lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # Note: we use X/Y because it's shorter, and more math-y | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 16721 | my ($Py, $Px) = ($P->[0], $P->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # Compute the distance between point $P and line segment [$A, $B]. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # Maths borrowed from Philip Nicoletti (see below). | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # Note: we approximate distance using flat (Euclidian) geometry, | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # rather than trying to bring the curvature of the earth into it. | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # This greatly simplifies things, and makes the calcs faster... | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # Note: distance calculations have been brought in-line as the | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # majority of encoding time was spent calling the 'distance' | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # method. This way we can avoid passing lots of data by value, | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # setting up the sub stack, and we can also cache some values. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | #my $dist = $self->distance($points->[$i], $A, $B, $seg_length, $seg_length_squared); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 16721 | my $dist; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 16721 | ▶ | if ($seg_length_is_0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | # The line is really just a point, so calc dist between it and $P: | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $dist = sqrt(($By - $Py) ** 2 + ($Bx - $Px) ** 2); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # Thanks to Philip Nicoletti's explanation: | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # http://www.codeguru.com/forum/printthread.php?t=194400 | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # So, to find out how far the line segment (AB) is from the point (P), | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # let 'I' be the point of perpendicular projection of P on AB. The | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # parameter 'r' indicates I's position along AB, and is computed by | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # the dot product of AP and AB divided by the square of the length | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # of AB: | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # AP . AB (Px-Ax)(Bx-Ax) + (Py-Ay)(By-Ay) | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # r = -------- = ------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # ||AB||^2 L^2 | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # r can be interpreded ala: | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # r=0 I = A | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # r=1 I = B | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # r<0 I is on the backward extension of A-B | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # r>1 I is on the forward extension of A-B | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # 0<r<1 I is interior to A-B | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # In cases 1-4 we can simply use the distance between P and either A or B. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # In case 5 we can use the distance between I and P. To do that we need to | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # find I: | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # Ix = Ax + r(Bx-Ax) | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # Iy = Ay + r(By-Ay) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # And the distance from A to I = r*L. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # Use another parameter s to indicate the location along IP, with the | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # following meaning: | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # s<0 P is left of AB | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # s>0 P is right of AB | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # s=0 P is on AB | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # Compute s as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # (Ay-Py)(Bx-Ax) - (Ax-Px)(By-Ay) | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # s = ------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # L^2 | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # Then the distance from P to I = |s|*L. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 16721 | my $r = (($Px - $Ax) * ($Bx - $Ax) + | |||||||||||||||||||||||||||||||||||||||||
| 273 | ($Py - $Ay) * ($By - $Ay)) / $seg_length_squared; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 16721 | ▶ | if ($r <= 0.0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | # Either I = A, or I is on the backward extension of A-B, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # so find dist between $P & $A: | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 4 | $dist = sqrt(($Ay - $Py) ** 2 + ($Ax - $Px) ** 2); | |||||||||||||||||||||||||||||||||||||||||
| 279 | } elsif ($r >= 1.0) { | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # Either I = B, or I is on the forward extension of A-B, | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # so find dist between $P & $B: | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 116 | $dist = sqrt(($By - $Py) ** 2 + ($Bx - $Px) ** 2); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # I is interior to A-B, so find $s, and use it to find the | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # dist between $P and A-B: | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 16601 | my $s = (($Ay - $Py) * $Bx_minus_Ax - | |||||||||||||||||||||||||||||||||||||||||
| 287 | ($Ax - $Px) * $By_minus_Ay) / $seg_length_squared; | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 16601 | $dist = abs($s) * $seg_length; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # warn "\t$Px\t$Py\t$Ax\t$Ay\t$Bx\t$By\t$r\t$dist\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | # See if this distance is the greatest for this segment so far: | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 16721 | ▶ | if ($dist > $current_max_dist) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 5388 | $current_max_dist = $dist; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 5388 | $current_max_dist_idx = $i; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 5388 | ▶ | if ($current_max_dist > $max_dist) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 289 | $max_dist = $current_max_dist; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # If the point that had the greatest distance from the line seg is | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # also greater than our threshold, process again using it as a new | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # start/end point for the line. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 2375 | ▶ | if ($current_max_dist > $visible_threshold) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | # store this distance - we'll use it later when creating zoom values | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 1184 | $dists[$current_max_dist_idx] = $current_max_dist; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 1184 | push @stack, [$current_0, $current_max_dist_idx]; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 1184 | push @stack, [$current_max_dist_idx, $current_1]; | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 7 | $self->dists( \@dists )->max_dist( $max_dist ); | |||||||||||||||||||||||||||||||||||||||||
| 315 | } # calculate_distances | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | # The encode_points function is very similar to Google's | ||||||||||||||||||||||||||||||||||||||||||
| 319 | # http://www.google.com/apis/maps/documentation/polyline.js | ||||||||||||||||||||||||||||||||||||||||||
| 320 | # The key difference is that not all points are encoded, | ||||||||||||||||||||||||||||||||||||||||||
| 321 | # since some were eliminated by Douglas-Peucker. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 10 | ▶ | sub encode_points { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_points: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 10 | my $points = $self->points; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 10 | my $dists = $self->dists; | |||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 10 | my $encoded_points = ""; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 10 | my $oldencoded_points = ""; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 10 | my ($last_lat, $last_lon) = (0.0, 0.0); | |||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 10 | ▶ | for (my $i = 0; $i < @$points; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 1581 | my $point = $points->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 1581 | my $lat = $point->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 1581 | my $lon = $point->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 1581 | ▶ | if (defined($dists->[$i]) || $i == 0 || $i == @$points - 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $dists->[$i] or $i == 0 or $i == @$points - 1
defined $dists->[$i] or $i == 0
MC/DC100%3/3
defined $dists->[$i] or $i == 0 or $i == @$points - 1
defined $dists->[$i]
$i == 0
$i == @$points - 1
Truth table100%4/4
A or B or C
A= defined $dists->[$i]B= $i == 0C= $i == @$points - 1
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | # compute deltas, rounded to 5 decimal places: | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 1204 | my $lat_e5 = sprintf('%.5f', $lat)+0; # round() | |||||||||||||||||||||||||||||||||||||||||
| 339 | 1204 | my $lon_e5 = sprintf('%.5f', $lon)+0; # round() | |||||||||||||||||||||||||||||||||||||||||
| 340 | 1204 | my $delta_lat = sprintf('%.5f', $lat_e5 - $last_lat)+0; | |||||||||||||||||||||||||||||||||||||||||
| 341 | 1204 | my $delta_lon = sprintf('%.5f', $lon_e5 - $last_lon)+0; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 1204 | ($last_lat, $last_lon) = ($lat_e5, $lon_e5); | |||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 1204 | $encoded_points .= | |||||||||||||||||||||||||||||||||||||||||
| 345 | $self->encode_signed_number($delta_lat) . | ||||||||||||||||||||||||||||||||||||||||||
| 346 | $self->encode_signed_number($delta_lon); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # warn "skipping point: $lat, $lon"; | ||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 10 | $self->encoded_points( $encoded_points ); | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | # Use compute_level to march down the list of points and encode the levels. | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # Like encode_points, we ignore points whose distance (in dists) is undefined. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # See http://code.google.com/apis/maps/documentation/polylinealgorithm.html | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 10 | ▶ | sub encode_levels { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_levels: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 10 | my $points = $self->points; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 10 | my $dists = $self->dists; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 10 | my $max_dist = $self->max_dist; | |||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | # Cache for performance: | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 10 | my $num_levels = $self->num_levels; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 10 | my $num_levels_minus_1 = $num_levels - 1; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 10 | my $visible_threshold = $self->visible_threshold; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 10 | my $zoom_level_breaks = $self->zoom_level_breaks; | |||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 10 | my $encoded_levels = ""; | |||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 10 | ▶ | if ($self->force_endpoints) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 10 | $encoded_levels .= $self->encode_number($num_levels_minus_1); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | $encoded_levels .= $self->encode_number($num_levels_minus_1 - $self->compute_level($max_dist)); | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | # Note: skip the first & last point: | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 10 | for my $i (1 .. scalar(@$points) - 2) { | |||||||||||||||||||||||||||||||||||||||||
| 382 | 1561 | my $dist = $dists->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 1561 | ▶ | if (defined $dist) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | # Note: brought compute_level in-line as it was performing *really* slowly | ||||||||||||||||||||||||||||||||||||||||||
| 385 | # | ||||||||||||||||||||||||||||||||||||||||||
| 386 | # This computes the appropriate zoom level of a point in terms of it's | ||||||||||||||||||||||||||||||||||||||||||
| 387 | # distance from the relevant segment in the DP algorithm. Could be done | ||||||||||||||||||||||||||||||||||||||||||
| 388 | # in terms of a logarithm, but this approach makes it a bit easier to | ||||||||||||||||||||||||||||||||||||||||||
| 389 | # ensure that the level is not too large. | ||||||||||||||||||||||||||||||||||||||||||
| 390 | #my $level = $self->compute_level($dist); | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 1184 | my $level = 0; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 1184 | ▶ | if ($dist > $visible_threshold) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 1184 | ▶ | while ($dist < $zoom_level_breaks->[$level]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 12431 | $level++; | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | 1184 | $encoded_levels .= $self->encode_number($num_levels_minus_1 - $level); | |||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | 10 | ▶ | if ($self->force_endpoints) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | 10 | $encoded_levels .= $self->encode_number($num_levels_minus_1); | |||||||||||||||||||||||||||||||||||||||||
| 404 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | $encoded_levels .= $self->encode_number($num_levels_minus_1 - $self->compute_level($max_dist)); | |||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 10 | $self->encoded_levels( $encoded_levels ); | |||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | # This computes the appropriate zoom level of a point in terms of it's | ||||||||||||||||||||||||||||||||||||||||||
| 413 | # distance from the relevant segment in the DP algorithm. Could be done | ||||||||||||||||||||||||||||||||||||||||||
| 414 | # in terms of a logarithm, but this approach makes it a bit easier to | ||||||||||||||||||||||||||||||||||||||||||
| 415 | # ensure that the level is not too large. | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | sub compute_level { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compute_level: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | my ($self, $dist) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | # Cache for performance: | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | my $zoom_level_breaks = $self->zoom_level_breaks; | |||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | my $level; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | if ($dist > $self->visible_threshold) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | $level = 0; | |||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | while ($dist < $zoom_level_breaks->[$level]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | $level++; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | return $level; | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | # Based on the official google example | ||||||||||||||||||||||||||||||||||||||||||
| 434 | # http://code.google.com/apis/maps/documentation/include/polyline.js | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 2412 | ▶ | sub encode_signed_number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_signed_number: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 2412 | my ($self, $orig_num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | # 1. Take the initial signed value: | ||||||||||||||||||||||||||||||||||||||||||
| 439 | # 2. Take the decimal value and multiply it by 1e5, rounding the result: | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | # Note 1: we limit the number to 5 decimal places with sprintf to avoid | ||||||||||||||||||||||||||||||||||||||||||
| 442 | # perl's rounding errors (they can throw the line off by a big margin sometimes) | ||||||||||||||||||||||||||||||||||||||||||
| 443 | # From Geo::Google: use the correct floating point precision or else | ||||||||||||||||||||||||||||||||||||||||||
| 444 | # 34.06694 - 34.06698 will give you -3.999999999999999057E-5 which doesn't | ||||||||||||||||||||||||||||||||||||||||||
| 445 | # encode properly. -4E-5 encodes properly. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | # Note 2: we use sprintf(%.0f ...) rather than int() for similar reasons | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # (see perldoc -f int), though there's not much in it and the sprintf approach | ||||||||||||||||||||||||||||||||||||||||||
| 449 | # ends up doing more of a round() than a floor() in some cases: | ||||||||||||||||||||||||||||||||||||||||||
| 450 | # floor = -30 num=-30 *int=-29 1e5=-30 %3.5f=-0.00030 orig=-0.000300000000009959 | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # floor = 119 *num=120 int=119 1e5=120 %3.5f=0.00120 orig=0.0011999999999972 | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | # Note 3: We don't use floor() to avoid a dependency on POSIX. And it | ||||||||||||||||||||||||||||||||||||||||||
| 454 | # doesn't round() anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | # do this in a series of steps so we can see what's going on in the debugger: | ||||||||||||||||||||||||||||||||||||||||||
| 457 | 2412 | my $num3_5 = sprintf('%.5f', $orig_num)+0; # round at 5 decimal places | |||||||||||||||||||||||||||||||||||||||||
| 458 | 2412 | my $num_1e5 = $num3_5 * 1e5; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 2412 | my $num = sprintf('%.0f', $num_1e5)+0; # think int(...) | |||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | # RT 49327: the signedness has to be determined *after* rounding | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 2412 | my $is_negative = $num < 0; | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | { | ||||||||||||||||||||||||||||||||||||||||||
| 465 | # 3. Convert the decimal value to binary. Note that a negative value | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # must be calculated using its two's complement by inverting the | ||||||||||||||||||||||||||||||||||||||||||
| 467 | # binary value and adding one to the result. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | # Note: perl ints are already binary, but bitwise operators work on | ||||||||||||||||||||||||||||||||||||||||||
| 470 | # the assumption they are unsigned, ie ~$num => one's complement. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | # if we 'use integer' bitwise operands are treated as signed: | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 3 | ▶ | use integer; # force 2's complement | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | # 4. Left-shift the binary value one bit: | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 2412 | $num = $num << 1; | |||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | # 5. If the original decimal value is negative, invert this encoding: | ||||||||||||||||||||||||||||||||||||||||||
| 478 | # (see note on RT 49327 above) | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 2412 | ▶ | if ($is_negative) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 626 | $num = ~$num; | |||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | 2412 | return $self->encode_number($num); | |||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | # Based on the official google example | ||||||||||||||||||||||||||||||||||||||||||
| 488 | # http://code.google.com/apis/maps/documentation/include/polyline.js | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 3618 | ▶ | sub encode_number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_number: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 3618 | my ($self, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 3 | ▶ | no integer; # treat bitwise operands as unsigned | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | # 6. Break the binary value out into 5-bit chunks (starting from the right hand side): | ||||||||||||||||||||||||||||||||||||||||||
| 494 | # 7. Place the 5-bit chunks into reverse order: | ||||||||||||||||||||||||||||||||||||||||||
| 495 | # 8. OR each value with 0x20 if another bit chunk follows: | ||||||||||||||||||||||||||||||||||||||||||
| 496 | # 9. Convert each value to decimal: | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # 10. Add 63 to each value: | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | 3618 | my $encodeString = ""; | |||||||||||||||||||||||||||||||||||||||||
| 500 | 3618 | ▶ | while ($num >= 0x20) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 1974 | my $nextValue = (0x20 | ($num & 0x1f)) + 63; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 1974 | $encodeString .= chr( $nextValue ); | |||||||||||||||||||||||||||||||||||||||||
| 503 | 1974 | $num >>= 5; | |||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 3618 | my $finalValue = $num + 63; | |||||||||||||||||||||||||||||||||||||||||
| 507 | 3618 | $encodeString .= chr( $finalValue ); | |||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 3618 | return $encodeString; | |||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | # Superficial validation of encoded points. Note that decode_points | ||||||||||||||||||||||||||||||||||||||||||
| 513 | # does not check that points are validated before decoding. | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 6 | ▶ | sub validate_encoded_points { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_encoded_points: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 515 | 6 | my ($class, $encoded) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | 6 | ▶ | return unless (defined $encoded && $encoded ne ""); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $encoded and $encoded ne ""
MC/DC0%0/2
defined $encoded and $encoded ne ""
defined $encoded
$encoded ne ""
Truth table33%1/3
A and B
A= defined $encodedB= $encoded ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | 6 | my @ords = unpack "c*", $encoded; | |||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 6 | ▶ | my @out = grep { $_ < 63 || $_ > 127 } @ords; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$_ < 63 || $_ > 127
MC/DC0%0/2
$_ < 63 || $_ > 127
$_ < 63
$_ > 127
Truth table33%1/3
A || B
A= $_ < 63B= $_ > 127
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 6 | ▶ | return if @out; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | # Decode an encoded polyline into a list of lat/lng tuples. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # adapted from http://code.google.com/apis/maps/documentation/include/polyline.js | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 12 | ▶ | sub decode_points { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_points: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 12 | my ($class, $encoded) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 12 | my $len = length( $encoded ); | |||||||||||||||||||||||||||||||||||||||||
| 533 | 12 | my @array; | |||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 12 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 536 | 12 | my $lat = 0; | |||||||||||||||||||||||||||||||||||||||||
| 537 | 12 | my $lon = 0; | |||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 12 | ▶ | while ($index < $len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | { | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 2339 | my $b; | |||||||||||||||||||||||||||||||||||||||||
| 542 | 2339 | my $shift = 0; | |||||||||||||||||||||||||||||||||||||||||
| 543 | 2339 | my $result = 0; | |||||||||||||||||||||||||||||||||||||||||
| 544 | 2339 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 4194 | $b = ord( substr( $encoded, $index++, 1 ) ) - 63; | |||||||||||||||||||||||||||||||||||||||||
| 546 | 4194 | $result |= ($b & 0x1f) << $shift; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 4194 | $shift += 5; | |||||||||||||||||||||||||||||||||||||||||
| 548 | } while ($b >= 0x20); | ||||||||||||||||||||||||||||||||||||||||||
| 549 | 2339 | my $dlat = $result >> 1; | |||||||||||||||||||||||||||||||||||||||||
| 550 | 2339 | ▶ | if ($result & 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | 3 | ▶ | use integer; # force 2's complement | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 706 | $dlat = ~$dlat; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 2339 | $lat += $dlat; | |||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | # cut-n-paste to improve performance? | ||||||||||||||||||||||||||||||||||||||||||
| 557 | 2339 | $shift = 0; | |||||||||||||||||||||||||||||||||||||||||
| 558 | 2339 | $result = 0; | |||||||||||||||||||||||||||||||||||||||||
| 559 | 2339 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 560 | 4248 | $b = ord( substr( $encoded, $index++, 1 ) ) - 63; | |||||||||||||||||||||||||||||||||||||||||
| 561 | 4248 | $result |= ($b & 0x1f) << $shift; | |||||||||||||||||||||||||||||||||||||||||
| 562 | 4248 | $shift += 5; | |||||||||||||||||||||||||||||||||||||||||
| 563 | } while ($b >= 0x20); | ||||||||||||||||||||||||||||||||||||||||||
| 564 | 2339 | my $dlon = $result >> 1; | |||||||||||||||||||||||||||||||||||||||||
| 565 | 2339 | ▶ | if ($result & 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | 3 | ▶ | use integer; # force 2's complement | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | 498 | $dlon = ~$dlon; | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 2339 | $lon += $dlon; | |||||||||||||||||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | 2339 | push @array, { lat => $lat * 1e-5, lon => $lon * 1e-5 }; | |||||||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | 12 | return \@array; | |||||||||||||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | # Decode an encoded levels string into a list of levels. | ||||||||||||||||||||||||||||||||||||||||||
| 579 | # adapted from http://code.google.com/apis/maps/documentation/include/polyline.js | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 10 | ▶ | sub decode_levels { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_levels: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | 10 | my ($class, $encoded) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | 10 | my $len = length( $encoded ); | |||||||||||||||||||||||||||||||||||||||||
| 584 | 10 | my @levels; | |||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | 10 | ▶ | for (my $index = 0; $index < $len; $index++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 2317 | my $level = ord( substr( $encoded, $index, 1 ) ) - 63; | |||||||||||||||||||||||||||||||||||||||||
| 588 | 2317 | push @levels, $level; | |||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | 10 | return \@levels; | |||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | __END__ | ||||||||||||||||||||||||||||||||||||||||||