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 1/2] 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'); From 3a03e9f76a083c7a2e888d519bbded8b77e99280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 22 Apr 2015 15:11:30 +0200 Subject: [PATCH 2/2] Add ol.source.WMTS#getRequestEncoding --- src/ol/source/wmtssource.js | 19 ++++++++++++++++++- test/spec/ol/source/wmtssource.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index 13ee9b4a0a..53eb1f3bcb 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -96,10 +96,17 @@ ol.source.WMTS = function(options) { // 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) ? + + /** + * @private + * @type {ol.source.WMTSRequestEncoding} + */ + this.requestEncoding_ = goog.isDef(options.requestEncoding) ? /** @type {ol.source.WMTSRequestEncoding} */ (options.requestEncoding) : ol.source.WMTSRequestEncoding.KVP; + var requestEncoding = this.requestEncoding_; + // FIXME: should we create a default tileGrid? // we could issue a getCapabilities xhr to retrieve missing configuration var tileGrid = options.tileGrid; @@ -268,6 +275,16 @@ ol.source.WMTS.prototype.getMatrixSet = function() { }; +/** + * Return the request encoding, either "KVP" or "REST". + * @return {ol.source.WMTSRequestEncoding} Request encoding. + * @api + */ +ol.source.WMTS.prototype.getRequestEncoding = function() { + return this.requestEncoding_; +}; + + /** * Return the style of the WMTS source. * @return {string} Style. diff --git a/test/spec/ol/source/wmtssource.test.js b/test/spec/ol/source/wmtssource.test.js index ca9ffb49f9..2943040969 100644 --- a/test/spec/ol/source/wmtssource.test.js +++ b/test/spec/ol/source/wmtssource.test.js @@ -209,6 +209,32 @@ describe('ol.source.WMTS', function() { }); }); + + describe('#getRequestEncoding', function() { + + var source; + + beforeEach(function() { + source = new ol.source.WMTS({ + layer: 'layer', + style: 'default', + matrixSet: 'foo', + requestEncoding: 'REST', + tileGrid: new ol.tilegrid.WMTS({ + origin: [0, 0], + resolutions: [], + matrixIds: [] + }) + }); + }); + + it('returns the request encoding', function() { + var requestEncoding = source.getRequestEncoding(); + expect(requestEncoding).to.be.eql('REST'); + }); + + }); + }); goog.require('ol.format.WMTSCapabilities');