]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/TMS.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / TMS.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  * @requires OpenLayers/Tile/Image.js
9  */
10
11 /**
12  * Class: OpenLayers.Layer.TMS
13  * 
14  * Inherits from:
15  *  - <OpenLayers.Layer.Grid>
16  */
17 OpenLayers.Layer.TMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
18
19     /**
20      * APIProperty: serviceVersion
21      * {String}
22      */
23     serviceVersion: "1.0.0",
24
25     /**
26      * APIProperty: isBaseLayer
27      * {Boolean}
28      */
29     isBaseLayer: true,
30
31     /**
32      * APIProperty: tileOrigin
33      * {<OpenLayers.Pixel>}
34      */
35     tileOrigin: null,
36
37     /**
38      * APIProperty: serverResolutions
39      * {Array} A list of all resolutions available on the server.  Only set this 
40      *     property if the map resolutions differs from the server.
41      */
42     serverResolutions: null,
43
44     /**
45      * Constructor: OpenLayers.Layer.TMS
46      * 
47      * Parameters:
48      * name - {String}
49      * url - {String}
50      * options - {Object} Hashtable of extra options to tag onto the layer
51      */
52     initialize: function(name, url, options) {
53         var newArguments = [];
54         newArguments.push(name, url, {}, options);
55         OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
56     },    
57
58     /**
59      * APIMethod:destroy
60      */
61     destroy: function() {
62         // for now, nothing special to do here. 
63         OpenLayers.Layer.Grid.prototype.destroy.apply(this, arguments);  
64     },
65
66     
67     /**
68      * APIMethod: clone
69      * 
70      * Parameters:
71      * obj - {Object}
72      * 
73      * Returns:
74      * {<OpenLayers.Layer.TMS>} An exact clone of this <OpenLayers.Layer.TMS>
75      */
76     clone: function (obj) {
77         
78         if (obj == null) {
79             obj = new OpenLayers.Layer.TMS(this.name,
80                                            this.url,
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 
101      *          parameters
102      */
103     getURL: function (bounds) {
104         bounds = this.adjustBounds(bounds);
105         var res = this.map.getResolution();
106         var x = Math.round((bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w));
107         var y = Math.round((bounds.bottom - this.tileOrigin.lat) / (res * this.tileSize.h));
108         var z = this.serverResolutions != null ?
109             OpenLayers.Util.indexOf(this.serverResolutions, res) :
110             this.map.getZoom();
111         var path = this.serviceVersion + "/" + this.layername + "/" + z + "/" + x + "/" + y + "." + this.type; 
112         var url = this.url;
113         if (url instanceof Array) {
114             url = this.selectUrl(path, url);
115         }
116         return url + path;
117     },
118
119     /**
120      * Method: addTile
121      * addTile creates a tile, initializes it, and adds it to the layer div. 
122      * 
123      * Parameters:
124      * bounds - {<OpenLayers.Bounds>}
125      * position - {<OpenLayers.Pixel>}
126      * 
127      * Returns:
128      * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image
129      */
130     addTile:function(bounds,position) {
131         return new OpenLayers.Tile.Image(this, position, bounds, 
132                                          null, this.tileSize);
133     },
134
135     /** 
136      * APIMethod: setMap
137      * When the layer is added to a map, then we can fetch our origin 
138      *    (if we don't have one.) 
139      * 
140      * Parameters:
141      * map - {<OpenLayers.Map>}
142      */
143     setMap: function(map) {
144         OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
145         if (!this.tileOrigin) { 
146             this.tileOrigin = new OpenLayers.LonLat(this.map.maxExtent.left,
147                                                 this.map.maxExtent.bottom);
148         }                                       
149     },
150
151     CLASS_NAME: "OpenLayers.Layer.TMS"
152 });