1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Layer/Grid.js |
---|
8 | * @requires OpenLayers/Tile/Image.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Layer.WMTS |
---|
13 | * Instances of the WMTS class allow viewing of tiles from a service that |
---|
14 | * implements the OGC WMTS specification version 1.0.0. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Layer.Grid> |
---|
18 | */ |
---|
19 | OpenLayers.Layer.WMTS = OpenLayers.Class(OpenLayers.Layer.Grid, { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIProperty: isBaseLayer |
---|
23 | * {Boolean} The layer will be considered a base layer. Default is true. |
---|
24 | */ |
---|
25 | isBaseLayer: true, |
---|
26 | |
---|
27 | /** |
---|
28 | * Property: version |
---|
29 | * {String} WMTS version. Default is "1.0.0". |
---|
30 | */ |
---|
31 | version: "1.0.0", |
---|
32 | |
---|
33 | /** |
---|
34 | * APIProperty: requestEncoding |
---|
35 | * {String} Request encoding. Can be "REST" or "KVP". Default is "KVP". |
---|
36 | */ |
---|
37 | requestEncoding: "KVP", |
---|
38 | |
---|
39 | /** |
---|
40 | * APIProperty: url |
---|
41 | * {String} The base URL for the WMTS service. Must be provided. |
---|
42 | */ |
---|
43 | url: null, |
---|
44 | |
---|
45 | /** |
---|
46 | * APIProperty: layer |
---|
47 | * {String} The layer identifier advertised by the WMTS service. Must be |
---|
48 | * provided. |
---|
49 | */ |
---|
50 | layer: null, |
---|
51 | |
---|
52 | /** |
---|
53 | * APIProperty: matrixSet |
---|
54 | * {String} One of the advertised matrix set identifiers. Must be provided. |
---|
55 | */ |
---|
56 | matrixSet: null, |
---|
57 | |
---|
58 | /** |
---|
59 | * APIProperty: style |
---|
60 | * {String} One of the advertised layer styles. Must be provided. |
---|
61 | */ |
---|
62 | style: null, |
---|
63 | |
---|
64 | /** |
---|
65 | * APIProperty: format |
---|
66 | * {String} The image MIME type. Default is "image/jpeg". |
---|
67 | */ |
---|
68 | format: "image/jpeg", |
---|
69 | |
---|
70 | /** |
---|
71 | * APIProperty: tileOrigin |
---|
72 | * {<OpenLayers.LonLat>} The top-left corner of the tile matrix in map |
---|
73 | * units. If the tile origin for each matrix in a set is different, |
---|
74 | * the <matrixIds> should include a topLeftCorner property. If |
---|
75 | * not provided, the tile origin will default to the top left corner |
---|
76 | * of the layer <maxExtent>. |
---|
77 | */ |
---|
78 | tileOrigin: null, |
---|
79 | |
---|
80 | /** |
---|
81 | * APIProperty: tileFullExtent |
---|
82 | * {<OpenLayers.Bounds>} The full extent of the tile set. If not supplied, |
---|
83 | * the layer's <maxExtent> property will be used. |
---|
84 | */ |
---|
85 | tileFullExtent: null, |
---|
86 | |
---|
87 | /** |
---|
88 | * APIProperty: formatSuffix |
---|
89 | * {String} For REST request encoding, an image format suffix must be |
---|
90 | * included in the request. If not provided, the suffix will be derived |
---|
91 | * from the <format> property. |
---|
92 | */ |
---|
93 | formatSuffix: null, |
---|
94 | |
---|
95 | /** |
---|
96 | * APIProperty: matrixIds |
---|
97 | * {Array} A list of tile matrix identifiers. If not provided, the matrix |
---|
98 | * identifiers will be assumed to be integers corresponding to the |
---|
99 | * map zoom level. If a list of strings is provided, each item should |
---|
100 | * be the matrix identifier that corresponds to the map zoom level. |
---|
101 | * Additionally, a list of objects can be provided. Each object should |
---|
102 | * describe the matrix as presented in the WMTS capabilities. These |
---|
103 | * objects should have the propertes shown below. |
---|
104 | * |
---|
105 | * Matrix properties: |
---|
106 | * identifier - {String} The matrix identifier (required). |
---|
107 | * topLeftCorner - {<OpenLayers.LonLat>} The top left corner of the |
---|
108 | * matrix. Must be provided if different than the layer <tileOrigin>. |
---|
109 | * tileWidth - {Number} The tile width for the matrix. Must be provided |
---|
110 | * if different than the width given in the layer <tileSize>. |
---|
111 | * tileHeight - {Number} The tile height for the matrix. Must be provided |
---|
112 | * if different than the height given in the layer <tileSize>. |
---|
113 | */ |
---|
114 | matrixIds: null, |
---|
115 | |
---|
116 | /** |
---|
117 | * APIProperty: dimensions |
---|
118 | * {Array} For RESTful request encoding, extra dimensions may be specified. |
---|
119 | * Items in this list should be property names in the <params> object. |
---|
120 | * Values of extra dimensions will be determined from the corresponding |
---|
121 | * values in the <params> object. |
---|
122 | */ |
---|
123 | dimensions: null, |
---|
124 | |
---|
125 | /** |
---|
126 | * APIProperty: params |
---|
127 | * {Object} Extra parameters to include in tile requests. For KVP |
---|
128 | * <requestEncoding>, these properties will be encoded in the request |
---|
129 | * query string. For REST <requestEncoding>, these properties will |
---|
130 | * become part of the request path, with order determined by the |
---|
131 | * <dimensions> list. |
---|
132 | */ |
---|
133 | params: null, |
---|
134 | |
---|
135 | /** |
---|
136 | * APIProperty: zoomOffset |
---|
137 | * {Number} If your cache has more levels than you want to provide |
---|
138 | * access to with this layer, supply a zoomOffset. This zoom offset |
---|
139 | * is added to the current map zoom level to determine the level |
---|
140 | * for a requested tile. For example, if you supply a zoomOffset |
---|
141 | * of 3, when the map is at the zoom 0, tiles will be requested from |
---|
142 | * level 3 of your cache. Default is 0 (assumes cache level and map |
---|
143 | * zoom are equivalent). Additionally, if this layer is to be used |
---|
144 | * as an overlay and the cache has fewer zoom levels than the base |
---|
145 | * layer, you can supply a negative zoomOffset. For example, if a |
---|
146 | * map zoom level of 1 corresponds to your cache level zero, you would |
---|
147 | * supply a -1 zoomOffset (and set the maxResolution of the layer |
---|
148 | * appropriately). The zoomOffset value has no effect if complete |
---|
149 | * matrix definitions (including scaleDenominator) are supplied in |
---|
150 | * the <matrixIds> property. Defaults to 0 (no zoom offset). |
---|
151 | */ |
---|
152 | zoomOffset: 0, |
---|
153 | |
---|
154 | /** |
---|
155 | * Property: formatSuffixMap |
---|
156 | * {Object} a map between WMTS 'format' request parameter and tile image file suffix |
---|
157 | */ |
---|
158 | formatSuffixMap: { |
---|
159 | "image/png": "png", |
---|
160 | "image/png8": "png", |
---|
161 | "image/png24": "png", |
---|
162 | "image/png32": "png", |
---|
163 | "png": "png", |
---|
164 | "image/jpeg": "jpg", |
---|
165 | "image/jpg": "jpg", |
---|
166 | "jpeg": "jpg", |
---|
167 | "jpg": "jpg" |
---|
168 | }, |
---|
169 | |
---|
170 | /** |
---|
171 | * Property: matrix |
---|
172 | * {Object} Matrix definition for the current map resolution. Updated by |
---|
173 | * the <updateMatrixProperties> method. |
---|
174 | */ |
---|
175 | matrix: null, |
---|
176 | |
---|
177 | /** |
---|
178 | * Constructor: OpenLayers.Layer.WMTS |
---|
179 | * Create a new WMTS layer. |
---|
180 | * |
---|
181 | * Example: |
---|
182 | * (code) |
---|
183 | * var wmts = new OpenLayers.Layer.WMTS({ |
---|
184 | * name: "My WMTS Layer", |
---|
185 | * url: "http://example.com/wmts", |
---|
186 | * layer: "layer_id", |
---|
187 | * style: "default", |
---|
188 | * matrixSet: "matrix_id" |
---|
189 | * }); |
---|
190 | * (end) |
---|
191 | * |
---|
192 | * Parameters: |
---|
193 | * config - {Object} Configuration properties for the layer. |
---|
194 | * |
---|
195 | * Required configuration properties: |
---|
196 | * url - {String} The base url for the service. See the <url> property. |
---|
197 | * layer - {String} The layer identifier. See the <layer> property. |
---|
198 | * style - {String} The layer style identifier. See the <style> property. |
---|
199 | * matrixSet - {String} The tile matrix set identifier. See the <matrixSet> |
---|
200 | * property. |
---|
201 | * |
---|
202 | * Any other documented layer properties can be provided in the config object. |
---|
203 | */ |
---|
204 | initialize: function(config) { |
---|
205 | |
---|
206 | // confirm required properties are supplied |
---|
207 | var required = { |
---|
208 | url: true, |
---|
209 | layer: true, |
---|
210 | style: true, |
---|
211 | matrixSet: true |
---|
212 | }; |
---|
213 | for (var prop in required) { |
---|
214 | if (!(prop in config)) { |
---|
215 | throw new Error("Missing property '" + prop + "' in layer configuration."); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | config.params = OpenLayers.Util.upperCaseObject(config.params); |
---|
220 | var args = [config.name, config.url, config.params, config]; |
---|
221 | OpenLayers.Layer.Grid.prototype.initialize.apply(this, args); |
---|
222 | |
---|
223 | |
---|
224 | // determine format suffix (for REST) |
---|
225 | if (!this.formatSuffix) { |
---|
226 | this.formatSuffix = this.formatSuffixMap[this.format] || this.format.split("/").pop(); |
---|
227 | } |
---|
228 | |
---|
229 | // expand matrixIds (may be array of string or array of object) |
---|
230 | if (this.matrixIds) { |
---|
231 | var len = this.matrixIds.length; |
---|
232 | if (len && typeof this.matrixIds[0] === "string") { |
---|
233 | var ids = this.matrixIds; |
---|
234 | this.matrixIds = new Array(len); |
---|
235 | for (var i=0; i<len; ++i) { |
---|
236 | this.matrixIds[i] = {identifier: ids[i]}; |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | }, |
---|
242 | |
---|
243 | /** |
---|
244 | * Method: setMap |
---|
245 | */ |
---|
246 | setMap: function() { |
---|
247 | OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments); |
---|
248 | this.updateMatrixProperties(); |
---|
249 | }, |
---|
250 | |
---|
251 | /** |
---|
252 | * Method: updateMatrixProperties |
---|
253 | * Called when map resolution changes to update matrix related properties. |
---|
254 | */ |
---|
255 | updateMatrixProperties: function() { |
---|
256 | this.matrix = this.getMatrix(); |
---|
257 | if (this.matrix) { |
---|
258 | if (this.matrix.topLeftCorner) { |
---|
259 | this.tileOrigin = this.matrix.topLeftCorner; |
---|
260 | } |
---|
261 | if (this.matrix.tileWidth && this.matrix.tileHeight) { |
---|
262 | this.tileSize = new OpenLayers.Size( |
---|
263 | this.matrix.tileWidth, this.matrix.tileHeight |
---|
264 | ); |
---|
265 | } |
---|
266 | if (!this.tileOrigin) { |
---|
267 | this.tileOrigin = new OpenLayers.LonLat( |
---|
268 | this.maxExtent.left, this.maxExtent.top |
---|
269 | ); |
---|
270 | } |
---|
271 | if (!this.tileFullExtent) { |
---|
272 | this.tileFullExtent = this.maxExtent; |
---|
273 | } |
---|
274 | } |
---|
275 | }, |
---|
276 | |
---|
277 | /** |
---|
278 | * Method: moveTo |
---|
279 | * |
---|
280 | * Parameters: |
---|
281 | * bound - {<OpenLayers.Bounds>} |
---|
282 | * zoomChanged - {Boolean} Tells when zoom has changed, as layers have to |
---|
283 | * do some init work in that case. |
---|
284 | * dragging - {Boolean} |
---|
285 | */ |
---|
286 | moveTo:function(bounds, zoomChanged, dragging) { |
---|
287 | if (zoomChanged || !this.matrix) { |
---|
288 | this.updateMatrixProperties(); |
---|
289 | } |
---|
290 | return OpenLayers.Layer.Grid.prototype.moveTo.apply(this, arguments); |
---|
291 | }, |
---|
292 | |
---|
293 | /** |
---|
294 | * APIMethod: clone |
---|
295 | * |
---|
296 | * Parameters: |
---|
297 | * obj - {Object} |
---|
298 | * |
---|
299 | * Returns: |
---|
300 | * {<OpenLayers.Layer.WMTS>} An exact clone of this <OpenLayers.Layer.WMTS> |
---|
301 | */ |
---|
302 | clone: function(obj) { |
---|
303 | if (obj == null) { |
---|
304 | obj = new OpenLayers.Layer.WMTS(this.options); |
---|
305 | } |
---|
306 | //get all additions from superclasses |
---|
307 | obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); |
---|
308 | // copy/set any non-init, non-simple values here |
---|
309 | return obj; |
---|
310 | }, |
---|
311 | |
---|
312 | /** |
---|
313 | * Method: getMatrix |
---|
314 | * Get the appropriate matrix definition for the current map resolution. |
---|
315 | */ |
---|
316 | getMatrix: function() { |
---|
317 | var matrix; |
---|
318 | if (!this.matrixIds || this.matrixIds.length === 0) { |
---|
319 | matrix = {identifier: this.map.getZoom() + this.zoomOffset}; |
---|
320 | } else { |
---|
321 | // get appropriate matrix given the map scale if possible |
---|
322 | if ("scaleDenominator" in this.matrixIds[0]) { |
---|
323 | // scale denominator calculation based on WMTS spec |
---|
324 | var denom = |
---|
325 | OpenLayers.METERS_PER_INCH * |
---|
326 | OpenLayers.INCHES_PER_UNIT[this.units] * |
---|
327 | this.map.getResolution() / 0.28E-3; |
---|
328 | var diff = Number.POSITIVE_INFINITY; |
---|
329 | var delta; |
---|
330 | for (var i=0, ii=this.matrixIds.length; i<ii; ++i) { |
---|
331 | delta = Math.abs(1 - (this.matrixIds[i].scaleDenominator / denom)); |
---|
332 | if (delta < diff) { |
---|
333 | diff = delta; |
---|
334 | matrix = this.matrixIds[i]; |
---|
335 | } |
---|
336 | } |
---|
337 | } else { |
---|
338 | // fall back on zoom as index |
---|
339 | matrix = this.matrixIds[this.map.getZoom() + this.zoomOffset]; |
---|
340 | } |
---|
341 | } |
---|
342 | return matrix; |
---|
343 | }, |
---|
344 | |
---|
345 | /** |
---|
346 | * Method: getTileInfo |
---|
347 | * Get tile information for a given location at the current map resolution. |
---|
348 | * |
---|
349 | * Parameters: |
---|
350 | * loc - {<OpenLayers.LonLat} A location in map coordinates. |
---|
351 | * |
---|
352 | * Returns: |
---|
353 | * {Object} An object with "col", "row", "i", and "j" properties. The col |
---|
354 | * and row values are zero based tile indexes from the top left. The |
---|
355 | * i and j values are the number of pixels to the left and top |
---|
356 | * (respectively) of the given location within the target tile. |
---|
357 | */ |
---|
358 | getTileInfo: function(loc) { |
---|
359 | var res = this.map.getResolution(); |
---|
360 | |
---|
361 | var fx = (loc.lon - this.tileOrigin.lon) / (res * this.tileSize.w); |
---|
362 | var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h); |
---|
363 | |
---|
364 | var col = Math.floor(fx); |
---|
365 | var row = Math.floor(fy); |
---|
366 | |
---|
367 | return { |
---|
368 | col: col, |
---|
369 | row: row, |
---|
370 | i: Math.floor((fx - col) * this.tileSize.w), |
---|
371 | j: Math.floor((fy - row) * this.tileSize.h) |
---|
372 | }; |
---|
373 | }, |
---|
374 | |
---|
375 | /** |
---|
376 | * Method: getURL |
---|
377 | * |
---|
378 | * Parameters: |
---|
379 | * bounds - {<OpenLayers.Bounds>} |
---|
380 | * |
---|
381 | * Returns: |
---|
382 | * {String} A URL for the tile corresponding to the given bounds. |
---|
383 | */ |
---|
384 | getURL: function(bounds) { |
---|
385 | bounds = this.adjustBounds(bounds); |
---|
386 | var url = ""; |
---|
387 | if (!this.tileFullExtent || this.tileFullExtent.intersectsBounds(bounds)) { |
---|
388 | |
---|
389 | var center = bounds.getCenterLonLat(); |
---|
390 | var info = this.getTileInfo(center); |
---|
391 | var matrixId = this.matrix.identifier; |
---|
392 | |
---|
393 | if (this.requestEncoding.toUpperCase() === "REST") { |
---|
394 | |
---|
395 | // include 'version', 'layer' and 'style' in tile resource url |
---|
396 | var path = this.version + "/" + this.layer + "/" + this.style + "/"; |
---|
397 | |
---|
398 | // append optional dimension path elements |
---|
399 | if (this.dimensions) { |
---|
400 | for (var i=0; i<this.dimensions.length; i++) { |
---|
401 | if (this.params[this.dimensions[i]]) { |
---|
402 | path = path + this.params[this.dimensions[i]] + "/"; |
---|
403 | } |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | // append other required path elements |
---|
408 | path = path + this.matrixSet + "/" + this.matrix.identifier + |
---|
409 | "/" + info.row + "/" + info.col + "." + this.formatSuffix; |
---|
410 | |
---|
411 | if (this.url instanceof Array) { |
---|
412 | url = this.selectUrl(path, this.url); |
---|
413 | } else { |
---|
414 | url = this.url; |
---|
415 | } |
---|
416 | if (!url.match(/\/$/)) { |
---|
417 | url = url + "/"; |
---|
418 | } |
---|
419 | url = url + path; |
---|
420 | |
---|
421 | } else if (this.requestEncoding.toUpperCase() === "KVP") { |
---|
422 | |
---|
423 | // assemble all required parameters |
---|
424 | var params = { |
---|
425 | SERVICE: "WMTS", |
---|
426 | REQUEST: "GetTile", |
---|
427 | VERSION: this.version, |
---|
428 | LAYER: this.layer, |
---|
429 | STYLE: this.style, |
---|
430 | TILEMATRIXSET: this.matrixSet, |
---|
431 | TILEMATRIX: this.matrix.identifier, |
---|
432 | TILEROW: info.row, |
---|
433 | TILECOL: info.col, |
---|
434 | FORMAT: this.format |
---|
435 | }; |
---|
436 | url = OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(this, [params]); |
---|
437 | |
---|
438 | } |
---|
439 | } |
---|
440 | return url; |
---|
441 | }, |
---|
442 | |
---|
443 | /** |
---|
444 | * APIMethod: mergeNewParams |
---|
445 | * Extend the existing layer <params> with new properties. Tiles will be |
---|
446 | * reloaded with updated params in the request. |
---|
447 | * |
---|
448 | * Parameters: |
---|
449 | * newParams - {Object} Properties to extend to existing <params>. |
---|
450 | */ |
---|
451 | mergeNewParams: function(newParams) { |
---|
452 | if (this.requestEncoding.toUpperCase() === "KVP") { |
---|
453 | return OpenLayers.Layer.Grid.prototype.mergeNewParams.apply( |
---|
454 | this, [OpenLayers.Util.upperCaseObject(newParams)] |
---|
455 | ); |
---|
456 | } |
---|
457 | }, |
---|
458 | |
---|
459 | /** |
---|
460 | * Method: addTile |
---|
461 | * Create a tile, initialize it, and add it to the layer div. |
---|
462 | * |
---|
463 | * Parameters: |
---|
464 | * bounds - {<OpenLayers.Bounds>} |
---|
465 | * position - {<OpenLayers.Pixel>} |
---|
466 | * |
---|
467 | * Returns: |
---|
468 | * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image |
---|
469 | */ |
---|
470 | addTile: function(bounds,position) { |
---|
471 | return new OpenLayers.Tile.Image(this, position, bounds, |
---|
472 | null, this.tileSize); |
---|
473 | }, |
---|
474 | |
---|
475 | CLASS_NAME: "OpenLayers.Layer.WMTS" |
---|
476 | }); |
---|