]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/KaMapCache.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / KaMapCache.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  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  * @requires OpenLayers/Layer/KaMap.js
9  */
10
11 /**
12  * Class: OpenLayers.Layer.KaMapCache
13  * 
14  * This class is designed to talk directly to a web-accessible ka-Map
15  * cache generated by the precache2.php script.
16  * 
17  * To create a a new KaMapCache layer, you must indicate also the "i" parameter
18  * (that will be used to calculate the file extension), and another special
19  * parameter, object names "metaTileSize", with "h" (height) and "w" (width)
20  * properties.
21  * 
22  *     // Create a new kaMapCache layer. 
23  *     var kamap_base = new OpenLayers.Layer.KaMapCache(
24  *         "Satellite",
25  *         "http://www.example.org/web/acessible/cache",
26  *         {g: "satellite", map: "world", i: 'png24', metaTileSize: {w: 5, h: 5} }
27  *       );
28  *    
29  *     // Create an kaMapCache overlay layer (using "isBaseLayer: false"). 
30  *     // Forces the output to be a "gif", using the "i" parameter.
31  *     var kamap_overlay = new OpenLayers.Layer.KaMapCache(
32  *         "Streets",
33  *         "http://www.example.org/web/acessible/cache",
34  *         {g: "streets", map: "world", i: "gif", metaTileSize: {w: 5, h: 5} },
35  *         {isBaseLayer: false}
36  *       );
37  *
38  * The cache URLs must look like: 
39  *   var/cache/World/50000/Group_Name/def/t-440320/l20480
40  * 
41  * This means that the cache generated via tile.php will *not* work with
42  *     this class, and should instead use the KaMap layer.
43  *
44  * More information is available in Ticket #1518.
45  * 
46  * Inherits from:
47  *  - <OpenLayers.Layer.KaMap>
48  *  - <OpenLayers.Layer.Grid>
49  */
50 OpenLayers.Layer.KaMapCache = OpenLayers.Class(OpenLayers.Layer.KaMap, {
51
52     /**
53      * Constant: IMAGE_EXTENSIONS
54      * {Object} Simple hash map to convert format to extension.
55      */
56     IMAGE_EXTENSIONS: {
57         'jpeg': 'jpg',
58         'gif' : 'gif',
59         'png' : 'png',
60         'png8' : 'png',
61         'png24' : 'png',
62         'dithered' : 'png'
63     },
64     
65     /**
66      * Constant: DEFAULT_FORMAT
67      * {Object} Simple hash map to convert format to extension.
68      */
69     DEFAULT_FORMAT: 'jpeg',
70     
71     /**
72      * Constructor: OpenLayers.Layer.KaMapCache
73      * 
74      * Parameters:
75      * name - {String}
76      * url - {String}
77      * params - {Object} Parameters to be sent to the HTTP server in the
78      *    query string for the tile. The format can be set via the 'i'
79      *    parameter (defaults to jpg) , and the map should be set via 
80      *    the 'map' parameter. It has been reported that ka-Map may behave
81      *    inconsistently if your format parameter does not match the format
82      *    parameter configured in your config.php. (See ticket #327 for more
83      *    information.)
84      * options - {Object} Additional options for the layer. Any of the 
85      *     APIProperties listed on this layer, and any layer types it
86      *     extends, can be overridden through the options parameter. 
87      */
88     initialize: function(name, url, params, options) {
89         OpenLayers.Layer.KaMap.prototype.initialize.apply(this, arguments);
90         this.extension = this.IMAGE_EXTENSIONS[this.params.i.toLowerCase() || DEFAULT_FORMAT];
91     },
92
93     /**
94      * Method: getURL
95      * 
96      * Parameters:
97      * bounds - {<OpenLayers.Bounds>} 
98      * 
99      * Returns:
100      * {String} A string with the layer's url and parameters and also the 
101      *          passed-in bounds and appropriate tile size specified as 
102      *          parameters
103      */
104     getURL: function (bounds) {
105         bounds = this.adjustBounds(bounds);
106         var mapRes = this.map.getResolution();
107         var scale = Math.round((this.map.getScale() * 10000)) / 10000;
108         var pX = Math.round(bounds.left / mapRes);
109         var pY = -Math.round(bounds.top / mapRes);
110
111         var metaX = Math.floor(pX / this.tileSize.w / this.params.metaTileSize.w) * this.tileSize.w * this.params.metaTileSize.w;
112         var metaY = Math.floor(pY / this.tileSize.h / this.params.metaTileSize.h) * this.tileSize.h * this.params.metaTileSize.h;
113
114         // if url is not a string, it should be an array of strings,
115         // in which case we will deterministically select one of them in
116         // order to evenly distribute requests to different urls.
117         //
118         var url = this.url;
119         if (url instanceof Array) {
120             url = this.selectUrl(paramsString, url);
121         }  
122     
123         var components = [
124             url,
125             "/",
126             this.params.map,
127             "/",
128             scale,
129             "/",
130             this.params.g.replace(/\s/g, '_'),
131             "/def/t", 
132             metaY,
133             "/l",
134             metaX,
135             "/t",
136             pY,
137             "l",
138             pX,
139             ".",
140             this.extension
141           ];
142           
143         return components.join("");
144     },
145
146     CLASS_NAME: "OpenLayers.Layer.KaMapCache"
147 });