Project

General

Profile

Download (5.64 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Layer/Grid.js
8
 */
9

    
10
/** 
11
 * Class: OpenLayers.Layer.XYZ
12
 * The XYZ class is designed to make it easier for people who have tiles
13
 * arranged by a standard XYZ grid. 
14
 * 
15
 * Inherits from:
16
 *  - <OpenLayers.Layer.Grid>
17
 */
18
OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
19
    
20
    /**
21
     * APIProperty: isBaseLayer
22
     * Default is true, as this is designed to be a base tile source. 
23
     */
24
    isBaseLayer: true,
25
    
26
    /**
27
     * APIProperty: sphericalMercator
28
     * Whether the tile extents should be set to the defaults for 
29
     *    spherical mercator. Useful for things like OpenStreetMap.
30
     *    Default is false, except for the OSM subclass.
31
     */
32
    sphericalMercator: false,
33

    
34
    /**
35
     * APIProperty: zoomOffset
36
     * {Number} If your cache has more zoom levels than you want to provide
37
     *     access to with this layer, supply a zoomOffset.  This zoom offset
38
     *     is added to the current map zoom level to determine the level
39
     *     for a requested tile.  For example, if you supply a zoomOffset
40
     *     of 3, when the map is at the zoom 0, tiles will be requested from
41
     *     level 3 of your cache.  Default is 0 (assumes cache level and map
42
     *     zoom are equivalent).  Using <zoomOffset> is an alternative to
43
     *     setting <serverResolutions> if you only want to expose a subset
44
     *     of the server resolutions.
45
     */
46
    zoomOffset: 0,
47
    
48
    /**
49
     * APIProperty: serverResolutions
50
     * {Array} A list of all resolutions available on the server.  Only set this
51
     *     property if the map resolutions differ from the server. This
52
     *     property serves two purposes. (a) <serverResolutions> can include
53
     *     resolutions that the server supports and that you don't want to
54
     *     provide with this layer; you can also look at <zoomOffset>, which is
55
     *     an alternative to <serverResolutions> for that specific purpose.
56
     *     (b) The map can work with resolutions that aren't supported by
57
     *     the server, i.e. that aren't in <serverResolutions>. When the
58
     *     map is displayed in such a resolution data for the closest
59
     *     server-supported resolution is loaded and the layer div is
60
     *     stretched as necessary.
61
     */
62
    serverResolutions: null,
63

    
64
    /**
65
     * Constructor: OpenLayers.Layer.XYZ
66
     *
67
     * Parameters:
68
     * name - {String}
69
     * url - {String}
70
     * options - {Object} Hashtable of extra options to tag onto the layer
71
     */
72
    initialize: function(name, url, options) {
73
        if (options && options.sphericalMercator || this.sphericalMercator) {
74
            options = OpenLayers.Util.extend({
75
                projection: "EPSG:900913",
76
                numZoomLevels: 19
77
            }, options);
78
        }
79
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, [
80
            name || this.name, url || this.url, {}, options
81
        ]);
82
    },
83
    
84
    /**
85
     * APIMethod: clone
86
     * Create a clone of this layer
87
     *
88
     * Parameters:
89
     * obj - {Object} Is this ever used?
90
     * 
91
     * Returns:
92
     * {<OpenLayers.Layer.XYZ>} An exact clone of this OpenLayers.Layer.XYZ
93
     */
94
    clone: function (obj) {
95
        
96
        if (obj == null) {
97
            obj = new OpenLayers.Layer.XYZ(this.name,
98
                                            this.url,
99
                                            this.getOptions());
100
        }
101

    
102
        //get all additions from superclasses
103
        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
104

    
105
        return obj;
106
    },    
107

    
108
    /**
109
     * Method: getURL
110
     *
111
     * Parameters:
112
     * bounds - {<OpenLayers.Bounds>}
113
     *
114
     * Returns:
115
     * {String} A string with the layer's url and parameters and also the
116
     *          passed-in bounds and appropriate tile size specified as
117
     *          parameters
118
     */
119
    getURL: function (bounds) {
120
        var xyz = this.getXYZ(bounds);
121
        var url = this.url;
122
        if (OpenLayers.Util.isArray(url)) {
123
            var s = '' + xyz.x + xyz.y + xyz.z;
124
            url = this.selectUrl(s, url);
125
        }
126
        
127
        return OpenLayers.String.format(url, xyz);
128
    },
129
    
130
    /**
131
     * Method: getXYZ
132
     * Calculates x, y and z for the given bounds.
133
     *
134
     * Parameters:
135
     * bounds - {<OpenLayers.Bounds>}
136
     *
137
     * Returns:
138
     * {Object} - an object with x, y and z properties.
139
     */
140
    getXYZ: function(bounds) {
141
        var res = this.getServerResolution();
142
        var x = Math.round((bounds.left - this.maxExtent.left) /
143
            (res * this.tileSize.w));
144
        var y = Math.round((this.maxExtent.top - bounds.top) /
145
            (res * this.tileSize.h));
146
        var z = this.getServerZoom();
147

    
148
        if (this.wrapDateLine) {
149
            var limit = Math.pow(2, z);
150
            x = ((x % limit) + limit) % limit;
151
        }
152

    
153
        return {'x': x, 'y': y, 'z': z};
154
    },
155
    
156
    /* APIMethod: setMap
157
     * When the layer is added to a map, then we can fetch our origin 
158
     *    (if we don't have one.) 
159
     * 
160
     * Parameters:
161
     * map - {<OpenLayers.Map>}
162
     */
163
    setMap: function(map) {
164
        OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
165
        if (!this.tileOrigin) { 
166
            this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
167
                                                this.maxExtent.bottom);
168
        }                                       
169
    },
170

    
171
    CLASS_NAME: "OpenLayers.Layer.XYZ"
172
});
(30-30/31)