From bd0140f42d74f8a40fdaa3cffa04b1f6af72bbea Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 24 May 2016 13:30:36 +0200 Subject: [PATCH] Fix the requestEncoding automatic selection If the `requestEncoding` parameter was not provided, the function used `KVP` but without checking if it was supported. --- examples/wmts-hidpi.js | 1 - src/ol/source/wmtssource.js | 45 +++++++++++++++----------- test/spec/ol/source/wmtssource.test.js | 18 +++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/examples/wmts-hidpi.js b/examples/wmts-hidpi.js index 6b7a4e72e6..386b8ad609 100644 --- a/examples/wmts-hidpi.js +++ b/examples/wmts-hidpi.js @@ -30,7 +30,6 @@ fetch(capabilitiesUrl).then(function(response) { var options = ol.source.WMTS.optionsFromCapabilities(result, { layer: layer, matrixSet: 'google3857', - requestEncoding: 'REST', style: 'normal' }); options.tilePixelRatio = tilePixelRatio; diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index d247d4615f..1231c508b1 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -428,32 +428,39 @@ ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, config) { 'requestEncoding (%s) is one of "REST", "RESTful", "KVP" or ""', requestEncoding); - if (!wmtsCap.hasOwnProperty('OperationsMetadata') || - !wmtsCap['OperationsMetadata'].hasOwnProperty('GetTile') || - requestEncoding.indexOf('REST') === 0) { - // Add REST tile resource url - requestEncoding = ol.source.WMTSRequestEncoding.REST; - l['ResourceURL'].forEach(function(elt, index, array) { - if (elt['resourceType'] == 'tile') { - format = elt['format']; - urls.push(/** @type {string} */ (elt['template'])); - } - }); - } else { + if ('OperationsMetadata' in wmtsCap && 'GetTile' in wmtsCap['OperationsMetadata']) { var gets = wmtsCap['OperationsMetadata']['GetTile']['DCP']['HTTP']['Get']; + goog.asserts.assert(gets.length >= 1); for (var i = 0, ii = gets.length; i < ii; ++i) { - var constraint = ol.array.find(gets[i]['Constraint'], - function(elt, index, array) { - return elt['name'] == 'GetEncoding'; - }); + var constraint = ol.array.find(gets[i]['Constraint'], function(element) { + return element['name'] == 'GetEncoding'; + }); var encodings = constraint['AllowedValues']['Value']; - if (encodings.length > 0 && ol.array.includes(encodings, 'KVP')) { - requestEncoding = ol.source.WMTSRequestEncoding.KVP; - urls.push(/** @type {string} */ (gets[i]['href'])); + goog.asserts.assert(encodings.length >= 1); + + if (requestEncoding === '') { + // requestEncoding not provided, use the first encoding from the list + requestEncoding = encodings[0]; + } + if (requestEncoding === ol.source.WMTSRequestEncoding.KVP) { + if (ol.array.includes(encodings, ol.source.WMTSRequestEncoding.KVP)) { + urls.push(/** @type {string} */ (gets[i]['href'])); + } + } else { + break; } } } + if (urls.length === 0) { + requestEncoding = ol.source.WMTSRequestEncoding.REST; + l['ResourceURL'].forEach(function(element) { + if (element['resourceType'] === 'tile') { + format = element['format']; + urls.push(/** @type {string} */ (element['template'])); + } + }); + } goog.asserts.assert(urls.length > 0, 'At least one URL found'); return { diff --git a/test/spec/ol/source/wmtssource.test.js b/test/spec/ol/source/wmtssource.test.js index 21d1c307be..e594fb992c 100644 --- a/test/spec/ol/source/wmtssource.test.js +++ b/test/spec/ol/source/wmtssource.test.js @@ -162,6 +162,7 @@ describe('ol.source.WMTS', function() { var options = ol.source.WMTS.optionsFromCapabilities( capabilities, { layer: 'Demographics_USA_Population_Density', + requestEncoding: 'KVP', matrixSet: 'default028mm' }); @@ -171,6 +172,23 @@ describe('ol.source.WMTS', function() { 'http://services.arcgisonline.com/arcgis/rest/services/' + 'Demographics/USA_Population_Density/MapServer/WMTS?'); }); + + it('can create REST options from spec/ol/format/wmts/arcgis.xml', + function() { + var options = ol.source.WMTS.optionsFromCapabilities( + capabilities, { + layer: 'Demographics_USA_Population_Density', + matrixSet: 'default028mm' + }); + + expect(options.urls).to.be.an('array'); + expect(options.urls).to.have.length(1); + expect(options.urls[0]).to.be.eql( + 'http://services.arcgisonline.com/arcgis/rest/services/' + + 'Demographics/USA_Population_Density/MapServer/WMTS/' + + 'tile/1.0.0/Demographics_USA_Population_Density/' + + '{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png'); + }); }); describe('#getUrls', function() {