Convert lat, lng and zoom values to pixel (x,y) on a map

March 6th, 2009 by Carlos Barros

Working on a project this week, that involves Google Maps, I needed to, given a latitude, a longitude and a zoom level, calculate the pixel position (x,y) within the map. If you need to do this using javascript, it’s pretty easy, as GMap API provides a handy method to do this:

var pixel = map.getCurrentMapType().getProjection().fromLatLngToPixel(new GLatLng(10,20),map.getZoom());

Unfortunately, my need was to convert lat, lng and zoom into pixel position from PHP. I “googled” about this subject for a while, and it’s incredible how frequent this need is, and how this information lacks. I saw a lot of people asking for similar things, and also some people that claim that got it working, but without providing any code for this. Googling a bit more I figured out that google uses one thing called Mercator Projection, but even this being a standard, I didn’t find anything that does what I needed. After a lot of time searching about this, I finally stumbled upon a perl script with a set of methods that accomplishes the task. Actually, this perl script is more focused on working with map “tiles”, but there are two methods that does exactly what I needed. It was just a matter of converting these perl methods into php and we’re done.

Here’s the “magic” perl script:

# Calculate tile characteristics given a bounding box of coordinates and a zoom...
# Author. John D. Coryat 01/2008...
# USNaviguide LLC.
# Published under Apache 2.0 license.
# Adapted from: Google Maps API Javascript...
##
# In order to correctly locate objects of interest on a Custom Map Overlay Google Maps, 
# the characteristics of each tile to build are required.
##
# Google_Tiles					# Calculate all tiles for a bounding box and zoom
# Google_Tile_Factors				# Calculate the factors needed ( Zoom, Tilesize )
# Google_Tile_Calc				# Calculate a single tile features from a tile name and zoom
# Google_Tile_to_Pix				# Calculate tile name to pixel
# Google_Coord_to_Pix				# Calculate coordinate to Pixel
# Google_Pix_to_Tile				# Calculate a tile name from a pixel location and zoom
 
require 5.003 ;
package USNaviguide_Google_Tiles ;
use strict ;
use Math::Trig ;
 
BEGIN {
 use Exporter ;
 use vars qw ( $VERSION @ISA @EXPORT) ;
 $VERSION	= 1.0 ;
 @ISA		= qw ( Exporter ) ;
 @EXPORT	= qw ( 
 Google_Tiles
 Google_Tile_Factors
 Google_Tile_Calc
 Google_Tile_to_Pix
 Google_Coord_to_Pix
 Google_Pix_to_Tile
 ) ;
}
 
#
# Call as: <array of Hashes> = &Google_Tiles(<LatitudeS>, <LongitudeW>, <LatitudeN>, <LongitudeE>, <Zoom>, [<option: tileSize>], [<option: Partial/Whole>]) ;
# Partial/Whole option: (Default: Partial)
#	Partial: Include the edge to create partial tiles
#       Whole: Include only tiles that are contained by the bounds
#
#          Returned Array Specifications:
#            Each element is a reference to a Hash:
#              NAMEY - Tile Name y
#              NAMEX - Tile Name x
#              PYS - Pixel South
#              PXW - Pixel West
#              PYN - Pixel North
#              PXE - Pixel East
#              LATS - South Latitude
#              LNGW - West Longitude
#              LATN - North Latitude
#              LNGE - East Longitude
#
#          Note: X is width, Y is height...
#
 
sub Google_Tiles
{
 my $latS	= shift ;
 my $lngW	= shift ;
 my $latN	= shift ;
 my $lngE	= shift ;
 my $zoom	= shift ;
 my $tileSize	= shift ;
 my $parwho	= shift ;
 my $ty		= 0 ;
 my $tx		= 0 ;
 my @ret	= ( ) ;
 my %first	= ( ) ;				# First Results Hash
 my %last	= ( ) ;				# Last Results Hash
 
 my $value	= &Google_Tile_Factors($zoom, $tileSize) ; # Calculate Tile Factors
 
 if (!defined($parwho) or !$parwho)
 {
  $parwho = 'Partial' ;
 }
 
 # NW: Convert Coordinates to Pixels...
 
 ($first{'NORTH'},$first{'WEST'}) = &Google_Coord_to_Pix( $value, $latN, $lngW ) ;
 
 # Convert Pixels to Tile Name...
 
 ($first{'NAMEY'},$first{'NAMEX'}) = &PixtoTileName( $value, $first{'NORTH'}, $first{'WEST'}, 'N', 'W', $parwho ) ;
 
 # SE: Convert Coordinates to Pixels...
 
 ($last{'SOUTH'},$last{'EAST'}) = &Google_Coord_to_Pix( $value, $latS, $lngE ) ;
 
 # Convert Pixels to Tile Name...
 
 ($last{'NAMEY'},$last{'NAMEX'}) = &PixtoTileName( $value, $last{'SOUTH'}, $last{'EAST'}, 'S', 'E', $parwho ) ;
 
 # Calculate tile values for all tiles...
 
 if ( $first{'NAMEX'} > $last{'NAMEX'} )			# Across the date line
 {
  for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
  {
   for ( $tx = $first{'NAMEX'} ; $tx <= $$value{'max'} ; $tx++ )
   {
    push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
   }
   for ( $tx = 0 ; $tx <= $last{'NAMEX'} ; $tx++ )
   {
    push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
   }
  }
 } else
 {
  for ( $ty = $first{'NAMEY'} ; $ty <= $last{'NAMEY'} ; $ty++ )
  {
   for ( $tx = $first{'NAMEX'} ; $tx <= $last{'NAMEX'} ; $tx++ )
   {
    push( @ret, {&Google_Tile_Calc( $value, $ty, $tx)} ) ;
   }
  }
 }
 
 $ret[0]{'NORTH'} = $first{'NORTH'} ;
 $ret[0]{'WEST'} = $first{'WEST'} ;
 
 $ret[$#ret]{'SOUTH'} = $last{'SOUTH'} ;
 $ret[$#ret]{'EAST'} = $last{'EAST'} ;
 
 return ( @ret ) ;
}
 
# Calculate Tile Factors...
 
sub Google_Tile_Factors
{
 my $zoom	= shift ;
 my $tileSize	= shift ;
 my %value	= ( ) ;
 
 # Validate and correct input parameters...
 
 if ( !defined($zoom) or $zoom < 0 )
 {
  $zoom	= 0 ;
 }
 
 if ( !defined($tileSize) or !$tileSize )
 {
  $tileSize	= 256 ;
 }
 
 # Calculate Values...
 
 $value{'zoom'}	= $zoom ;
 $value{'PI'}	= 3.1415926536 ;
 $value{'bc'}	= 2 * $value{'PI'} ;
 $value{'Wa'}	= $value{'PI'} / 180 ;
 $value{'cp'}	= 2 ** ($value{'zoom'} + 8) ;
 $value{'max'}	= (2 ** $value{'zoom'}) - 1 ;		# Maximum Tile Number
 $value{'pixLngDeg'}= $value{'cp'} / 360;
 $value{'pixLngRad'}= $value{'cp'} / $value{'bc'} ;
 $value{'bmO'}	= $value{'cp'} / 2 ;
 $value{'tileSize'} = $tileSize ;
 
 return \%value ;
}
 
# Calculate tile values from Name...
 
sub Google_Tile_Calc
{
 my %result	= ( ) ;
 my $value	= shift ;
 $result{'NAMEY'} = shift ;
 $result{'NAMEX'} = shift ;
 
 # Convert Tile Name to Pixels...
 
 ($result{'PYN'},$result{'PXW'}) = &Google_Tile_to_Pix( $value, $result{'NAMEY'}, $result{'NAMEX'} ) ;
 
 # Convert Pixels to Coordinates (Upper Left Corner)...
 
 ($result{'LATN'},$result{'LNGW'}) = &PixtoCoordinate( $value, $result{'PYN'}, $result{'PXW'} ) ;
 
 $result{'PYS'} = $result{'PYN'} + 255 ;
 $result{'PXE'} = $result{'PXW'} + 255 ;
 
 # Convert Pixels to Coordinates (Lower Right Corner)...
 
 ($result{'LATS'},$result{'LNGE'}) = &PixtoCoordinate( $value, $result{'PYS'}, $result{'PXE'} ) ;
 
 return %result ;
}
 
# Calculate a tile name from a pixel location and zoom...
 
sub Google_Pix_to_Tile
{
 my $value	= shift ;
 my $ty		= shift ;
 my $tx		= shift ;
 
 # Convert Pixels to Tile Name...
 
 ($ty,$tx) = &PixtoTileName( $value, $ty, $tx, 'N', 'W', 'Partial' ) ;
 
 return ( $ty,$tx ) ;
}
 
# Translate a coordinate to a pixel location...
 
sub Google_Coord_to_Pix
{
 my $value	= shift ;
 my $lat	= shift ;
 my $lng	= shift ;
 my @d		= ( ) ; 
 my $e		= 0 ;
 
 $d[1] = sprintf("%0.0f", $$value{'bmO'} + $lng * $$value{'pixLngDeg'} ) ;
 
 $e = sin($lat * $$value{'Wa'}) ;
 
 if( $e > 0.99999 )
 {
  $e = 0.99999 ;
 }
 
 if( $e < -0.99999 )
 {
  $e = -0.99999 ;
 }
 
 $d[0] = sprintf("%0.0f", $$value{'bmO'} + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $$value{'pixLngRad'} ) ;
 
 return (@d) ;
}
 
# Translate a pixel location to a tile name...
 
sub PixtoTileName
{
 my $value	= shift ;
 my $y		= shift ;
 my $x		= shift ;
 my $yd		= shift ;				# Y Direction: N or S
 my $xd		= shift ;				# X Direction: W or E
 my $parwho	= shift ;				# Partial / Whole
 my $yn		= 0 ;					# Y Name
 my $xn		= 0 ;					# X Name
 
 $yn = int( $y / $$value{'tileSize'} ) ;		# Round Down
 $xn = int( $x / $$value{'tileSize'} ) ;		# Round Down
 
 if ( $parwho ne 'Partial' )
 {
  if ( $yd eq 'N' )
  {
   $yn++ ;
  } else
  {
   $yn-- ;
  }
  if ( $xd eq 'W' )
  {
   $xn++ ;
  } else
  {
   $xn-- ;
  }
 }
 
 # Make sure tile numbers are sane...
 
 if ( $yn > $$value{'max'} )
 {
  $yn = $$value{'max'} ;
 } elsif ( $yn < 0 )
 {
  $yn = 0 ;
 }
 
 if ( $xn > $$value{'max'} )
 {
  $xn = $$value{'max'} ;
 } elsif ( $xn < 0 )
 {
  $xn = 0 ;
 }
 
 return( $yn, $xn ) ; 
}
 
# Translate a tile name to a pixel location...
 
sub Google_Tile_to_Pix
{
 my $value	= shift ;
 my $y		= shift ;
 my $x		= shift ;
 return( (sprintf("%0.0f", $y * $$value{'tileSize'} ), sprintf("%0.0f", $x * $$value{'tileSize'} )) ) ; 
}
 
# Translate a pixel location to a coordinate...
 
sub PixtoCoordinate
{
 my $value	= shift ;
 my $y		= shift ;
 my $x		= shift ;
 my @d		= ( ) ;
 my $e		= (($y - $$value{'bmO'}) / $$value{'pixLngRad'}) * (-1) ;
 
 $d[1]	= sprintf("%0.6f",($x - $$value{'bmO'}) / $$value{'pixLngDeg'}) ;
 
 $d[0]	= sprintf("%0.6f", (2 * atan(exp($e)) - $$value{'PI'} / 2) / $$value{'Wa'}) ;
 return (@d);
}
 
1;
 
__END__
 
=head1 SYNOPSIS
 
#!/usr/bin/perl -w
# Test Program:
 
# Google Tile Calculator Test Program...
# Author. John D. Coryat 01/2008 USNaviguide LLC
 
use strict;
use USNaviguide_Google_Tiles ;
 
my $latS        = 34.177442 ;
my $lngW        = -91.318359 ;
my $latN        = 35.797300 ;
my $lngE        = -88.681641 ;
my $zoom        = 8 ;
my $x           = '' ;
my %tile        = ( ) ;
my $i           = 0 ;
 
my @d = &Google_Tiles($latS, $lngW, $latN, $lngE, $zoom,0,'Whole');
 
print "Total: " . scalar(@d) . "\n" ;
 
for ( $i = 0; $i <= $#d; $i++ )
{
 print "Tile # $i Y: $d[$i]{'NAMEY'} X: $d[$i]{'NAMEX'}\n" ;
 
 %tile = %{$d[$i]} ;
 
 foreach $x (sort keys %tile)
 {
  print "\t$x: $tile{$x}\n" ;
 }
}
 
=cut

Original link to this code is: http://www.usnaviguide.com/google-tiles.htm

The interesting methods in the script are:

  • Google_Tile_Factors: This method receives a zoom level and the tile size (256 in google maps) and return an array with some values that are used in next method.
  • Google_Coord_to_Pix: This method receives the array returned by the previous method, a latitude and a longitude, and will return an array with a (x,y) pair that correspond to the pixel position in the map. Note that it won’t return a pixel position relative to the current view, but relative to the entire map.

What about the PHP version?? Well, unfortunately my code is part of a project and I can’t post it here, but it’s pretty easy to convert these perl methods into PHP (I don’t have ANY experience with perl, and did it in a few minutes).

That’s it, I hope this helps you as it helped me.

Carlos

Share this post...
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Ma.gnolia
  • MySpace
  • Rec6
  • Reddit
  • StumbleUpon
  • Technorati

Comments are closed.