World Pixels: The Best Map Tiles Technique
Published at 17:52 on 3 December 2017
If you go to the Open Street Map Wiki, you can find pages like this one giving helpful information about how map tiles work, and how tiles map to latitude and longitude.
But you don’t see any mention of a technique I call world pixels, an idea that woke me from a dream last night and which implementing put an end to countless headaches in getting map bounds just right.
Consider the following facts:
- Zoom levels run from 0 through 18.
- Each zoom level, Z, has 2Z tiles in each direction.
- Each tile has 256 (28) pixels in each direction.
- 18 + 8 = 26.
- 26 ≤ 32.
Therefore, at all zoom levels, it is possible to code the (x, y) coordinates of all pixels in all tiles with two globally unique 32-bit integers. Put the tile pixel dimensions in the 8 least significant bits and the tile numbers in the next 18 most significant bits. Translate your lat/long coordinates to world pixels as early as possible in the process, and all your math then becomes integer math, which runs faster and is free from rounding errors.
Better yet, it makes zooming a breeze. Want to zoom in? Shift left; the most significant bit of the tile pixel number will become the least significant bit of the tile number. Likewise, want to zoom out? Shift right.
Simplicity itself; both easier to understand and it offers faster execution times and it produces more accurate results.