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

Stephen Woodbridge woodbri at swoodbridge.com
Thu Feb 23 11:57:22 EST 2006


Paul,

That helped a lot. This is what I came up with (in perl)

     my $cellsize = $scale/(72.0*4374754.0);

     my $w = 200;   # viewport width
     my $h = 200;   # viewport height

     my $tw = 200;  # tile width
     my $th = 200;  # tile height

     my ($cy, $cx) = ($lat, $lon);

     my $xmin  = $cx - ($w/2)*$cellsize;
     my $ymin  = $cy - ($h/2)*$cellsize;
     my $xmax  = $cx + ($w/2)*$cellsize;
     my $ymax  = $cy + ($h/2)*$cellsize;

     my $pixw  = ($xmax-$xmin)/$w;
     my $pixh  = ($ymax-$ymin)/$h;

     my $pixl  = $xmin/$pixw;
     my $pixt  = -1*$ymax/$pixh;

     # clamp to tile
     my $top   = floor($pixt/$th + 1.5)*$th;
     my $left  = floor($pixl/$tw + 1.0)*$tw;

And it is returning results as expected. I needed this to generate 
random tile requests to do some performance and load testing on one of 
our Where2getit.com servers.

Thanks,
   -Steve

Paul Spencer wrote:
> 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/|
> +-----------------------------------------------------------------+
> 
> 
> 
> 
> 



More information about the ka-Map-users mailing list