I had a few problems with TileCache the other week which I am eager to blog about, since I knew for sure that later on, I might encounter the same. I don’t have the exact errors with me right now, so I’m jotting this down from my head…

  1. Classic Resolutions problem. Use extent_type=loose
  2. Can not set image type

UPDATED (JAN 11, 2010): Classic Resolutions problem:

How are resolutions calculated? Assuming we have:

Original:
Lower Left (LL) or minx, miny: 12453557, -5434940
Upper Right (UR) or max, maxy: 16980842, -1180729

maxResolution = (max – minx)/tilesize = (16980842 – 12453557)/512 = 8842.353
where tilesize = 512.

Therefore, we can set/guess for max so that we have maxResolution as a whole number.

Adjusted:
minx, miny: 12453557, -5434940
maxx, maxy: 16980661, -1180729

gives a maxResolution (whole number) of 8842.

Now, you can use 8842 in both the TileCache.cfg and OpenLayers Javascript.

A. Google BaseLayer + Road WMS Overlay = Validation approach before we proceed to TC.

  1. OpenLayers (OL) Code: http://www.gisnotes.com/platform/docs/examples/GoogleWMSRoads.cfm

  2. Map File Configuration: /home/map/beijing/new/beijing_google_roads

Well the above files are not exactly for TC, but it is a good demonstration that we could overlay our custom layers (WMS) on top of Google as the base layer. For a more detailed explanation regarding Google + OL, please read my previous gisnote. Also, this is a good test to see if our Mapserver mapfile configuration is correct. I do suggest not to proceed with OL + TC not unless you have validated the mapfile through OL (Google + WMS).

B. Google900913 Tilecache as baselayer

Ok.. so here it is..

  1. OpenLayers Code: http://www.gisnotes.com/platform/docs/examples/TilecacheGoogle900913Base.cfm
        window.onload = function() {
            var options = {
                        projection: new OpenLayers.Projection("EPSG:900913"),
                        units: "m",
 
                        //maxResolution: 156543.0339,
                        resolutions : [156543.03390000001, 78271.516950000005, 39135.758475000002, 19567.879237500001, 9783.9396187500006, 4891.9698093750003, 2445.9849046875001, 1222.9924523437501, 611.49622617187504, 305.74811308593752, 152.87405654296876, 76.43702827148438, 38.21851413574219, 19.109257067871095, 9.5546285339355475, 4.7773142669677737, 2.3886571334838869, 1.1943285667419434, 0.59716428337097172, 0.29858214168548586],
                        //zoom: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] @ numZoomLevels: 20
                        numZoomLevels: 20,
                        //maxExtent: new OpenLayers.Bounds(12823075.86334, 4800551.12375, 13101918.14248, 5021301.26141)
                        //maxExtent: new OpenLayers.Bounds(12823075, 4800551, 13101918, 5021301)
                        //maxExtent: new OpenLayers.Bounds(12848138, 4785083, 13080212, 5021118.5)
                        maxExtent: new OpenLayers.Bounds(-20037508, -20037508,20037508, 20037508.34)
                    };
 
            OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
            OpenLayers.Util.onImageLoadErrorColor = "transparent";
 
            map = new OpenLayers.Map('mapdiv',options);
 
            var serverURL = "/tilecache/tilecache.py";
            // start custom layer here
            var layer_obj = new OpenLayers.Layer.WMS(
                "Beijing",
                serverURL,
                {
                    layers: 'beijing_900913',
                    map: '/home/map/beijing/new/beijing_900913/beijing.map',
                    format: 'image/jpg',
                    'transparent': 'off'
                }
            );
            layer_obj.setIsBaseLayer(true);
            layer_obj.setVisibility(true);
            map.addLayer(layer_obj);
  1. TileCache Configuration: /wwwroot/tilecache/tilecache.cfg
[beijing_900913]
type=WMSLayer
url=http://127.0.0.1/cgi-bin/mapserv?map=/home/map/beijing/new/beijing_900913/beijing.map
layers=beijing_900913
metatile=true
extension=jpg
levels=20
resolutions=156543.03390000001, 78271.516950000005, 39135.758475000002, 19567.879237500001, 9783.9396187500006, 4891.9698093750003, 2445.9849046875001, 1222.9924523437501, 611.49622617187504, 305.74811308593752, 152.87405654296876, 76.43702827148438, 38.21851413574219, 19.109257067871095, 9.5546285339355475, 4.7773142669677737, 2.3886571334838869, 1.1943285667419434, 0.59716428337097172, 0.29858214168548586
srs=EPSG:900913
bbox=-20037508, -20037508, 20037508, 20037508.34
#maxResolution=156543.0339
extent_type=loose
debug=on

You can either set resolutions or maxResolutions. I always wanted to be explicit, so setting the resolutions is my preference.

  1. MapFile Configuration: /home/map/beijing/new/beijing_900913/beijing.map
    I have a long mapfile, and using includes helped a lot, see my previous gisnote. For debugging your mapfile, read this post.

Always Remember…I believe when they made TC, I presume they always tested on WGS84(lonlat), this means if you don’t set anything explicit, it would implicitly set settings in WGS84(lon/lat)…

  • Ensure that you have the vector data in the necessary projection, in my case it’s 900913.

  • Ensure that the resolutions from OL (B.1) is reflected in TC configuration (B.2).

  • Ensure that the maxextents from OL (B.1) is reflected in TC configuration (B.2) as bbox.

</ul> C. Still can’t get it running?

  1. Narrow down the error.

  2. tail -f /var/log/apache2/error.log and access.log is your bestfriend. It’s a good starting point to trace down what went wrong. If you can’t see it from Apache’s error.log then ensure that python debugging is turned on.

  3. Sometimes TC [a] gives you an obvious error or [b] its just a link to the mapserv request.

Examples of [a]:

  • *Classic Resolution problem stated above. *- If you followed my final notes above, then this should not happen. If you’ve set up things correctly but it’s still broken and you want to pull your hair out, then do a clean test stated below in D.
  • Cannot set image type – Check the extensions from the mapfile and TC config. Are they both in jpg’s?

The latter [b] is harder to debug. Most likely its a mapserver mapfile mishap and the easiest way to hunt this down is only thru your error.log (note: you need mapserver to have a debug=true when you compiled it, that’s another story.)

D. Doing a clean test as always

  1. Always disable any caching when testing. Disable cache from your browser, for FF I use the webdeveloper toolbar. It’s as easy as Disable -> Disable Cache.

  2. Stop/Start Apache. Hoping Python get’s a clean start as well. If you found any pyc (python compiled) under TC source directory… then stop Apache; remove the pycs; start Apache.

  3. Seeing the same images? Always remove the image caches! rm -Rf /data/tilecache/beijing_900913