[Tilecache] Calculating tile bounding boxes.

Dave Weaver zen13097 at zen.co.uk
Thu Jun 14 08:41:38 EDT 2007

I have Tilecache working nicely, sat between an OpenLayers front-end
and a MapServer back-end.

However, I need to replace the OpenLayers part with some code of my
own using WMS requests, and I'm struggling to get the tile bounding
box calculations work correctly.

First of all, here's are the Tilecache & OpenLayers configs I'm using
for my testing:


       function init(){
            map = new OpenLayers.Map( $('map'), {
                        maxResolution: 156543.0339,
                        projection: 'EPSG:54004',
                        units: 'm',
                        maxExtent: new
            layer = new OpenLayers.Layer.WMS( "green",
                    "/tilecache/tilecache.cgi?", {
                        layers: 'test',
                        format: 'image/png'
             } );

So, for the outermost zoom, where the whole earth fits in one 256x256
tile, my brain tells  the bounding box is obviously
(-20037508.34,-20037508.34,20037508.34,20037508.34), however
OpenLayers and TileCache work it out as (-20037508.34, -20037508.34,
20037508.3384, 20037508.3384)

My calculations reckon that at the next zoom level, the bounding box
for the lower left quadrant should be (-20037508.34,-20037508.34,0,0),
but when I try this in the URL:

I get this error from TileCache:

  An error occurred: couldn't calculate tile index for layer test from
([-20037508.34, -20037508.34, 0.0, 0.0])

If I inspect the requests generated by OpenLayers, I see that it is
requesting the bounding box (-20037508.34, -20037508.34, -0.0008,
-0.0008) for the same tile, which works.

How do both OpenLayers and TileCache arrive at these values? Where am
I going wrong?

Many Thanks,

More information about the Tilecache mailing list