From 2e22fce718a468f4f4939d562be42e1631522787 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 8 Mar 2017 15:03:48 +0100 Subject: [PATCH] Return null if the layer was not found in the WMTS capabilities --- examples/reprojection.js | 8 +++++--- examples/wmts-hidpi.js | 2 +- examples/wmts-layer-from-capabilities.js | 8 +++++--- src/ol/source/wmts.js | 5 ++++- test/spec/ol/source/wmts.test.js | 8 ++++++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/examples/reprojection.js b/examples/reprojection.js index 51c8c6592f..115215a639 100644 --- a/examples/reprojection.js +++ b/examples/reprojection.js @@ -99,13 +99,15 @@ fetch(url).then(function(response) { return response.text(); }).then(function(text) { var result = parser.read(text); - var options = ol.source.WMTS.optionsFromCapabilities(result, - {layer: 'OSM_Land_Mask', matrixSet: 'EPSG3413_250m'}); + var options = ol.source.WMTS.optionsFromCapabilities(result, { + layer: 'OSM_Land_Mask', + matrixSet: 'EPSG3413_250m' + }); options.crossOrigin = ''; options.projection = 'EPSG:3413'; options.wrapX = false; layers['wmts3413'] = new ol.layer.Tile({ - source: new ol.source.WMTS(options) + source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options)) }); }); diff --git a/examples/wmts-hidpi.js b/examples/wmts-hidpi.js index 747b6f634c..e5dbf7a625 100644 --- a/examples/wmts-hidpi.js +++ b/examples/wmts-hidpi.js @@ -34,6 +34,6 @@ fetch(capabilitiesUrl).then(function(response) { }); options.tilePixelRatio = tilePixelRatio; map.addLayer(new ol.layer.Tile({ - source: new ol.source.WMTS(options) + source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options)) })); }); diff --git a/examples/wmts-layer-from-capabilities.js b/examples/wmts-layer-from-capabilities.js index 3a1852f0e2..45badb4c89 100644 --- a/examples/wmts-layer-from-capabilities.js +++ b/examples/wmts-layer-from-capabilities.js @@ -12,8 +12,10 @@ fetch('data/WMTSCapabilities.xml').then(function(response) { return response.text(); }).then(function(text) { var result = parser.read(text); - var options = ol.source.WMTS.optionsFromCapabilities(result, - {layer: 'layer-7328', matrixSet: 'EPSG:3857'}); + var options = ol.source.WMTS.optionsFromCapabilities(result, { + layer: 'layer-7328', + matrixSet: 'EPSG:3857' + }); map = new ol.Map({ layers: [ @@ -23,7 +25,7 @@ fetch('data/WMTSCapabilities.xml').then(function(response) { }), new ol.layer.Tile({ opacity: 1, - source: new ol.source.WMTS(options) + source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options)) }) ], target: 'map', diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index 61ce683832..684f9195d2 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -292,7 +292,7 @@ ol.source.WMTS.prototype.updateDimensions = function(dimensions) { * - style - {string} The name of the style * - format - {string} Image format for the layer. Default is the first * format returned in the GetCapabilities response. - * @return {olx.source.WMTSOptions} WMTS source options object. + * @return {?olx.source.WMTSOptions} WMTS source options object or `null` if the layer was not found. * @api */ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { @@ -300,6 +300,9 @@ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { var l = ol.array.find(layers, function(elt, index, array) { return elt['Identifier'] == config['layer']; }); + if (l === null) { + return null; + } var tileMatrixSets = wmtsCap['Contents']['TileMatrixSet']; var idx, matrixSet, matrixLimits; if (l['TileMatrixSetLink'].length > 1) { diff --git a/test/spec/ol/source/wmts.test.js b/test/spec/ol/source/wmts.test.js index dbe38829d7..c539fd23f0 100644 --- a/test/spec/ol/source/wmts.test.js +++ b/test/spec/ol/source/wmts.test.js @@ -23,6 +23,14 @@ describe('ol.source.WMTS', function() { }); }); + it('returns null if the layer was not found in the capabilities', function() { + var options = ol.source.WMTS.optionsFromCapabilities(capabilities, { + layer: 'invalid' + }); + + expect(options).to.be(null); + }); + it('can create KVP options from spec/ol/format/wmts/ogcsample.xml', function() { var options = ol.source.WMTS.optionsFromCapabilities(