Return null if the layer was not found in the WMTS capabilities

This commit is contained in:
Frederic Junod
2017-03-08 15:03:48 +01:00
parent cc13ca68fa
commit 2e22fce718
5 changed files with 23 additions and 8 deletions
+5 -3
View File
@@ -99,13 +99,15 @@ fetch(url).then(function(response) {
return response.text(); return response.text();
}).then(function(text) { }).then(function(text) {
var result = parser.read(text); var result = parser.read(text);
var options = ol.source.WMTS.optionsFromCapabilities(result, var options = ol.source.WMTS.optionsFromCapabilities(result, {
{layer: 'OSM_Land_Mask', matrixSet: 'EPSG3413_250m'}); layer: 'OSM_Land_Mask',
matrixSet: 'EPSG3413_250m'
});
options.crossOrigin = ''; options.crossOrigin = '';
options.projection = 'EPSG:3413'; options.projection = 'EPSG:3413';
options.wrapX = false; options.wrapX = false;
layers['wmts3413'] = new ol.layer.Tile({ layers['wmts3413'] = new ol.layer.Tile({
source: new ol.source.WMTS(options) source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options))
}); });
}); });
+1 -1
View File
@@ -34,6 +34,6 @@ fetch(capabilitiesUrl).then(function(response) {
}); });
options.tilePixelRatio = tilePixelRatio; options.tilePixelRatio = tilePixelRatio;
map.addLayer(new ol.layer.Tile({ map.addLayer(new ol.layer.Tile({
source: new ol.source.WMTS(options) source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options))
})); }));
}); });
+5 -3
View File
@@ -12,8 +12,10 @@ fetch('data/WMTSCapabilities.xml').then(function(response) {
return response.text(); return response.text();
}).then(function(text) { }).then(function(text) {
var result = parser.read(text); var result = parser.read(text);
var options = ol.source.WMTS.optionsFromCapabilities(result, var options = ol.source.WMTS.optionsFromCapabilities(result, {
{layer: 'layer-7328', matrixSet: 'EPSG:3857'}); layer: 'layer-7328',
matrixSet: 'EPSG:3857'
});
map = new ol.Map({ map = new ol.Map({
layers: [ layers: [
@@ -23,7 +25,7 @@ fetch('data/WMTSCapabilities.xml').then(function(response) {
}), }),
new ol.layer.Tile({ new ol.layer.Tile({
opacity: 1, opacity: 1,
source: new ol.source.WMTS(options) source: new ol.source.WMTS(/** @type {!olx.source.WMTSOptions} */ (options))
}) })
], ],
target: 'map', target: 'map',
+4 -1
View File
@@ -292,7 +292,7 @@ ol.source.WMTS.prototype.updateDimensions = function(dimensions) {
* - style - {string} The name of the style * - style - {string} The name of the style
* - format - {string} Image format for the layer. Default is the first * - format - {string} Image format for the layer. Default is the first
* format returned in the GetCapabilities response. * 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 * @api
*/ */
ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { 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) { var l = ol.array.find(layers, function(elt, index, array) {
return elt['Identifier'] == config['layer']; return elt['Identifier'] == config['layer'];
}); });
if (l === null) {
return null;
}
var tileMatrixSets = wmtsCap['Contents']['TileMatrixSet']; var tileMatrixSets = wmtsCap['Contents']['TileMatrixSet'];
var idx, matrixSet, matrixLimits; var idx, matrixSet, matrixLimits;
if (l['TileMatrixSetLink'].length > 1) { if (l['TileMatrixSetLink'].length > 1) {
+8
View File
@@ -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', it('can create KVP options from spec/ol/format/wmts/ogcsample.xml',
function() { function() {
var options = ol.source.WMTS.optionsFromCapabilities( var options = ol.source.WMTS.optionsFromCapabilities(