Map Tiles (3) – Tile Math

August 6th, 2007

Before using our Tile Map we created in the last post, a quick discussion on “tile math” might be helpful. More specifically, we need be able to easily switch between tile coordinates and real-world coordinates to create a complete tile mapping program.

We will use this map tile as an example:

Map Tile Sample

Also included in the image is the tile coordinates of it’s four corners (remember that we are naming map tiles based on the lower-left corner so this tile is the 0,0 tile).

The tile also has real-world (geographic) coordinates. We are keeping this simple by using longitude (X) and latitude (Y) so the corner locations look like this:

Tile Map Sample

Our tile mapping program will use tile coordinates throughout the code but numerous functions will require geographic coordinates. So to convert tile coordinates to geographic coordinates, we need two more related pieces of information – the geographic width and height of each tile. In the above example, the geographic width (“tile distance X”) is 180 degrees and the height (“tile distance Y”) is 90 degrees.

Tile Math Example

Let’s envision a scenario where our tile mapping program is centered on the tile coordinate 0.5,0.5 – basically the center of our sample tile. We want to show the user the location of the map center but telling them 0.5, 0.5 is useless. So to display longitude/latitude, we need to perform separate calculations. First, longitude:

 

Longitude = [Tile Coordinate X] x [Tile Distance X]
Longitude = 0.5 * 180 = 90

 

And latitude:

 

Latitude = [Tile Coordinate Y] x [Tile Distance Y]
Latitude = 0.5 * 90 = 45

 

So converting tile coordinate 0.5,0.5 to geographic coordinates is 90,45.

Special Case – Tile Origin doesn’t match Geographic Origin

The above equations work for TileSets that have their origin at the geographic origin. In some TileSets, this may not be the case. For example, remember this tile from the last post:

TileSet 0

This single tile of the entire earth has it’s origin at the lower left corner which is really -180, -90 in terms of longitude and latitude.

LatLongMap

When we convert tile coordinates here to geographic coordinates, we must include the tile origin in the equation.

Again, assuming our map is centered on tile coordinate 0.5, 0.5, the geographic center is calculated below. Note that with this tile, the tile distance X is 360 degrees and the tile distance Y is 180 degrees.

 

Longitude = (0.5 * 360) + -180 = 0

 

 

Latitude = (0.5 * 180) + -90 = 0

 

So converting tile coordinate 0.5, 0.5 to geographic coordinates is 0,0.

The next post will begin constructing our XNA-based tile mapping program so stay tuned.

Continue to part 4