1 # Calculate tile characteristics given a bounding box of coordinates and a zoom...
2 # Author. John D. Coryat 01/2008...
4 # Published under Apache 2.0 license.
5 # Adapted from: Google Maps API Javascript...
7 # In order to correctly locate objects of interest on a Custom Map Overlay Google Maps,
8 # the characteristics of each tile to build are required.
10 # Google_Tiles # Calculate all tiles for a bounding box and zoom
11 # Google_Tile_Factors # Calculate the factors needed ( Zoom, Tilesize )
12 # Google_Tile_Calc # Calculate a single tile features from a tile name and zoom
13 # Google_Tile_to_Pix # Calculate tile name to pixel
14 # Google_Coord_to_Pix # Calculate coordinate to Pixel
15 # Google_Pix_to_Coord # Calculate Pixels to coordinates
16 # Google_Pix_to_Tile # Calculate a tile name from a pixel location and zoom
19 package USNaviguide_Google_Tiles ;
25 use vars qw ( $VERSION @ISA @EXPORT) ;
27 @ISA = qw ( Exporter ) ;
40 # Call as: <array of Hashes> = &Google_Tiles(<LatitudeS>, <LongitudeW>, <LatitudeN>, <LongitudeE>, <Zoom>, [<option: tileSize>], [<option: Partial/Whole>]) ;
41 # Partial/Whole option: (Default: Partial)
42 # Partial: Include the edge to create partial tiles
43 # Whole: Include only tiles that are contained by the bounds
45 # Returned Array Specifications:
46 # Each element is a reference to a Hash:
53 # LATS - South Latitude
54 # LNGW - West Longitude
55 # LATN - North Latitude
56 # LNGE - East Longitude
58 # Note: X is width, Y is height...
68 my $tileSize = shift ;
73 my %first = ( ) ; # First Results Hash
74 my %last = ( ) ; # Last Results Hash
76 my $value = &Google_Tile_Factors($zoom, $tileSize) ; # Calculate Tile Factors
78 if (!defined($parwho) or !$parwho)
83 # NW: Convert Coordinates to Pixels...
85 ($first{'NORTH'},$first{'WEST'}) = &Google_Coord_to_Pix( $value, $latN, $lngW ) ;
87 # Convert Pixels to Tile Name...
89 ($first{'NAMEY'},$first{'NAMEX'}) = &PixtoTileName( $value, $first{'NORTH'}, $first{'WEST'}, 'N', 'W', $parwho ) ;
91 # SE: Convert Coordinates to Pixels...
93 ($last{'SOUTH'},$last{'EAST'}) = &Google_Coord_to_Pix( $value, $latS, $lngE ) ;
95 # Convert Pixels to Tile Name...
97 ($last{'NAMEY'},$last{'NAMEX'}) = &PixtoTileName( $value, $last{'SOUTH'}, $last{'EAST'}, 'S', 'E', $parwho ) ;
99 # Calculate tile values for all tiles...
101 if ( $first{'NAMEX'} > $last{'NAMEX'} ) # Across the date line
103 for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
105 for ( $tx = $first{'NAMEX'} ; $tx <= $$value{'max'} ; $tx++ )
107 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
109 for ( $tx = 0 ; $tx <= $last{'NAMEX'} ; $tx++ )
111 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
116 for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
118 for ( $tx = $first{'NAMEX'} ; $tx <= $last{'NAMEX'} ; $tx++ )
120 push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
125 $ret[0]{'NORTH'} = $first{'NORTH'} ;
126 $ret[0]{'WEST'} = $first{'WEST'} ;
128 $ret[$#ret]{'SOUTH'} = $last{'SOUTH'} ;
129 $ret[$#ret]{'EAST'} = $last{'EAST'} ;
134 # Calculate Tile Factors...
136 sub Google_Tile_Factors
139 my $tileSize = shift ;
142 # Validate and correct input parameters...
144 if ( !defined($zoom) or $zoom < 0 )
149 if ( !defined($tileSize) or !$tileSize )
154 # Calculate Values...
156 $value{'zoom'} = $zoom ;
157 $value{'PI'} = 3.1415926536 ;
158 $value{'bc'} = 2 * $value{'PI'} ;
159 $value{'Wa'} = $value{'PI'} / 180 ;
160 $value{'cp'} = 2 ** ($value{'zoom'} + 8) ;
161 $value{'max'} = (2 ** $value{'zoom'}) - 1 ; # Maximum Tile Number
162 $value{'pixLngDeg'}= $value{'cp'} / 360;
163 $value{'pixLngRad'}= $value{'cp'} / $value{'bc'} ;
164 $value{'bmO'} = $value{'cp'} / 2 ;
165 $value{'tileSize'} = $tileSize ;
170 # Calculate tile values from Name...
176 $result{'NAMEY'} = shift ;
177 $result{'NAMEX'} = shift ;
179 # Convert Tile Name to Pixels...
181 ($result{'PYN'},$result{'PXW'}) = &Google_Tile_to_Pix( $value, $result{'NAMEY'}, $result{'NAMEX'} ) ;
183 # Convert Pixels to Coordinates (Upper Left Corner)...
185 ($result{'LATN'},$result{'LNGW'}) = &Google_Pix_to_Coord( $value, $result{'PYN'}, $result{'PXW'} ) ;
187 $result{'PYS'} = $result{'PYN'} + 255 ;
188 $result{'PXE'} = $result{'PXW'} + 255 ;
190 # Convert Pixels to Coordinates (Lower Right Corner)...
192 ($result{'LATS'},$result{'LNGE'}) = &Google_Pix_to_Coord( $value, $result{'PYS'}, $result{'PXE'} ) ;
197 # Translate a coordinate to a pixel location...
199 sub Google_Coord_to_Pix
225 $d[1] = sprintf("%0.0f", $$value{'bmO'} + $lng * $$value{'pixLngDeg'} ) ;
227 $e = sin($lat * $$value{'Wa'}) ;
239 $d[0] = sprintf("%0.0f", $$value{'bmO'} + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $$value{'pixLngRad'} ) ;
244 # Calculate a tile name from a pixel location and zoom: Externally called version...
246 sub Google_Pix_to_Tile
252 # Convert Pixels to Tile Name...
254 ($ty,$tx) = &PixtoTileName( $value, $ty, $tx, 'N', 'W', 'Partial' ) ;
259 # Translate a pixel location to a tile name: Internally called version...
266 my $yd = shift ; # Y Direction: N or S
267 my $xd = shift ; # X Direction: W or E
268 my $parwho = shift ; # Partial / Whole
269 my $yn = 0 ; # Y Name
270 my $xn = 0 ; # X Name
272 $yn = int( $y / $$value{'tileSize'} ) ; # Round Down
273 $xn = int( $x / $$value{'tileSize'} ) ; # Round Down
275 if ( $parwho ne 'Partial' )
293 # Make sure tile numbers are sane...
295 if ( $yn > $$value{'max'} )
297 $yn = $$value{'max'} ;
303 if ( $xn > $$value{'max'} )
305 $xn = $$value{'max'} ;
314 # Translate a tile name to a pixel location...
316 sub Google_Tile_to_Pix
321 return( (sprintf("%0.0f", $y * $$value{'tileSize'} ), sprintf("%0.0f", $x * $$value{'tileSize'} )) ) ;
324 # Translate a pixel location to a coordinate...
326 sub Google_Pix_to_Coord
332 my $e = (($y - $$value{'bmO'}) / $$value{'pixLngRad'}) * (-1) ;
334 $d[1] = sprintf("%0.6f",($x - $$value{'bmO'}) / $$value{'pixLngDeg'}) ;
336 $d[0] = sprintf("%0.6f", (2 * atan(exp($e)) - $$value{'PI'} / 2) / $$value{'Wa'}) ;
349 # Google Tile Calculator Test Program...
350 # Author. John D. Coryat 01/2008 USNaviguide LLC
353 use USNaviguide_Google_Tiles ;
355 my $latS = 34.177442 ;
356 my $lngW = -91.318359 ;
357 my $latN = 35.797300 ;
358 my $lngE = -88.681641 ;
364 my @d = &Google_Tiles($latS, $lngW, $latN, $lngE, $zoom,0,'Whole');
366 print "Total: " . scalar(@d) . "\n" ;
368 for ( $i = 0; $i <= $#d; $i++ )
370 print "Tile # $i Y: $d[$i]{'NAMEY'} X: $d[$i]{'NAMEX'}\n" ;
374 foreach $x (sort keys %tile)
376 print "\t$x: $tile{$x}\n" ;