]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/XYZ.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / XYZ.js
1 /* Copyright (c) 2006-2009 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  * @requires OpenLayers/Layer/Grid.js
7  */
8
9 /** 
10  * Class: OpenLayers.Layer.XYZ
11  * The XYZ class is designed to make it easier for people who have tiles
12  * arranged by a standard XYZ grid. 
13  */
14 OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
15     
16     /**
17      * APIProperty: isBaseLayer
18      * Default is true, as this is designed to be a base tile source. 
19      */
20     isBaseLayer: true,
21     
22     /**
23      * APIProperty: sphericalMecator
24      * Whether the tile extents should be set to the defaults for 
25      *    spherical mercator. Useful for things like OpenStreetMap.
26      *    Default is false, except for the OSM subclass.
27      */
28     sphericalMercator: false,
29
30     /**
31      * Constructor: OpenLayers.Layer.OSM
32      *
33      * Parameters:
34      * name - {String}
35      * url - {String}
36      * options - {Object} Hashtable of extra options to tag onto the layer
37      */
38     initialize: function(name, url, options) {
39         if (options && options.sphericalMercator || this.sphericalMercator) {
40             options = OpenLayers.Util.extend({
41                 maxExtent: new OpenLayers.Bounds(
42                     -128 * 156543.0339,
43                     -128 * 156543.0339,
44                     128 * 156543.0339,
45                     128 * 156543.0339
46                 ),
47                 maxResolution: 156543.0339,
48                 numZoomLevels: 19,
49                 units: "m",
50                 projection: "EPSG:900913"
51             }, options);
52         }
53         url = url || this.url;
54         name = name || this.name;
55         var newArguments = [name, url, {}, options];
56         OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
57     },
58     
59     /**
60      * APIMethod: clone
61      * Create a clone of this layer
62      *
63      * Parameters:
64      * obj - {Object} Is this ever used?
65      * 
66      * Returns:
67      * {<OpenLayers.Layer.Grid>} An exact clone of this OpenLayers.Layer.Grid
68      */
69     clone: function (obj) {
70         
71         if (obj == null) {
72             obj = new OpenLayers.Layer.XYZ(this.name,
73                                             this.url,
74                                             this.options);
75         }
76
77         //get all additions from superclasses
78         obj = OpenLayers.Layer.HTTPRequest.prototype.clone.apply(this, [obj]);
79
80         // copy/set any non-init, non-simple values here
81         if (this.tileSize != null) {
82             obj.tileSize = this.tileSize.clone();
83         }
84         
85         // we do not want to copy reference to grid, so we make a new array
86         obj.grid = [];
87
88         return obj;
89     },    
90
91     /**
92      * Method: getUrl
93      *
94      * Parameters:
95      * bounds - {<OpenLayers.Bounds>}
96      *
97      * Returns:
98      * {String} A string with the layer's url and parameters and also the
99      *          passed-in bounds and appropriate tile size specified as
100      *          parameters
101      */
102     getURL: function (bounds) {
103         var res = this.map.getResolution();
104         var x = Math.round((bounds.left - this.maxExtent.left) 
105             / (res * this.tileSize.w));
106         var y = Math.round((this.maxExtent.top - bounds.top) 
107             / (res * this.tileSize.h));
108         var z = this.map.getZoom();
109         var limit = Math.pow(2, z);
110
111         var url = this.url;
112         var s = '' + x + y + z;
113         if (url instanceof Array)
114         {
115             url = this.selectUrl(s, url);
116         }
117         
118         var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z});
119
120         return path;
121     },
122     
123     /**
124      * Method: addTile
125      * addTile creates a tile, initializes it, and adds it to the layer div. 
126      * 
127      * Parameters:
128      * bounds - {<OpenLayers.Bounds>}
129      * position - {<OpenLayers.Pixel>}
130      * 
131      * Returns:
132      * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image
133      */
134     addTile:function(bounds,position) {
135         return new OpenLayers.Tile.Image(this, position, bounds, 
136                                          null, this.tileSize);
137     },
138      
139     /* APIMethod: setMap
140      * When the layer is added to a map, then we can fetch our origin 
141      *    (if we don't have one.) 
142      * 
143      * Parameters:
144      * map - {<OpenLayers.Map>}
145      */
146     setMap: function(map) {
147         OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
148         if (!this.tileOrigin) { 
149             this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
150                                                 this.maxExtent.bottom);
151         }                                       
152     },
153
154     CLASS_NAME: "OpenLayers.Layer.XYZ"
155 });
156
157
158 /**
159  * Class: OpenLayers.Layer.OSM
160  * A class to access OpenStreetMap tiles. By default, uses the OpenStreetMap
161  *    hosted tile.openstreetmap.org 'Mapnik' tileset. If you wish to use
162  *    tiles@home / osmarender layer instead, you can pass a layer like:
163  * 
164  * (code)
165  *     new OpenLayers.Layer.OSM("t@h", 
166  *       "http://tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png"); 
167  * (end)
168  *
169  * This layer defaults to Spherical Mercator.
170  */
171
172 OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
173      name: "OpenStreetMap",
174      attribution: "Data CC-By-SA by <a href='http://openstreetmap.org/'>OpenStreetMap</a>",
175      sphericalMercator: true,
176      url: 'http://tile.openstreetmap.org/${z}/${x}/${y}.png',
177      CLASS_NAME: "OpenLayers.Layer.OSM"
178 });