From 2a2fea379ea88a37c8b0c52c186574b10d79b3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 22 Apr 2015 12:16:15 +0200 Subject: [PATCH] Add ol.source.WMTS#getUrls --- src/ol/source/wmtssource.js | 34 ++++++++++++++----- src/ol/tileurlfunction.js | 3 ++ test/spec/ol/source/wmtssource.test.js | 47 ++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index 9e2b982df9..13ee9b4a0a 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -83,6 +83,17 @@ ol.source.WMTS = function(options) { */ this.style_ = options.style; + var urls = options.urls; + if (!goog.isDef(urls) && goog.isDef(options.url)) { + urls = ol.TileUrlFunction.expandUrl(options.url); + } + + /** + * @private + * @type {!Array.} + */ + this.urls_ = goog.isDefAndNotNull(urls) ? urls : []; + // FIXME: should we guess this requestEncoding from options.url(s) // structure? that would mean KVP only if a template is not provided. var requestEncoding = goog.isDef(options.requestEncoding) ? @@ -158,15 +169,10 @@ ol.source.WMTS = function(options) { }); } - var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction; - var urls = options.urls; - if (!goog.isDef(urls) && goog.isDef(options.url)) { - urls = ol.TileUrlFunction.expandUrl(options.url); - } - if (goog.isDef(urls)) { - tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions( - goog.array.map(urls, createFromWMTSTemplate)); - } + var tileUrlFunction = this.urls_.length > 0 ? + ol.TileUrlFunction.createFromTileUrlFunctions( + goog.array.map(this.urls_, createFromWMTSTemplate)) : + ol.TileUrlFunction.nullTileUrlFunction; var tmpExtent = ol.extent.createEmpty(); tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform( @@ -272,6 +278,16 @@ ol.source.WMTS.prototype.getStyle = function() { }; +/** + * Return the URLs used for this WMTS source. + * @return {!Array.} URLs. + * @api + */ +ol.source.WMTS.prototype.getUrls = function() { + return this.urls_; +}; + + /** * Return the version of the WMTS source. * @return {string} Version. diff --git a/src/ol/tileurlfunction.js b/src/ol/tileurlfunction.js index 00b4e822b4..28382f33fa 100644 --- a/src/ol/tileurlfunction.js +++ b/src/ol/tileurlfunction.js @@ -2,6 +2,7 @@ goog.provide('ol.TileUrlFunction'); goog.provide('ol.TileUrlFunctionType'); goog.require('goog.array'); +goog.require('goog.asserts'); goog.require('goog.math'); goog.require('ol.TileCoord'); goog.require('ol.tilecoord'); @@ -74,6 +75,8 @@ ol.TileUrlFunction.createFromTemplates = function(templates) { * @return {ol.TileUrlFunctionType} Tile URL function. */ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) { + goog.asserts.assert(tileUrlFunctions.length > 0, + 'Length of tile url functions should be greater than 0'); if (tileUrlFunctions.length === 1) { return tileUrlFunctions[0]; } diff --git a/test/spec/ol/source/wmtssource.test.js b/test/spec/ol/source/wmtssource.test.js index 41e7c57299..ca9ffb49f9 100644 --- a/test/spec/ol/source/wmtssource.test.js +++ b/test/spec/ol/source/wmtssource.test.js @@ -162,6 +162,53 @@ describe('ol.source.WMTS', function() { 'Demographics/USA_Population_Density/MapServer/WMTS?'); }); }); + + describe('#getUrls', function() { + + var sourceOptions; + var source; + + beforeEach(function() { + sourceOptions = { + layer: 'layer', + style: 'default', + matrixSet: 'foo', + requestEncoding: 'REST', + tileGrid: new ol.tilegrid.WMTS({ + origin: [0, 0], + resolutions: [], + matrixIds: [] + }) + }; + }); + + describe('using a "url" option', function() { + beforeEach(function() { + sourceOptions.url = 'some_wmts_url'; + source = new ol.source.WMTS(sourceOptions); + }); + + it('returns the WMTS URLs', function() { + var urls = source.getUrls(); + expect(urls).to.be.eql(['some_wmts_url']); + }); + + }); + + describe('using a "urls" option', function() { + beforeEach(function() { + sourceOptions.urls = ['some_wmts_url1', 'some_wmts_url2']; + source = new ol.source.WMTS(sourceOptions); + }); + + it('returns the WMTS URLs', function() { + var urls = source.getUrls(); + expect(urls).to.be.eql(['some_wmts_url1', 'some_wmts_url2']); + }); + + }); + + }); }); goog.require('ol.format.WMTSCapabilities');