# [ka-Map-users] lat/lon/scale to tile

Paul Spencer pspencer at dmsolutions.ca
Thu Feb 23 11:41:33 EST 2006

```lol ... I have a sign on my window that one of the guys wrote up
quoting me as saying:

"The thing I don't like about ka-map is that I don't fully understand
the math"

But seriously ... the fundamental assumption is that 0,0 in pixel
space is at 0,0 in geographic space regardless of projection.  This
really simplifies things (believe it or not).

Given a scale value, you can calculate the size of a pixel in
geographic units:

//units is MapServer's units MS_DD etc .. this array was taken from
mapserver source
//resolution is the resolution of the map file, default is 72
var inchesPerUnit = new Array(1, 12, 63360.0, 39.3701, 39370.1,
4374754);
var cellSize = scale/(resolution * inchesPerUnit[units]);

With a cell size and the basic 0,0 assumption, you can calculate the
pixel location of a geographic location as follows:

var px = gx / cellSize;
var py = gy / cellSize;

The best example of this in practice is kaMap.prototype.zoomTo

Tiles are of a fixed size.  0,0 is the top, left of one image, so all
image locations are in increments of tile size from 0,0.  Any given
tile is requested by its top/left pixel location when requesting it
from tile.php.  See _layer.prototype.setTile.

If Tile.php doesn't find the tile in the cache (path and file name
use pixel coordinates) then the process is reversed to determine the
geographic location of the requested tile.  It also then figures out
a larger (meta)tile that the requested tile is part of and generates
the metatile and slices it into tiles.

Does this help?

Cheers

Paul

On 23-Feb-06, at 11:00 AM, Stephen Woodbridge wrote:

> Hi all,
>
> Can someone point to the code that converts a lat,lon,scale into a
> tile.php request? I have tried to reverse engineer the math, but I
> now have a headache :)
>
> Thanks,
>   -Steve W.
> _______________________________________________
> ka-Map-users mailing list
> ka-Map-users at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/ka-map-users

+-----------------------------------------------------------------+
|Paul Spencer                           pspencer at dmsolutions.ca   |
+-----------------------------------------------------------------+
|Applications & Software Development                              |
|DM Solutions Group Inc                 http://www.dmsolutions.ca/|
+-----------------------------------------------------------------+

```