12,554,623 members (60,245 online)
alternative version

#### Stats

619.1K views
202 bookmarked
Posted

, 7 Jan 2008 CPOL
 Rate this:
An analysis of the encoding of the tiles used by Google map

## Introduction

This is my analysis about how Google map works, and specially how the tiles are encoded. Google map uses pre-rendered tiles that can be obtained with a simple URL. This article explains how to build the URL for a tile from its geo coordinates (latitude/longitude).

## Map Tile Encoding

Google map uses two different algorithms to encode the location of the tiles.

For Google map, the URL of a tile looks like http://mt1.google.com/mt?n=404&v=w2.12&x=130&y=93&zoom=9 using x and y for the tile coordinates, and a zoom factor. The zoom factor goes from `17` (fully zoomed out) to `0` (maximum definition). At a factor `17`, the whole earth is in one tile where x=0 and y=0. At a factor `16`, the earth is divided in 2x2 parts, where 0<=x<=1 and 0<=y<=1, and at each zoom step, each tile is divided into 4 parts. So at a zoom factor Z, the number of horizontal and vertical tiles is 2^(17-z)

### Algorithm to Find a Tile from a Latitude, a Longitude and a Zoom Factor

```//correct the latitude to go from 0 (north) to 180 (south),
// instead of 90(north) to -90(south)
latitude=90-latitude;

//correct the longitude to go from 0 to 360
longitude=180+longitude;

//find tile size from zoom level
double latTileSize=180/(pow(2,(17-zoom)));
double longTileSize=360/(pow(2,(17-zoom)));

//find the tile coordinates
int tilex=(int)(longitude/longTileSize);
int tiley=(int)(latitude/latTileSize);```

In fact this algorithm is theoretical as the covered zone doesn't match the whole globe.

### Servers

Google uses four servers to balance the load. These are mt0, mt1, mt2 and mt3.

### Tile Size

Each tile is a 256x256 PNG picture.

## For Satellite Images, the Encoding is a Bit Different

The URL looks like http://kh0.google.com/kh?n=404&v=8&t=trtqtt where the 't' parameters encode the image location. The length of the parameter indicates a zoom level.

To see the whole globe, just use 't=t'. This gives a single tile representing the earth. For the next zoom level, this tile is divided into 4 quadrants, called, clockwise from top left : 'q' 'r' 's' and 't'. To see a quadrant, just append the letter of that quadrant to the image you are viewing. For example :'t=tq' will give the upper left quadrant of the 't' image. And so on at each zoom level...

### Algorithm to Find a Tile from a Latitude, a Longitude and a Zoom Factor

```//initialise the variables;
double xmin=-180;
double xmax=180;
double ymin=-90;
double ymax=90;

double xmid=0;
double ymid=0;

string location="t";

//Google uses a latitude divided by 2;
double halflat = latitude / 2;

for (int i = 0; i < zoom; i++)
{
xmoy = (xmax + xmin) / 2;
ymoy = (ymax + ymin) / 2;
if (halflat > ymoy) //upper part (q or r)
{
ymin = ymoy;
if (longitude < xmoy)
{ /*q*/
location+= "q";
xmax = xmoy;
}
else
{/*r*/
location+= "r";
xmin = xmoy;
}
}
else //lower part (t or s)
{
ymax = ymoy;
if (longitude < xmoy)
{ /*t*/

location+= "t";
xmax = xmoy;
}
else
{/*s*/
location+= "s";
xmin = xmoy;
}
}
}
//here, the location should contain the string corresponding to the tile...```

Again, this algorithm is quite theoretical, as the covered zone doesn't match the full globe.

### Servers

Google uses four servers to balance the load. These are kh0, kh1, kh2 and kh3.

### Tile Size

Each tile is a 256x256 JPG picture.

## Mercator Projection

Due to the Mercator projection, the above algorithm has to be modified. In Mercator projection, the spacing between two parallels is not constant. So the angle described by a tile depends on its vertical position.

Here comes a piece of code to compute a tile's vertical number from its latitude.

```/**<summary>Get the vertical tile number from a latitude
using Mercator projection formula</summary>*/
private int getMercatorLatitude(double lati)
{
double maxlat = Math.PI;

double lat = lati;

if (lat > 90) lat = lat - 180;
if (lat < -90) lat = lat + 180;

double phi = Math.PI * lat / 180;

double res;
//double temp = Math.Tan(Math.PI / 4 - phi / 2);
//res = Math.Log(temp);
res = 0.5 * Math.Log((1 + Math.Sin(phi)) / (1 - Math.Sin(phi)));
double maxTileY = Math.Pow(2, zoom);
int result = (int)(((1 - res / maxlat) / 2) * (maxTileY));

return (result);
}```

## Covered Zone

Theoretically, latitude should go from `-90 `to `90`, but in fact due to the Mercator projection which sends the poles to the infinites, the covered zone is a bit less than `-90 `to `90`. In fact the maximum latitude is the one that gives `PI` (3.1415926) on the Mercator projection, using the formula `Y = 1/2((1+sin(lat))/(1-sin(lat)))` (see the link in the Mercator paragraph).

## Protection

Google map uses a protection mechanism to keep a good quality of service. If one makes too many requests, Google map will add its IP address to a blacklist, and send a nice message:

We're sorry... ... but your query looks similar to automated requests from a computer virus or spyware application. To protect our users, we can't process your request right now. We'll restore your access as quickly as possible, so try again soon. In the meantime, if you suspect that your computer or network has been infected, you might want to run a virus checker or spyware remover to make sure that your systems are free of viruses and other spurious software. We apologize for the inconvenience, and hope we'll see you again on Google.

To avoid being blacklisted, developers should use a caching mechanism if possible...

## Sat Examples

See the whole globe at http://kh0.google.com/kh?n=404&v=8&t=t.

And the four corresponding quadrants: (note the 4 servers name to balance the load)

## Map Examples

See the whole globe at http://mt1.google.com/mt?n=404&v=&x=0&y=0&zoom=17.

Nice, isn't it?

## History

• Article edited: Google map has changed the v parameter for the maps. It was 2.12 when I wrote this article, but it's now 2.66. I suppose this is a version number or something like that...

## Share

 Web Developer France
No Biography provided

## You may also be interested in...

 Pro Pro

 Re: Server numbering and secure word (Galileo) JasonDiplomat27-Aug-08 13:18 JasonDiplomat 27-Aug-08 13:18
 Re: Server numbering and secure word (Galileo) CoderCZ28-Aug-08 0:42 CoderCZ 28-Aug-08 0:42
 Re: Server numbering and secure word (Galileo) segu@gmx.net27-Aug-08 18:42 segu@gmx.net 27-Aug-08 18:42
 Re: Server numbering and secure word (Galileo) CoderCZ28-Aug-08 0:47 CoderCZ 28-Aug-08 0:47
 Re: Server numbering and secure word (Galileo) segu@gmx.net28-Aug-08 1:01 segu@gmx.net 28-Aug-08 1:01
 Getting a tile Member 15975873-Jun-08 1:18 Member 1597587 3-Jun-08 1:18
 Licence problems Aleksey Zaharov6-Apr-08 5:11 Aleksey Zaharov 6-Apr-08 5:11
 Re: Licence problems Pascal Buirey6-Apr-08 22:12 Pascal Buirey 6-Apr-08 22:12
 Re: Licence problems Daniel Ch. Bloch (MCSD, MCAD, MCTS)19-Nov-08 19:27 Daniel Ch. Bloch (MCSD, MCAD, MCTS) 19-Nov-08 19:27
 Summary Article [modified] James_Lin28-Feb-08 13:53 James_Lin 28-Feb-08 13:53
 Is this project dead? James_Lin20-Feb-08 15:30 James_Lin 20-Feb-08 15:30
 Re: Is this project dead? Pascal Buirey20-Feb-08 20:47 Pascal Buirey 20-Feb-08 20:47
 Re: Is this project dead? James_Lin23-Feb-08 19:07 James_Lin 23-Feb-08 19:07
 Re: Is this project dead? Pascal Buirey24-Feb-08 21:52 Pascal Buirey 24-Feb-08 21:52
 Re: Is this project dead? TimmyTee26-Feb-08 2:51 TimmyTee 26-Feb-08 2:51
 Re: Is this project dead? James_Lin28-Feb-08 9:39 James_Lin 28-Feb-08 9:39
 Re: Is this project dead? senthilmuruganbtech12-Aug-08 23:24 senthilmuruganbtech 12-Aug-08 23:24
 Re: Is this project dead? senthilmuruganbtech12-Aug-08 23:23 senthilmuruganbtech 12-Aug-08 23:23
 Re: Is this project dead? senthilmuruganbtech12-Aug-08 23:24 senthilmuruganbtech 12-Aug-08 23:24
 Re: Is this project dead? senthilmuruganbtech13-Aug-08 2:11 senthilmuruganbtech 13-Aug-08 2:11
 hi. how to use real-time tiles...? hyohaeng31-Jan-08 19:05 hyohaeng 31-Jan-08 19:05
 Re: hi. how to use real-time tiles...? Pascal Buirey31-Jan-08 22:21 Pascal Buirey 31-Jan-08 22:21
 get x,y pixel inside a tile from given GPS coordinates James_Lin31-Jan-08 10:30 James_Lin 31-Jan-08 10:30
 Re: get x,y pixel inside a tile from given GPS coordinates Pascal Buirey31-Jan-08 21:54 Pascal Buirey 31-Jan-08 21:54
 Re: get x,y pixel inside a tile from given GPS coordinates James_Lin3-Feb-08 9:33 James_Lin 3-Feb-08 9:33
 Re: get x,y pixel inside a tile from given GPS coordinates Pascal Buirey4-Feb-08 6:07 Pascal Buirey 4-Feb-08 6:07
 Re: get x,y pixel inside a tile from given GPS coordinates [modified] James_Lin4-Feb-08 11:53 James_Lin 4-Feb-08 11:53
 Re: get x,y pixel inside a tile from given GPS coordinates James_Lin10-Feb-08 9:13 James_Lin 10-Feb-08 9:13
 Re: get x,y pixel inside a tile from given GPS coordinates TimmyTee13-Feb-08 1:19 TimmyTee 13-Feb-08 1:19
 Re: get x,y pixel inside a tile from given GPS coordinates James_Lin13-Feb-08 11:32 James_Lin 13-Feb-08 11:32
 c# getTileXY [modified] James_Lin30-Jan-08 15:50 James_Lin 30-Jan-08 15:50
 ripping road maps programmatically, failed.... James_Lin30-Jan-08 12:57 James_Lin 30-Jan-08 12:57
 Re: ripping road maps programmatically, failed.... Pascal Buirey30-Jan-08 21:47 Pascal Buirey 30-Jan-08 21:47
 Re: ripping road maps programmatically, failed.... James_Lin30-Jan-08 22:31 James_Lin 30-Jan-08 22:31
 Re: ripping road maps programmatically, failed.... [modified] CoderCZ24-Jun-08 0:05 CoderCZ 24-Jun-08 0:05
 problem getting tilex and tiley James_Lin28-Jan-08 22:50 James_Lin 28-Jan-08 22:50
 Re: problem getting tilex and tiley Pascal Buirey29-Jan-08 2:04 Pascal Buirey 29-Jan-08 2:04
 Re: problem getting tilex and tiley [modified] James_Lin29-Jan-08 11:23 James_Lin 29-Jan-08 11:23
 Re: problem getting tilex and tiley Pascal Buirey29-Jan-08 21:46 Pascal Buirey 29-Jan-08 21:46
 Re: problem getting tilex and tiley James_Lin29-Jan-08 23:41 James_Lin 29-Jan-08 23:41
 how to get surround tiles for the road map? James_Lin24-Jan-08 11:57 James_Lin 24-Jan-08 11:57
 Re: how to get surround tiles for the road map? Pascal Buirey24-Jan-08 21:00 Pascal Buirey 24-Jan-08 21:00
 Re: how to get surround tiles for the road map? James_Lin29-Jan-08 23:42 James_Lin 29-Jan-08 23:42
 getMercatorLatitude Zen29-Dec-07 13:39 Zen 29-Dec-07 13:39
 how to get the tile teight in degrees for a given level? SmileT430-Oct-07 2:30 SmileT4 30-Oct-07 2:30
 Re: how to get the tile teight in degrees for a given level? Pascal Buirey30-Oct-07 2:45 Pascal Buirey 30-Oct-07 2:45
 Re: how to get the tile teight in degrees for a given level? SmileT430-Oct-07 4:42 SmileT4 30-Oct-07 4:42
 Re: how to get the tile teight in degrees for a given level? Pascal Buirey30-Oct-07 5:06 Pascal Buirey 30-Oct-07 5:06
 Re: how to get the tile teight in degrees for a given level? SmileT430-Oct-07 5:24 SmileT4 30-Oct-07 5:24
 How to calculation start longitude, latitude of each jpg image Charlie Shin23-Oct-07 5:57 Charlie Shin 23-Oct-07 5:57
 Last Visit: 31-Dec-99 18:00     Last Update: 25-Oct-16 10:48 Refresh 12345 Next »