Add a ol.source.WMTS.createFromCapabilities function

so that we can create a wmts source directly from a getCapabilities response
(parsed by the wmts parser)
This commit is contained in:
Bruno Binet
2013-03-07 22:22:36 +01:00
parent dc440a6604
commit 13d0b36a08
3 changed files with 161 additions and 0 deletions

View File

@@ -166,3 +166,133 @@ ol.source.WMTS = function(wmtsOptions) {
};
goog.inherits(ol.source.WMTS, ol.source.ImageTileSource);
/**
* @param {Object} wmtsCap An object representing the capabilities document.
* @param {string} layer The layer identifier that we want the grid for.
* @param {string|null=} opt_crossOrigin Optional crossOrigin value.
* @param {string=} opt_matrixSet Optional tileMatrixSet name. If not provided,
* it defaults to the first matrixSet found.
* @param {ol.source.WMTSRequestEncoding=} opt_requestEncoding Optional
* requestEncoding. If not provided, it defaults to the first value found.
* @param {string=} opt_format Optional format name. If not provided, it
* defaults to the first format found.
* @param {string=} opt_style Optional style name. If not provided, it defaults
* to the first style found.
* @param {Object=} opt_dimensions Optional object for setting dimensions
* values.
* @param {ol.Extent=} opt_extent Optional extent.
* @return {ol.source.WMTS} TileGrid instance.
*/
ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
opt_crossOrigin, opt_matrixSet, opt_requestEncoding, opt_format,
opt_style, opt_dimensions, opt_extent) {
// TODO: add support for TileMatrixLimits
var layers = wmtsCap['contents']['layers'];
var matrixSet = opt_matrixSet;
var l = goog.array.find(layers, function(elt, index, array) {
return elt['identifier'] == layer;
});
goog.asserts.assert(!goog.isNull(l));
var format = goog.isDef(opt_format) ?
opt_format : /** @type {string} */ (l['formats'][0]);
var idx = goog.array.findIndex(l['styles'], function(elt, index, array) {
if (goog.isDef(opt_style)) {
return elt['identifier'] == opt_style;
} else {
return elt['isDefault'];
}
});
if (idx < 0) {
goog.asserts.assert(!goog.isDef(opt_style)); // opt_style was not correct
idx = 0;
}
var style = /** @type {string} */ (l['styles'][idx]['identifier']);
var dimensions = {};
goog.array.forEach(l['dimensions'], function(elt, index, array) {
var key = elt['identifier'];
// With the special value 'default', the server should automatically
// select the default value for this dimension.
var value = (goog.isDef(opt_dimensions) &&
opt_dimensions.hasOwnProperty(key)) ?
opt_dimensions[key] : elt['default'];
if (goog.isDef(value)) {
goog.asserts.assert(goog.array.indexOf(elt['values'], value) >= 0);
} else {
value = elt['values'][0];
}
goog.asserts.assert(goog.isDef(value));
dimensions[key] = value;
});
if (goog.isDef(matrixSet)) {
goog.asserts.assert(0 < goog.array.findIndex(
l['tileMatrixSetLinks'], function(elt, index, arr) {
return elt['tileMatrixSet'] == matrixSet;
}));
} else {
goog.asserts.assert(l['tileMatrixSetLinks'].length > 0);
matrixSet = /** @type {string} */
(l['tileMatrixSetLinks'][0]['tileMatrixSet']);
}
var matrixSets = wmtsCap['contents']['tileMatrixSets'];
goog.asserts.assert(matrixSet in matrixSets);
var matrixSetObj = matrixSets[matrixSet];
var tileGrid = ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet(
matrixSetObj);
var projection = ol.projection.get(matrixSetObj['supportedCRS']);
var gets = wmtsCap['operationsMetadata']['GetTile']['dcp']['http']['get'];
var encodings = goog.object.getKeys(
gets[0]['constraints']['GetEncoding']['allowedValues']);
goog.asserts.assert(encodings.length > 0);
var requestEncoding = goog.isDef(opt_requestEncoding) ?
opt_requestEncoding : /** @type {ol.source.WMTSRequestEncoding} */
(encodings[0]);
// TODO: arcgis support, quote from ol2:
// The OGC documentation is not clear if we should use REST or RESTful,
// ArcGis use RESTful, and OpenLayers use REST.
var urls;
switch (requestEncoding) {
case ol.source.WMTSRequestEncoding.REST:
goog.asserts.assert(l['resourceUrls'].hasOwnProperty('tile'));
goog.asserts.assert(l['resourceUrls']['tile'].hasOwnProperty(format));
urls = /** @type {Array.<string>} */
(l['resourceUrls']['tile'][format]);
break;
case ol.source.WMTSRequestEncoding.KVP:
urls = [];
goog.array.forEach(gets, function(elt, index, array) {
if (elt['constraints']['GetEncoding']['allowedValues'].hasOwnProperty(
ol.source.WMTSRequestEncoding.KVP)) {
urls.push(/** @type {string} */ (elt['url']));
}
});
goog.asserts.assert(urls.length > 0);
break;
default:
goog.asserts.assert(false);
}
return new ol.source.WMTS({
urls: urls,
layer: layer,
matrixSet: matrixSet,
format: format,
projection: projection,
requestEncoding: requestEncoding,
tileGrid: tileGrid,
style: style,
crossOrigin: opt_crossOrigin,
extent: opt_extent,
dimensions: dimensions
});
};