1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * licence. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Layer/Grid.js
11 * Class: OpenLayers.Layer.TileCache
12 * A read only TileCache layer. Used to requests tiles cached by TileCache in
13 * a web accessible cache. This means that you have to pre-populate your
14 * cache before this layer can be used. It is meant only to read tiles
15 * created by TileCache, and not to make calls to TileCache for tile
16 * creation. Create a new instance with the
17 * <OpenLayers.Layer.TileCache> constructor.
20 * - <OpenLayers.Layer.Grid>
22 OpenLayers.Layer.TileCache = OpenLayers.Class(OpenLayers.Layer.Grid, {
25 * APIProperty: isBaseLayer
26 * {Boolean} Treat this layer as a base layer. Default is true.
32 * {String} Mime type of the images returned. Default is image/png.
37 * APIProperty: serverResolutions
38 * {Array} A list of all resolutions available on the server. Only set this
39 * property if the map resolutions differs from the server.
41 serverResolutions: null,
44 * Constructor: OpenLayers.Layer.TileCache
45 * Create a new read only TileCache layer.
48 * name - {String} Name of the layer displayed in the interface
49 * url - {String} Location of the web accessible cache (not the location of
50 * your tilecache script!)
51 * layername - {String} Layer name as defined in the TileCache
53 * options - {Object} Optional object with properties to be set on the
54 * layer. Note that you should speficy your resolutions to match
55 * your TileCache configuration. This can be done by setting
56 * the resolutions array directly (here or on the map), by setting
57 * maxResolution and numZoomLevels, or by using scale based properties.
59 initialize: function(name, url, layername, options) {
60 this.layername = layername;
61 OpenLayers.Layer.Grid.prototype.initialize.apply(this,
62 [name, url, {}, options]);
63 this.extension = this.format.split('/')[1].toLowerCase();
64 this.extension = (this.extension == 'jpg') ? 'jpeg' : this.extension;
72 * {<OpenLayers.Layer.TileCache>} An exact clone of this
73 * <OpenLayers.Layer.TileCache>
75 clone: function (obj) {
78 obj = new OpenLayers.Layer.TileCache(this.name,
84 //get all additions from superclasses
85 obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
87 // copy/set any non-init, non-simple values here
96 * bounds - {<OpenLayers.Bounds>}
99 * {String} A string with the layer's url and parameters and also the
100 * passed-in bounds and appropriate tile size specified as parameters.
102 getURL: function(bounds) {
103 var res = this.map.getResolution();
104 var bbox = this.maxExtent;
105 var size = this.tileSize;
106 var tileX = Math.round((bounds.left - bbox.left) / (res * size.w));
107 var tileY = Math.round((bounds.bottom - bbox.bottom) / (res * size.h));
108 var tileZ = this.serverResolutions != null ?
109 OpenLayers.Util.indexOf(this.serverResolutions, res) :
112 * Zero-pad a positive integer.
117 * {String} A zero-padded string
119 function zeroPad(number, length) {
120 number = String(number);
122 for(var i=0; i<length; ++i) {
125 return zeros.join('').substring(0, length - number.length) + number;
130 zeroPad(parseInt(tileX / 1000000), 3),
131 zeroPad((parseInt(tileX / 1000) % 1000), 3),
132 zeroPad((parseInt(tileX) % 1000), 3),
133 zeroPad(parseInt(tileY / 1000000), 3),
134 zeroPad((parseInt(tileY / 1000) % 1000), 3),
135 zeroPad((parseInt(tileY) % 1000), 3) + '.' + this.extension
137 var path = components.join('/');
139 if (url instanceof Array) {
140 url = this.selectUrl(path, url);
142 url = (url.charAt(url.length - 1) == '/') ? url : url + '/';
148 * Create a tile, initialize it, and add it to the layer div.
151 * bounds - {<OpenLayers.Bounds>}
152 * position - {<OpenLayers.Pixel>}
155 * {<OpenLayers.Tile.Image>} The added <OpenLayers.Tile.Image>
157 addTile:function(bounds, position) {
158 var url = this.getURL(bounds);
159 return new OpenLayers.Tile.Image(this, position, bounds,
163 CLASS_NAME: "OpenLayers.Layer.TileCache"