From 13d0b36a08c03b7d925cc5be9d2737d50601625e Mon Sep 17 00:00:00 2001 From: Bruno Binet Date: Thu, 7 Mar 2013 22:22:36 +0100 Subject: [PATCH] Add a ol.source.WMTS.createFromCapabilities function so that we can create a wmts source directly from a getCapabilities response (parsed by the wmts parser) --- src/ol/source/wmtssource.exports | 1 + src/ol/source/wmtssource.js | 130 +++++++++++++++++++++++++++++++ src/ol/tilegrid/wmtstilegrid.js | 30 +++++++ 3 files changed, 161 insertions(+) diff --git a/src/ol/source/wmtssource.exports b/src/ol/source/wmtssource.exports index 13ed62b207..be270d4103 100644 --- a/src/ol/source/wmtssource.exports +++ b/src/ol/source/wmtssource.exports @@ -1 +1,2 @@ @exportClass ol.source.WMTS ol.source.WMTSOptions +@exportSymbol ol.source.WMTS.createFromCapabilities diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index 657a9d65e2..2f14199557 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -166,3 +166,133 @@ ol.source.WMTS = function(wmtsOptions) { }; goog.inherits(ol.source.WMTS, ol.source.ImageTileSource); + + +/** + * @param {Object} wmtsCap An object representing the capabilities document. + * @param {string} layer The layer identifier that we want the grid for. + * @param {string|null=} opt_crossOrigin Optional crossOrigin value. + * @param {string=} opt_matrixSet Optional tileMatrixSet name. If not provided, + * it defaults to the first matrixSet found. + * @param {ol.source.WMTSRequestEncoding=} opt_requestEncoding Optional + * requestEncoding. If not provided, it defaults to the first value found. + * @param {string=} opt_format Optional format name. If not provided, it + * defaults to the first format found. + * @param {string=} opt_style Optional style name. If not provided, it defaults + * to the first style found. + * @param {Object=} opt_dimensions Optional object for setting dimensions + * values. + * @param {ol.Extent=} opt_extent Optional extent. + * @return {ol.source.WMTS} TileGrid instance. + */ +ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer, + opt_crossOrigin, opt_matrixSet, opt_requestEncoding, opt_format, + opt_style, opt_dimensions, opt_extent) { + + // TODO: add support for TileMatrixLimits + + var layers = wmtsCap['contents']['layers']; + var matrixSet = opt_matrixSet; + var l = goog.array.find(layers, function(elt, index, array) { + return elt['identifier'] == layer; + }); + goog.asserts.assert(!goog.isNull(l)); + + var format = goog.isDef(opt_format) ? + opt_format : /** @type {string} */ (l['formats'][0]); + var idx = goog.array.findIndex(l['styles'], function(elt, index, array) { + if (goog.isDef(opt_style)) { + return elt['identifier'] == opt_style; + } else { + return elt['isDefault']; + } + }); + if (idx < 0) { + goog.asserts.assert(!goog.isDef(opt_style)); // opt_style was not correct + idx = 0; + } + var style = /** @type {string} */ (l['styles'][idx]['identifier']); + + var dimensions = {}; + goog.array.forEach(l['dimensions'], function(elt, index, array) { + var key = elt['identifier']; + // With the special value 'default', the server should automatically + // select the default value for this dimension. + var value = (goog.isDef(opt_dimensions) && + opt_dimensions.hasOwnProperty(key)) ? + opt_dimensions[key] : elt['default']; + if (goog.isDef(value)) { + goog.asserts.assert(goog.array.indexOf(elt['values'], value) >= 0); + } else { + value = elt['values'][0]; + } + goog.asserts.assert(goog.isDef(value)); + dimensions[key] = value; + }); + + if (goog.isDef(matrixSet)) { + goog.asserts.assert(0 < goog.array.findIndex( + l['tileMatrixSetLinks'], function(elt, index, arr) { + return elt['tileMatrixSet'] == matrixSet; + })); + } else { + goog.asserts.assert(l['tileMatrixSetLinks'].length > 0); + matrixSet = /** @type {string} */ + (l['tileMatrixSetLinks'][0]['tileMatrixSet']); + } + var matrixSets = wmtsCap['contents']['tileMatrixSets']; + goog.asserts.assert(matrixSet in matrixSets); + var matrixSetObj = matrixSets[matrixSet]; + + var tileGrid = ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet( + matrixSetObj); + + var projection = ol.projection.get(matrixSetObj['supportedCRS']); + + var gets = wmtsCap['operationsMetadata']['GetTile']['dcp']['http']['get']; + var encodings = goog.object.getKeys( + gets[0]['constraints']['GetEncoding']['allowedValues']); + goog.asserts.assert(encodings.length > 0); + var requestEncoding = goog.isDef(opt_requestEncoding) ? + opt_requestEncoding : /** @type {ol.source.WMTSRequestEncoding} */ + (encodings[0]); + // TODO: arcgis support, quote from ol2: + // The OGC documentation is not clear if we should use REST or RESTful, + // ArcGis use RESTful, and OpenLayers use REST. + + var urls; + switch (requestEncoding) { + case ol.source.WMTSRequestEncoding.REST: + goog.asserts.assert(l['resourceUrls'].hasOwnProperty('tile')); + goog.asserts.assert(l['resourceUrls']['tile'].hasOwnProperty(format)); + urls = /** @type {Array.} */ + (l['resourceUrls']['tile'][format]); + break; + case ol.source.WMTSRequestEncoding.KVP: + urls = []; + goog.array.forEach(gets, function(elt, index, array) { + if (elt['constraints']['GetEncoding']['allowedValues'].hasOwnProperty( + ol.source.WMTSRequestEncoding.KVP)) { + urls.push(/** @type {string} */ (elt['url'])); + } + }); + goog.asserts.assert(urls.length > 0); + break; + default: + goog.asserts.assert(false); + } + + return new ol.source.WMTS({ + urls: urls, + layer: layer, + matrixSet: matrixSet, + format: format, + projection: projection, + requestEncoding: requestEncoding, + tileGrid: tileGrid, + style: style, + crossOrigin: opt_crossOrigin, + extent: opt_extent, + dimensions: dimensions + }); +}; diff --git a/src/ol/tilegrid/wmtstilegrid.js b/src/ol/tilegrid/wmtstilegrid.js index 81f2c01adf..d7a3f36131 100644 --- a/src/ol/tilegrid/wmtstilegrid.js +++ b/src/ol/tilegrid/wmtstilegrid.js @@ -51,3 +51,33 @@ ol.tilegrid.WMTS.prototype.getMatrixId = function(z) { ol.tilegrid.WMTS.prototype.getMatrixIds = function() { return this.matrixIds_; }; + + +/** + * @param {Object} matrixSet An object representing a matrixSet in the + * capabilities document. + * @return {ol.tilegrid.WMTS} WMTS tileGrid instance. + */ +ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet = + function(matrixSet) { + + var resolutions = []; + var matrixIds = []; + var origins = []; + var tileSizes = []; + var projection = ol.projection.get(matrixSet['supportedCRS']); + var meters_per_unit = projection.getMetersPerUnit(); + goog.array.forEach(matrixSet['matrixIds'], function(elt, index, array) { + matrixIds.push(elt['identifier']); + origins.push(elt['topLeftCorner']); + resolutions.push(elt['scaleDenominator'] * 0.28E-3 / meters_per_unit); + tileSizes.push(new ol.Size(elt['tileWidth'], elt['tileHeight'])); + }); + + return new ol.tilegrid.WMTS({ + origins: origins, + resolutions: resolutions, + matrixIds: matrixIds, + tileSizes: tileSizes + }); +};