Merge pull request #3612 from elemoine/wmtsgeturls

Add ol.source.WMTS#getUrls and getRequestEncoding
This commit is contained in:
Éric Lemoine
2015-04-28 17:39:12 +02:00
3 changed files with 119 additions and 10 deletions

View File

@@ -83,12 +83,30 @@ 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.<string>}
*/
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) ?
/**
* @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;
@@ -158,15 +176,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(
@@ -262,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.
@@ -272,6 +295,16 @@ ol.source.WMTS.prototype.getStyle = function() {
};
/**
* Return the URLs used for this WMTS source.
* @return {!Array.<string>} URLs.
* @api
*/
ol.source.WMTS.prototype.getUrls = function() {
return this.urls_;
};
/**
* Return the version of the WMTS source.
* @return {string} Version.

View File

@@ -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];
}

View File

@@ -162,6 +162,79 @@ 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']);
});
});
});
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');