| 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
MC/DC100%1/1
delete $params{"zoom_limit"} // 20
delete $params{"zoom_limit"}
Truth table100%2/2
A
A= delete $params{"zoom_limit"}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
@_ or not defined $self->{"max_bounding_zoom"}
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"}
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 30 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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->{$_}
MC/DC0%0/2
delete $params{$_} // $self->{$_}
delete $params{$_}
$self->{$_}
Truth table66%2/3
A // B
A= delete $params{$_}B= $self->{$_}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 27 | ▶ | croak "At least one point must be provided!" unless @{$self->{points}} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 26 | ▶ | croak "Map width must be a positive number!" unless $self->{width} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 25 | ▶ | croak "Map height must be a positive number!" unless $self->{height} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 24 | ▶ | croak "Zoom limit must be greater of equal to 0!" unless $self->{zoom_limit} >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->{"points"} and $self->{"bounds"}
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"}
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | if ( ! $self->{map_center} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
-90 <= $blp->{"lat"} and $blp->{"lat"} <= 90
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
| |||||||||||||||||||||||||||||
| 111 | 29 | ▶ | croak "Point latitude out of bounds ( < -90 or > 90 )" unless ( -90 <= $trp->{'lat'} && $trp->{'lat'} <= 90 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-90 <= $trp->{"lat"} and $trp->{"lat"} <= 90
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
| |||||||||||||||||||||||||||||
| 112 | 28 | ▶ | croak "Point longitude out of bounds ( < -180 or > 180 )" unless ( -180 <= $blp->{'long'} && $blp->{'long'} <= 180 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-180 <= $blp->{"long"} and $blp->{"long"} <= 180
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
| |||||||||||||||||||||||||||||
| 113 | 27 | ▶ | croak "Point longitude out of bounds ( < -180 or > 180 )" unless ( -180 <= $trp->{'long'} && $trp->{'long'} <= 180 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-180 <= $trp->{"long"} and $trp->{"long"} <= 180
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 19 | my ($width, $height) = ($self->{width}, $self->{height}); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 19 | ▶ | croak "Map width not set!" unless defined $width; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 19 | ▶ | croak "Map height not set!" unless defined $height; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$delta->{"x"} <= $width and $delta->{"y"} <= $height
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 150 | 376 | ▶ | croak "Longitude not set!" unless defined $lng; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 376 | ▶ | croak "Zoom not set!" unless defined $zoom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||