]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/TileCache.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / TileCache.js
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. */
4
5
6 /**
7  * @requires OpenLayers/Layer/Grid.js
8  */
9
10 /**
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.
18  *
19  * Inherits from:
20  *  - <OpenLayers.Layer.Grid>
21  */
22 OpenLayers.Layer.TileCache = OpenLayers.Class(OpenLayers.Layer.Grid, {
23
24     /** 
25      * APIProperty: isBaseLayer
26      * {Boolean} Treat this layer as a base layer.  Default is true.
27      */
28     isBaseLayer: true,
29     
30     /** 
31      * APIProperty: format
32      * {String} Mime type of the images returned.  Default is image/png.
33      */
34     format: 'image/png',
35
36     /**
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.
40      */
41     serverResolutions: null,
42
43     /**
44      * Constructor: OpenLayers.Layer.TileCache
45      * Create a new read only TileCache layer.
46      *
47      * Parameters:
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 
52      *     configuration
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.
58      */
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;
65     },    
66
67     /**
68      * APIMethod: clone
69      * obj - {Object} 
70      * 
71      * Returns:
72      * {<OpenLayers.Layer.TileCache>} An exact clone of this 
73      *     <OpenLayers.Layer.TileCache>
74      */
75     clone: function (obj) {
76         
77         if (obj == null) {
78             obj = new OpenLayers.Layer.TileCache(this.name,
79                                                  this.url,
80                                                  this.layername,
81                                                  this.options);
82         }
83
84         //get all additions from superclasses
85         obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
86
87         // copy/set any non-init, non-simple values here
88
89         return obj;
90     },    
91     
92     /**
93      * Method: getURL
94      *
95      * Parameters:
96      * bounds - {<OpenLayers.Bounds>} 
97      * 
98      * Returns:
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.
101      */
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) :
110             this.map.getZoom();
111         /**
112          * Zero-pad a positive integer.
113          * number - {Int} 
114          * length - {Int} 
115          *
116          * Returns:
117          * {String} A zero-padded string
118          */
119         function zeroPad(number, length) {
120             number = String(number);
121             var zeros = [];
122             for(var i=0; i<length; ++i) {
123                 zeros.push('0');
124             }
125             return zeros.join('').substring(0, length - number.length) + number;
126         }
127         var components = [
128             this.layername,
129             zeroPad(tileZ, 2),
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
136         ];
137         var path = components.join('/'); 
138         var url = this.url;
139         if (url instanceof Array) {
140             url = this.selectUrl(path, url);
141         }
142         url = (url.charAt(url.length - 1) == '/') ? url : url + '/';
143         return url + path;
144     },
145
146     /**
147      * Method: addTile
148      * Create a tile, initialize it, and add it to the layer div. 
149      *
150      * Parameters: 
151      * bounds - {<OpenLayers.Bounds>} 
152      * position - {<OpenLayers.Pixel>}
153      *
154      * Returns:
155      * {<OpenLayers.Tile.Image>} The added <OpenLayers.Tile.Image>
156      */
157     addTile:function(bounds, position) {
158         var url = this.getURL(bounds);
159         return new OpenLayers.Tile.Image(this, position, bounds, 
160                                              url, this.tileSize);
161     },
162     
163     CLASS_NAME: "OpenLayers.Layer.TileCache"
164 });