blib/lib/Geo/GoogleMaps/FitBoundsZoomer.pm

filter: Statementstmt 100% 98 / 98 Branchbran 78.2% 36 / 46 Conditioncond 80.7% 21 / 26 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 89.6% 182 / 203 CC 37 SCAR 21.1
CC
37
Cov
91%
max_bounding_zoom
13.0
_get_bounds
11.3
_zoom_level
8.2
CRAP 8.2
↑ Summary
1 package Geo::GoogleMaps::FitBoundsZoomer;
2  
3 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.03';
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use constant ZOOM_LIMIT => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant PI => 3.141592653589793;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 33 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 33 my ($class, %params) = @_;
18  
19 33 my $points = delete $params{points};
20 33 my $map_width = delete $params{width};
21 33 my $map_height = delete $params{height};
22 33 ▶ my $zoom_limit = delete $params{zoom_limit} // ZOOM_LIMIT;
Condition100%2/2
delete $params{"zoom_limit"} // 20
l!l
429
MC/DC100%1/1
delete $params{"zoom_limit"} // 20
delete $params{"zoom_limit"}
Truth table100%2/2
A
A= delete $params{"zoom_limit"}
Aresult
11
00
23
24 33 bless {
25 points => $points,
26 width => $map_width,
27 height => $map_height,
28 zoom_limit => $zoom_limit
29 }, $class;
30 }
31  
32 # returns max zoom for a min bounding box
33 31 ▶sub max_bounding_zoom {
Subroutine max_bounding_zoom: called CC=13 SCAR=25.7
Pod documented
34 31 my $self = shift;
35  
36 31 ▶ if ( @_ || ! defined $self->{max_bounding_zoom} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ or not defined $self->{"max_bounding_zoom"}) 30 1 31
Condition100%3/3
@_ or not defined $self->{"max_bounding_zoom"}
l!l&&r!l&&!r
6241
MC/DC100%2/2
@_ or not defined $self->{"max_bounding_zoom"}
@_ not defined $self->{"max_bounding_zoom"}
Truth table100%3/3
A or not(B)
A= @_B= not defined $self->{"max_bounding_zoom"}
ABresult
1X1
011
000
37  
38 30 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 24 30
39 6 my %params = @_;
40  
41 6 undef $self->{map_center};
42 6 my @all_params = ('points', 'width', 'height', 'zoom_limit');
43 6 ▶ $self->{$_} = delete $params{$_} // $self->{$_} for @all_params;
Condition66%2/3
delete $params{$_} // $self->{$_}
l!l&&r!l&&!r
11130
MC/DC0%0/2
delete $params{$_} // $self->{$_}
delete $params{$_} $self->{$_}
Truth table66%2/3
A // B
A= delete $params{$_}B= $self->{$_}
ABresult
1X1
011
000
44  
45 }
46
47 30 foreach ('points', 'width', 'height') {
48 croak "No map $_ parameter! Usage: max_bounding_zoom( points => \$points, width => \$map_width, height => \$map_height )"
49 87 ▶ unless defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 3 84 87
50 }
51
52 27 ▶ croak "At least one point must be provided!" unless @{$self->{points}} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"points"};} > 0 1 26 27
53 26 ▶ croak "Map width must be a positive number!" unless $self->{width} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"width"} > 0 1 25 26
54 25 ▶ croak "Map height must be a positive number!" unless $self->{height} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"height"} > 0 1 24 25
55 24 ▶ croak "Zoom limit must be greater of equal to 0!" unless $self->{zoom_limit} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"zoom_limit"} >= 0 1 23 24
56
57 23 $self->{bounds} = $self->_get_bounds();
58 19 $self->{max_bounding_zoom} = $self->_zoom_level( $self->{bounds} );
59
60 }
61  
62 croak "Insufficient data to calculate maximum zoom! Usage: max_bounding_zoom( points => \$points, width => \$map_width, height => \$map_height )"
63 20 ▶ unless defined $self->{max_bounding_zoom};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_bounding_zoom"} 0 20 20
64  
65 20 return $self->{max_bounding_zoom};
66 }
67  
68 # returns center of the rectangular bounding box
69 5 ▶sub bounding_box_center {
Subroutine bounding_box_center: called CC=4 SCAR=13.9
Pod documented
70 5 my $self = shift;
71
72 croak "Map data not initialized! max_bounding_zoom needs to be called first. Usage: max_bounding_zoom( points => \$points, width => \$map_width, height => \$map_height )"
73 5 ▶ if !$self->{points} || !$self->{bounds};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"points"} and $self->{"bounds"} 2 3 5
Condition100%3/3
$self->{"points"} and $self->{"bounds"}
!ll&&!rl&&r
113
MC/DC100%2/2
$self->{"points"} and $self->{"bounds"}
$self->{"points"} $self->{"bounds"}
Truth table100%3/3
A and B
A= $self->{"points"}B= $self->{"bounds"}
ABresult
0X0
100
111
74  
75 3 ▶ if ( ! $self->{map_center} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"map_center"}) 3 0 3
76 3 my $center;
77 3 my $bounds = $self->{bounds};
78  
79 3 my ($blp, $trp) = ($bounds->{blp}, $bounds->{trp});
80  
81 3 $center->{lat} = ( ($trp->{lat} - $blp->{lat}) / 2 ) + $blp->{lat};
82 3 $center->{long} = ( ($trp->{long} - $blp->{long}) / 2 ) + $blp->{long};
83  
84 3 $self->{map_center} = $center;
85 }
86  
87 3 return $self->{map_center};
88 }
89  
90 # returns a bounding box (bottom left and top right point) for coordinates
91 30 ▶sub _get_bounds {
Subroutine _get_bounds: called CC=11 SCAR=24.2
92 30 my $self = shift;
93  
94 30 my $blp = { 'lat' => 90, 'long' => 180 }; # bottom left point
95 30 my $trp = { 'lat' => -90, 'long' => -180 }; # top right point
96
97 my $points = $self->{points}
98 30 ▶ or croak "Cannot calculate map bounds without points!";
Branch: 1 branch
BranchTrueFalseTotal
my $points = $self->{"points"} or croak("Cannot calculate map bounds without points!") 30 0 30
99  
100 30 foreach my $point (@$points) {
101
102 55 my ($lat, $lng) = ($point->{lat}, $point->{long});
103
104 55 $blp->{'lat'} = min ($blp->{'lat'}, $lat);
105 55 $trp->{'lat'} = max ($trp->{'lat'}, $lat);
106 55 $blp->{'long'} = min ($blp->{'long'}, $lng);
107 55 $trp->{'long'} = max ($trp->{'long'}, $lng);
108 }
109
110 30 ▶ croak "Point latitude out of bounds ( < -90 or > 90 )" unless ( -90 <= $blp->{'lat'} && $blp->{'lat'} <= 90 );
Branch: 1 branch
BranchTrueFalseTotal
unless -90 <= $blp->{"lat"} and $blp->{"lat"} <= 90 1 29 30
Condition66%2/3
-90 <= $blp->{"lat"} and $blp->{"lat"} <= 90
!ll&&!rl&&r
1029
MC/DC50%1/2
-90 <= $blp->{"lat"} and $blp->{"lat"} <= 90
-90 <= $blp->{"lat"} $blp->{"lat"} <= 90
Truth table66%2/3
A and B
A= -90 <= $blp->{"lat"}B= $blp->{"lat"} <= 90
ABresult
0X0
100
111
111 29 ▶ croak "Point latitude out of bounds ( < -90 or > 90 )" unless ( -90 <= $trp->{'lat'} && $trp->{'lat'} <= 90 );
Branch: 1 branch
BranchTrueFalseTotal
unless -90 <= $trp->{"lat"} and $trp->{"lat"} <= 90 1 28 29
Condition66%2/3
-90 <= $trp->{"lat"} and $trp->{"lat"} <= 90
!ll&&!rl&&r
0128
MC/DC50%1/2
-90 <= $trp->{"lat"} and $trp->{"lat"} <= 90
-90 <= $trp->{"lat"} $trp->{"lat"} <= 90
Truth table66%2/3
A and B
A= -90 <= $trp->{"lat"}B= $trp->{"lat"} <= 90
ABresult
0X0
100
111
112 28 ▶ croak "Point longitude out of bounds ( < -180 or > 180 )" unless ( -180 <= $blp->{'long'} && $blp->{'long'} <= 180 );
Branch: 1 branch
BranchTrueFalseTotal
unless -180 <= $blp->{"long"} and $blp->{"long"} <= 180 1 27 28
Condition66%2/3
-180 <= $blp->{"long"} and $blp->{"long"} <= 180
!ll&&!rl&&r
1027
MC/DC50%1/2
-180 <= $blp->{"long"} and $blp->{"long"} <= 180
-180 <= $blp->{"long"} $blp->{"long"} <= 180
Truth table66%2/3
A and B
A= -180 <= $blp->{"long"}B= $blp->{"long"} <= 180
ABresult
0X0
100
111
113 27 ▶ croak "Point longitude out of bounds ( < -180 or > 180 )" unless ( -180 <= $trp->{'long'} && $trp->{'long'} <= 180 );
Branch: 1 branch
BranchTrueFalseTotal
unless -180 <= $trp->{"long"} and $trp->{"long"} <= 180 1 26 27
Condition66%2/3
-180 <= $trp->{"long"} and $trp->{"long"} <= 180
!ll&&!rl&&r
0126
MC/DC50%1/2
-180 <= $trp->{"long"} and $trp->{"long"} <= 180
-180 <= $trp->{"long"} $trp->{"long"} <= 180
Truth table66%2/3
A and B
A= -180 <= $trp->{"long"}B= $trp->{"long"} <= 180
ABresult
0X0
100
111
114  
115 26 return { 'blp' => $blp, 'trp' => $trp };
116 }
117  
118 # returns max bounding zoom level given a set of points, map width and height
119 19 ▶sub _zoom_level {
Subroutine _zoom_level: called CC=8 SCAR=21.1
120 19 my ($self, $bounds) = @_;
121  
122 19 ▶ croak "Map bounds not set!" if !$bounds;
Branch: 1 branch
BranchTrueFalseTotal
unless $bounds 0 19 19
123  
124 19 my ($width, $height) = ($self->{width}, $self->{height});
125
126 19 ▶ croak "Map width not set!" unless defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 19 19
127 19 ▶ croak "Map height not set!" unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 0 19 19
128
129 19 my $zoom_limit = $self->{zoom_limit};
130  
131 19 my ($blp, $trp) = ($bounds->{blp}, $bounds->{trp});
132
133 19 foreach my $zoom_level (reverse (0 .. $zoom_limit)) {
134 188 my $blpxl = $self->_coord2pix($blp->{lat}, $blp->{long}, $zoom_level);
135 188 my $trpxl = $self->_coord2pix($trp->{lat}, $trp->{long}, $zoom_level);
136
137 188 ▶ $blpxl->{x} -= (2**($zoom_level + 8)) if ( $blpxl->{x} > $trpxl->{x} );
Branch: 1 branch
BranchTrueFalseTotal
if $blpxl->{"x"} > $trpxl->{"x"} 0 188 188
138 188 my $delta={ x => abs($trpxl->{x} - $blpxl->{x}), y => abs($trpxl->{y} - $blpxl->{y}) };
139 188 ▶ return $zoom_level if ( ($delta->{x} <= $width) && ($delta->{y} <= $height) );
Branch: 1 branch
BranchTrueFalseTotal
if $delta->{"x"} <= $width and $delta->{"y"} <= $height 18 170 188
Condition100%3/3
$delta->{"x"} <= $width and $delta->{"y"} <= $height
!ll&&!rl&&r
169118
MC/DC100%2/2
$delta->{"x"} <= $width and $delta->{"y"} <= $height
$delta->{"x"} <= $width $delta->{"y"} <= $height
Truth table100%3/3
A and B
A= $delta->{"x"} <= $widthB= $delta->{"y"} <= $height
ABresult
0X0
100
111
140 }
141 1 return 0;
142 }
143  
144 # returns an X,Y pixel cordinate from $lat, $lng coordinates for a given zoom level
145 376 ▶sub _coord2pix {
Subroutine _coord2pix: called CC=4 SCAR=14.0
146 #values hash is the output of google_magicâ„¢ for a given zoom level
147 376 my ($self, $lat, $lng, $zoom ) = @_;
148
149 376 ▶ croak "Latitude not set!" unless defined $lat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lat 0 376 376
150 376 ▶ croak "Longitude not set!" unless defined $lng;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lng 0 376 376
151 376 ▶ croak "Zoom not set!" unless defined $zoom;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $zoom 0 376 376
152  
153 376 my $center_point = 2**($zoom + 7);
154 376 my $total_pixels = $center_point*2;
155 376 my $pixels_per_lng_degree = $total_pixels / 360;
156 376 my $pixels_per_lng_radian = $total_pixels / (2 * PI);
157 376 my $siny = min ( max( sin( $lat*(PI/180) ), -0.99999999 ), 0.99999999 );
158  
159 376 my $coord;
160 376 $coord->{x} = $center_point + $lng * $pixels_per_lng_degree;
161 376 $coord->{y} = $center_point - 0.5 * log((1+$siny)/(1-$siny)) * $pixels_per_lng_radian;
162
163 376 return $coord;
164 }
165  
166 2 1; # end of Geo::GoogleMaps::FitBoundsZoomer
167  
168 __END__
↑ Summary