lib/Geo/Heatmap/USNaviguide_Google_Tiles.pm

filter: Statementstmt 50.7% 67 / 132 Branchbran 11.1% 4 / 36 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 8 totaltotal 40.6% 83 / 204 CC 22 SCAR 37.4
CC
22
Cov
41%
Google_Tiles
90.0
PixtoTileName
72.0
Google_Tile_Factors
5.4
CRAP 42.1
↑ Summary lib/Geo/Heatmap.pm »
1 package Geo::Heatmap::USNaviguide_Google_Tiles ;
2 # Calculate tile characteristics given a bounding box of coordinates and a zoom...
3 # Author. John D. Coryat 01/2008...
4 # USNaviguide LLC.
5 # Published under Apache 2.0 license.
6 # Adapted from: Google Maps API Javascript...
7 ##
8 # In order to correctly locate objects of interest on a Custom Map Overlay Google Maps,
9 # the characteristics of each tile to build are required.
10 ##
11 # Google_Tiles # Calculate all tiles for a bounding box and zoom
12 # Google_Tile_Factors # Calculate the factors needed ( Zoom, Tilesize )
13 # Google_Tile_Calc # Calculate a single tile features from a tile name and zoom
14 # Google_Tile_to_Pix # Calculate tile name to pixel
15 # Google_Coord_to_Pix # Calculate coordinate to Pixel
16 # Google_Pix_to_Tile # Calculate a tile name from a pixel location and zoom
17  
18 2 require 5.003 ;
19 2 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Math::Trig ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use Exporter ;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ use vars qw ( $VERSION @ISA @EXPORT) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 $VERSION = 1.0 ;
26 2 @ISA = qw ( Exporter ) ;
27 2 @EXPORT = qw (
28 Google_Tiles
29 Google_Tile_Factors
30 Google_Tile_Calc
31 Google_Tile_to_Pix
32 Google_Coord_to_Pix
33 Google_Pix_to_Tile
34 ) ;
35 }
36  
37 #
38 # Call as: <array of Hashes> = &Google_Tiles(<LatitudeS>, <LongitudeW>, <LatitudeN>, <LongitudeE>, <Zoom>, [<option: tileSize>], [<option: Partial/Whole>]) ;
39 # Partial/Whole option: (Default: Partial)
40 # Partial: Include the edge to create partial tiles
41 # Whole: Include only tiles that are contained by the bounds
42 #
43 # Returned Array Specifications:
44 # Each element is a reference to a Hash:
45 # NAMEY - Tile Name y
46 # NAMEX - Tile Name x
47 # PYS - Pixel South
48 # PXW - Pixel West
49 # PYN - Pixel North
50 # PXE - Pixel East
51 # LATS - South Latitude
52 # LNGW - West Longitude
53 # LATN - North Latitude
54 # LNGE - East Longitude
55 #
56 # Note: X is width, Y is height...
57 #
58  
59 sub Google_Tiles
60 0 ▶{
Subroutine Google_Tiles: not called CC=9 SCAR=45.0
Pod undocumented
61 0 my $latS = shift ;
62 0 my $lngW = shift ;
63 0 my $latN = shift ;
64 0 my $lngE = shift ;
65 0 my $zoom = shift ;
66 0 my $tileSize = shift ;
67 0 my $parwho = shift ;
68 0 my $ty = 0 ;
69 0 my $tx = 0 ;
70 0 my @ret = ( ) ;
71 0 my %first = ( ) ; # First Results Hash
72 0 my %last = ( ) ; # Last Results Hash
73  
74 0 my $value = &Google_Tile_Factors($zoom, $tileSize) ; # Calculate Tile Factors
75  
76 0 ▶ if (!defined($parwho) or !$parwho)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parwho and $parwho) 0 0 0
Condition0%0/3
defined $parwho and $parwho
!ll&&!rl&&r
000
MC/DC0%0/2
defined $parwho and $parwho
defined $parwho $parwho
Truth table0%0/3
A and B
A= defined $parwhoB= $parwho
ABresult
0X0
100
111
77 {
78 0 $parwho = 'Partial' ;
79 }
80  
81 # NW: Convert Coordinates to Pixels...
82  
83 0 ($first{'NORTH'},$first{'WEST'}) = &Google_Coord_to_Pix( $value, $latN, $lngW ) ;
84  
85 # Convert Pixels to Tile Name...
86  
87 0 ($first{'NAMEY'},$first{'NAMEX'}) = &PixtoTileName( $value, $first{'NORTH'}, $first{'WEST'}, 'N', 'W', $parwho ) ;
88  
89 # SE: Convert Coordinates to Pixels...
90  
91 0 ($last{'SOUTH'},$last{'EAST'}) = &Google_Coord_to_Pix( $value, $latS, $lngE ) ;
92  
93 # Convert Pixels to Tile Name...
94  
95 0 ($last{'NAMEY'},$last{'NAMEX'}) = &PixtoTileName( $value, $last{'SOUTH'}, $last{'EAST'}, 'S', 'E', $parwho ) ;
96  
97 # Calculate tile values for all tiles...
98  
99 0 ▶ if ( $first{'NAMEX'} > $last{'NAMEX'} ) # Across the date line
Branch: 1 branch
BranchTrueFalseTotal
if ($first{'NAMEX'} > $last{'NAMEX'}) { } 0 0 0
100 {
101 0 ▶ for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($ty <= $last{"NAMEY"}) 0 0 0
102 {
103 0 ▶ for ( $tx = $first{'NAMEX'} ; $tx <= $$value{'max'} ; $tx++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($tx <= $value->{"max"}) 0 0 0
104 {
105 0 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
106 }
107 0 ▶ for ( $tx = 0 ; $tx <= $last{'NAMEX'} ; $tx++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($tx <= $last{"NAMEX"}) 0 0 0
108 {
109 0 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
110 }
111 }
112 } else
113 {
114 0 ▶ for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($ty <= $last{"NAMEY"}) 0 0 0
115 {
116 0 ▶ for ( $tx = $first{'NAMEX'} ; $tx <= $last{'NAMEX'} ; $tx++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($tx <= $last{"NAMEX"}) 0 0 0
117 {
118 0 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
119 }
120 }
121 }
122  
123 0 $ret[0]{'NORTH'} = $first{'NORTH'} ;
124 0 $ret[0]{'WEST'} = $first{'WEST'} ;
125  
126 0 $ret[$#ret]{'SOUTH'} = $last{'SOUTH'} ;
127 0 $ret[$#ret]{'EAST'} = $last{'EAST'} ;
128
129 0 return ( @ret ) ;
130 }
131  
132 # Calculate Tile Factors...
133  
134 sub Google_Tile_Factors
135 9 ▶{
Subroutine Google_Tile_Factors: called CC=5 SCAR=16.8
Pod undocumented
136 9 my $zoom = shift ;
137 9 my $tileSize = shift ;
138 9 my %value = ( ) ;
139  
140 # Validate and correct input parameters...
141  
142 9 ▶ if ( !defined($zoom) or $zoom < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $zoom or $zoom < 0) 0 9 9
Condition33%1/3
not defined $zoom or $zoom < 0
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $zoom or $zoom < 0
not defined $zoom $zoom < 0
Truth table33%1/3
not(A) or B
A= not defined $zoomB= $zoom < 0
ABresult
1X1
011
000
143 {
144 0 $zoom = 0 ;
145 }
146  
147 9 ▶ if ( !defined($tileSize) or !$tileSize )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tileSize and $tileSize) 9 0 9
Condition33%1/3
defined $tileSize and $tileSize
!ll&&!rl&&r
090
MC/DC0%0/2
defined $tileSize and $tileSize
defined $tileSize $tileSize
Truth table33%1/3
A and B
A= defined $tileSizeB= $tileSize
ABresult
0X0
100
111
148 {
149 9 $tileSize = 256 ;
150 }
151  
152 # Calculate Values...
153  
154 9 $value{'zoom'} = $zoom ;
155 9 $value{'PI'} = 3.1415926536 ;
156 9 $value{'bc'} = 2 * $value{'PI'} ;
157 9 $value{'Wa'} = $value{'PI'} / 180 ;
158 9 $value{'cp'} = 2 ** ($value{'zoom'} + 8) ;
159 9 $value{'max'} = (2 ** $value{'zoom'}) - 1 ; # Maximum Tile Number
160 9 $value{'pixLngDeg'}= $value{'cp'} / 360;
161 9 $value{'pixLngRad'}= $value{'cp'} / $value{'bc'} ;
162 9 $value{'bmO'} = $value{'cp'} / 2 ;
163 9 $value{'tileSize'} = $tileSize ;
164  
165 9 return \%value ;
166 }
167  
168 # Calculate tile values from Name...
169  
170 sub Google_Tile_Calc
171 9 ▶{
Subroutine Google_Tile_Calc: called CC=1 SCAR=0.0
Pod undocumented
172 9 my %result = ( ) ;
173 9 my $value = shift ;
174 9 $result{'NAMEY'} = shift ;
175 9 $result{'NAMEX'} = shift ;
176  
177 # Convert Tile Name to Pixels...
178  
179 9 ($result{'PYN'},$result{'PXW'}) = &Google_Tile_to_Pix( $value, $result{'NAMEY'}, $result{'NAMEX'} ) ;
180  
181 # Convert Pixels to Coordinates (Upper Left Corner)...
182  
183 9 ($result{'LATN'},$result{'LNGW'}) = &PixtoCoordinate( $value, $result{'PYN'}, $result{'PXW'} ) ;
184  
185 9 $result{'PYS'} = $result{'PYN'} + 255 ;
186 9 $result{'PXE'} = $result{'PXW'} + 255 ;
187  
188 # Convert Pixels to Coordinates (Lower Right Corner)...
189  
190 9 ($result{'LATS'},$result{'LNGE'}) = &PixtoCoordinate( $value, $result{'PYS'}, $result{'PXE'} ) ;
191  
192 9 return %result ;
193 }
194  
195 # Calculate a tile name from a pixel location and zoom...
196  
197 sub Google_Pix_to_Tile
198 0 ▶{
Subroutine Google_Pix_to_Tile: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my $value = shift ;
200 0 my $ty = shift ;
201 0 my $tx = shift ;
202  
203 # Convert Pixels to Tile Name...
204  
205 0 ($ty,$tx) = &PixtoTileName( $value, $ty, $tx, 'N', 'W', 'Partial' ) ;
206  
207 0 return ( $ty,$tx ) ;
208 }
209  
210 # Translate a coordinate to a pixel location...
211  
212 sub Google_Coord_to_Pix
213 47668 ▶{
Subroutine Google_Coord_to_Pix: called CC=3 SCAR=11.4
Pod undocumented
214 47668 my $value = shift ;
215 47668 my $lat = shift ;
216 47668 my $lng = shift ;
217 47668 my @d = ( ) ;
218 47668 my $e = 0 ;
219  
220 47668 $d[1] = sprintf("%0.0f", $$value{'bmO'} + $lng * $$value{'pixLngDeg'} ) ;
221  
222 47668 $e = sin($lat * $$value{'Wa'}) ;
223  
224 47668 ▶ if( $e > 0.99999 )
Branch: 1 branch
BranchTrueFalseTotal
if ($e > "0.99999") 0 47668 47668
225 {
226 0 $e = 0.99999 ;
227 }
228  
229 47668 ▶ if( $e < -0.99999 )
Branch: 1 branch
BranchTrueFalseTotal
if ($e < "-0.99999") 0 47668 47668
230 {
231 0 $e = -0.99999 ;
232 }
233  
234 47668 $d[0] = sprintf("%0.0f", $$value{'bmO'} + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $$value{'pixLngRad'} ) ;
235  
236 47668 return (@d) ;
237 }
238  
239 # Translate a pixel location to a tile name...
240  
241 sub PixtoTileName
242 0 ▶{
Subroutine PixtoTileName: not called CC=8 SCAR=42.8
Pod undocumented
243 0 my $value = shift ;
244 0 my $y = shift ;
245 0 my $x = shift ;
246 0 my $yd = shift ; # Y Direction: N or S
247 0 my $xd = shift ; # X Direction: W or E
248 0 my $parwho = shift ; # Partial / Whole
249 0 my $yn = 0 ; # Y Name
250 0 my $xn = 0 ; # X Name
251  
252 0 $yn = int( $y / $$value{'tileSize'} ) ; # Round Down
253 0 $xn = int( $x / $$value{'tileSize'} ) ; # Round Down
254  
255 0 ▶ if ( $parwho ne 'Partial' )
Branch: 1 branch
BranchTrueFalseTotal
if ($parwho ne "Partial") 0 0 0
256 {
257 0 ▶ if ( $yd eq 'N' )
Branch: 1 branch
BranchTrueFalseTotal
if ($yd eq 'N') { } 0 0 0
258 {
259 0 $yn++ ;
260 } else
261 {
262 0 $yn-- ;
263 }
264 0 ▶ if ( $xd eq 'W' )
Branch: 1 branch
BranchTrueFalseTotal
if ($xd eq 'W') { } 0 0 0
265 {
266 0 $xn++ ;
267 } else
268 {
269 0 $xn-- ;
270 }
271 }
272  
273 # Make sure tile numbers are sane...
274  
275 0 ▶ if ( $yn > $$value{'max'} )
Branch: 2 branches
BranchTrueFalseTotal
if ($yn > $value->{'max'}) { } 0 0 0
elsif ($yn < 0) { } 0 0 0
276 {
277 0 $yn = $$value{'max'} ;
278 } elsif ( $yn < 0 )
279 {
280 0 $yn = 0 ;
281 }
282  
283 0 ▶ if ( $xn > $$value{'max'} )
Branch: 2 branches
BranchTrueFalseTotal
if ($xn > $value->{'max'}) { } 0 0 0
elsif ($xn < 0) { } 0 0 0
284 {
285 0 $xn = $$value{'max'} ;
286 } elsif ( $xn < 0 )
287 {
288 0 $xn = 0 ;
289 }
290  
291 0 return( $yn, $xn ) ;
292 }
293  
294 # Translate a tile name to a pixel location...
295  
296 sub Google_Tile_to_Pix
297 9 ▶{
Subroutine Google_Tile_to_Pix: called CC=1 SCAR=0.0
Pod undocumented
298 9 my $value = shift ;
299 9 my $y = shift ;
300 9 my $x = shift ;
301 9 return( (sprintf("%0.0f", $y * $$value{'tileSize'} ), sprintf("%0.0f", $x * $$value{'tileSize'} )) ) ;
302 }
303  
304 # Translate a pixel location to a coordinate...
305  
306 sub PixtoCoordinate
307 18 ▶{
Subroutine PixtoCoordinate: called CC=1 SCAR=0.0
Pod undocumented
308 18 my $value = shift ;
309 18 my $y = shift ;
310 18 my $x = shift ;
311 18 my @d = ( ) ;
312 18 my $e = (($y - $$value{'bmO'}) / $$value{'pixLngRad'}) * (-1) ;
313  
314 18 $d[1] = sprintf("%0.6f",($x - $$value{'bmO'}) / $$value{'pixLngDeg'}) ;
315  
316 18 $d[0] = sprintf("%0.6f", (2 * atan(exp($e)) - $$value{'PI'} / 2) / $$value{'Wa'}) ;
317 18 return (@d);
318 }
319  
320 2 1;
321  
322 __END__
↑ Summary lib/Geo/Heatmap.pm »