From c7cb26a066071d4e72b34efad04a416bffde5f40 Mon Sep 17 00:00:00 2001 From: Alexander Schenkel Date: Wed, 14 Sep 2016 13:28:08 +0200 Subject: [PATCH 1/3] Fixes setUrl() for WMTS sources (ol.source.WMTS) See Issue #5881: setUrl was inherited from ol.source UrlTile, which creates a wrong tileUrlFunction: The official WMTS URL Template variables were no longer replaced. --- src/ol/source/wmts.js | 16 ++++++++ test/spec/ol/source/wmts.test.js | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index 7ec74c36a4..ab699e04ed 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -148,6 +148,9 @@ ol.source.WMTS = function(options) { }); } + // Store it as accessible function to be used later on: + this.createFromWMTSTemplate = createFromWMTSTemplate; + var tileUrlFunction = (urls && urls.length > 0) ? ol.TileUrlFunction.createFromTileUrlFunctions( urls.map(createFromWMTSTemplate)) : @@ -175,6 +178,19 @@ ol.source.WMTS = function(options) { }; ol.inherits(ol.source.WMTS, ol.source.TileImage); +/** + * Set the URLs to use for requests. + * URLs may contain OCG conform URL Template Variables: {TileMatrix}, {TileRow}, {TileCol}. + * @param {Array.} urls URLs. + * @api stable + */ +ol.source.WMTS.prototype.setUrls = function(urls) { + this.urls = urls; + var key = urls.join('\n'); + this.setTileUrlFunction(this.fixedTileUrlFunction ? + this.fixedTileUrlFunction.bind(this) : + ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(this.createFromWMTSTemplate.bind(this))), key); +}; /** * Get the dimensions, i.e. those passed to the constructor through the diff --git a/test/spec/ol/source/wmts.test.js b/test/spec/ol/source/wmts.test.js index e5aeba8ef4..68155bb733 100644 --- a/test/spec/ol/source/wmts.test.js +++ b/test/spec/ol/source/wmts.test.js @@ -230,6 +230,70 @@ describe('ol.source.WMTS', function() { }); }); + describe('#setUrls()', function() { + it('sets the URL for the source', function() { + var source = new ol.source.WMTS({}); + + var urls = [ + 'https://a.example.com/', + 'https://b.example.com/', + 'https://c.example.com/' + ]; + source.setUrls(urls); + + expect(source.getUrls()).to.eql(urls); + }); + + it('updates the key for the source', function() { + var source = new ol.source.WMTS({}); + + var urls = [ + 'https://a.example.com/', + 'https://b.example.com/', + 'https://c.example.com/' + ]; + source.setUrls(urls); + + expect(source.getKey()).to.eql(urls.join('\n')); + }); + + it('generates the correct tileUrlFunction during application of setUrl()', function() { + var projection = ol.proj.get('EPSG:3857'); + var source = new ol.source.WMTS({ + projection: projection, + requestEncoding: 'REST', + urls: [ + 'http://1.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpeg', + 'http://2.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpeg' + ], + tileGrid: new ol.tilegrid.WMTS({ + matrixIds: [0,1,2,3,4,5,6,7], + origin: [2690000, 1285000], + resolutions: [4000, 3750, 3500, 3250, 3000, 2750, 2500, 2250] + }) + }); + + var urls = [ + 'https://a.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpg', + 'https://b.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpg' + ]; + source.setUrls(urls); + var tileUrl1 = source.tileUrlFunction([2,9,4],1,projection); + expect(tileUrl1).to.match(/https\:\/\/[ab]\.example\.com\/2\/-5\/9\.jpg/); + }); + }); + + describe('url option', function() { + it('expands url template', function() { + var tileSource = new ol.source.WMTS({ + url: '{1-3}' + }); + + var urls = tileSource.getUrls(); + expect(urls).to.eql(['1', '2', '3']); + }); + }); + describe('#getUrls', function() { var sourceOptions; From 9c8c9c1a17ca200c074dde96e523f94f1d3229db Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Mon, 4 Dec 2017 09:09:57 +0100 Subject: [PATCH 2/3] Store createFromWMTSTemplate into a private variable --- src/ol/source/wmts.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index ab699e04ed..1513f5f34a 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -105,8 +105,9 @@ ol.source.WMTS = function(options) { /** * @param {string} template Template. * @return {ol.TileUrlFunctionType} Tile URL function. + * @private */ - function createFromWMTSTemplate(template) { + this.createFromWMTSTemplate_ = function(template) { // TODO: we may want to create our own appendParams function so that params // order conforms to wmts spec guidance, and so that we can avoid to escape @@ -148,12 +149,9 @@ ol.source.WMTS = function(options) { }); } - // Store it as accessible function to be used later on: - this.createFromWMTSTemplate = createFromWMTSTemplate; - var tileUrlFunction = (urls && urls.length > 0) ? ol.TileUrlFunction.createFromTileUrlFunctions( - urls.map(createFromWMTSTemplate)) : + urls.map(this.createFromWMTSTemplate_)) : ol.TileUrlFunction.nullTileUrlFunction; ol.source.TileImage.call(this, { @@ -181,15 +179,14 @@ ol.inherits(ol.source.WMTS, ol.source.TileImage); /** * Set the URLs to use for requests. * URLs may contain OCG conform URL Template Variables: {TileMatrix}, {TileRow}, {TileCol}. - * @param {Array.} urls URLs. - * @api stable + * @override */ ol.source.WMTS.prototype.setUrls = function(urls) { this.urls = urls; var key = urls.join('\n'); this.setTileUrlFunction(this.fixedTileUrlFunction ? this.fixedTileUrlFunction.bind(this) : - ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(this.createFromWMTSTemplate.bind(this))), key); + ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(this.createFromWMTSTemplate_.bind(this))), key); }; /** From 14e3fd921470e2dfdbb40a0b36149a7d3008ff33 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Mon, 4 Dec 2017 10:06:00 +0100 Subject: [PATCH 3/3] Fix eslint errors --- src/ol/source/wmts.js | 6 +++--- test/spec/ol/source/wmts.test.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ol/source/wmts.js b/src/ol/source/wmts.js index 1513f5f34a..2f7b8cc4a2 100644 --- a/src/ol/source/wmts.js +++ b/src/ol/source/wmts.js @@ -147,7 +147,7 @@ ol.source.WMTS = function(options) { return url; } }); - } + }; var tileUrlFunction = (urls && urls.length > 0) ? ol.TileUrlFunction.createFromTileUrlFunctions( @@ -185,8 +185,8 @@ ol.source.WMTS.prototype.setUrls = function(urls) { this.urls = urls; var key = urls.join('\n'); this.setTileUrlFunction(this.fixedTileUrlFunction ? - this.fixedTileUrlFunction.bind(this) : - ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(this.createFromWMTSTemplate_.bind(this))), key); + this.fixedTileUrlFunction.bind(this) : + ol.TileUrlFunction.createFromTileUrlFunctions(urls.map(this.createFromWMTSTemplate_.bind(this))), key); }; /** diff --git a/test/spec/ol/source/wmts.test.js b/test/spec/ol/source/wmts.test.js index 68155bb733..1778705a20 100644 --- a/test/spec/ol/source/wmts.test.js +++ b/test/spec/ol/source/wmts.test.js @@ -267,7 +267,7 @@ describe('ol.source.WMTS', function() { 'http://2.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpeg' ], tileGrid: new ol.tilegrid.WMTS({ - matrixIds: [0,1,2,3,4,5,6,7], + matrixIds: [0, 1, 2, 3, 4, 5, 6, 7], origin: [2690000, 1285000], resolutions: [4000, 3750, 3500, 3250, 3000, 2750, 2500, 2250] }) @@ -278,7 +278,7 @@ describe('ol.source.WMTS', function() { 'https://b.example.com/{TileMatrix}/{TileRow}/{TileCol}.jpg' ]; source.setUrls(urls); - var tileUrl1 = source.tileUrlFunction([2,9,4],1,projection); + var tileUrl1 = source.tileUrlFunction([2, 9, 4], 1, projection); expect(tileUrl1).to.match(/https\:\/\/[ab]\.example\.com\/2\/-5\/9\.jpg/); }); });