Implement ol.source.WMTS.optionsFromCapabilities
and remove related ol.source.WMTS.createFromCapabilities function.
This commit is contained in:
@@ -30,11 +30,13 @@ xhr.open('GET', 'http://wmts.geo.admin.ch/1.0.0/WMTSCapabilities.xml', true);
|
|||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
if (xhr.status == 200) {
|
if (xhr.status == 200) {
|
||||||
capabilities = parser.read(xhr.responseXML);
|
capabilities = parser.read(xhr.responseXML);
|
||||||
|
var wmtsOptions = ol.source.WMTS.optionsFromCapabilities(
|
||||||
|
capabilities, 'ch.swisstopo.pixelkarte-farbe');
|
||||||
|
wmtsOptions.crossOrigin = null;
|
||||||
map = new ol.Map({
|
map = new ol.Map({
|
||||||
layers: [
|
layers: [
|
||||||
new ol.layer.TileLayer({
|
new ol.layer.TileLayer({
|
||||||
source: ol.source.WMTS.createFromCapabilities(
|
source: new ol.source.WMTS(wmtsOptions)
|
||||||
capabilities, 'ch.swisstopo.pixelkarte-farbe', null)
|
|
||||||
}),
|
}),
|
||||||
new ol.layer.ImageLayer({
|
new ol.layer.ImageLayer({
|
||||||
source: new ol.source.SingleImageWMS({
|
source: new ol.source.SingleImageWMS({
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
@exportClass ol.source.WMTS ol.source.WMTSOptions
|
@exportClass ol.source.WMTS ol.source.WMTSOptions
|
||||||
@exportSymbol ol.source.WMTS.createFromCapabilities
|
@exportSymbol ol.source.WMTS.optionsFromCapabilities
|
||||||
|
|||||||
+12
-48
@@ -168,45 +168,26 @@ goog.inherits(ol.source.WMTS, ol.source.ImageTileSource);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} wmtsCap An object representing the capabilities document.
|
* @param {Object} wmtsCap An object representing the capabilities document.
|
||||||
* @param {string} layer The layer identifier that we want the grid for.
|
* @param {string} layer The layer identifier.
|
||||||
* @param {string|null=} opt_crossOrigin Optional crossOrigin value.
|
* @return {ol.source.WMTSOptions} WMTS source options object.
|
||||||
* @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,
|
ol.source.WMTS.optionsFromCapabilities = function(wmtsCap, layer) {
|
||||||
opt_crossOrigin, opt_matrixSet, opt_requestEncoding, opt_format,
|
|
||||||
opt_style, opt_dimensions, opt_extent) {
|
|
||||||
|
|
||||||
// TODO: add support for TileMatrixLimits
|
// TODO: add support for TileMatrixLimits
|
||||||
|
|
||||||
var layers = wmtsCap['contents']['layers'];
|
var layers = wmtsCap['contents']['layers'];
|
||||||
var matrixSet = opt_matrixSet;
|
|
||||||
var l = goog.array.find(layers, function(elt, index, array) {
|
var l = goog.array.find(layers, function(elt, index, array) {
|
||||||
return elt['identifier'] == layer;
|
return elt['identifier'] == layer;
|
||||||
});
|
});
|
||||||
goog.asserts.assert(!goog.isNull(l));
|
goog.asserts.assert(!goog.isNull(l));
|
||||||
|
goog.asserts.assert(l['tileMatrixSetLinks'].length > 0);
|
||||||
var format = goog.isDef(opt_format) ?
|
var matrixSet = /** @type {string} */
|
||||||
opt_format : /** @type {string} */ (l['formats'][0]);
|
(l['tileMatrixSetLinks'][0]['tileMatrixSet']);
|
||||||
|
var format = /** @type {string} */ (l['formats'][0]);
|
||||||
var idx = goog.array.findIndex(l['styles'], function(elt, index, array) {
|
var idx = goog.array.findIndex(l['styles'], function(elt, index, array) {
|
||||||
if (goog.isDef(opt_style)) {
|
return elt['isDefault'];
|
||||||
return elt['identifier'] == opt_style;
|
|
||||||
} else {
|
|
||||||
return elt['isDefault'];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
goog.asserts.assert(!goog.isDef(opt_style)); // opt_style was not correct
|
|
||||||
idx = 0;
|
idx = 0;
|
||||||
}
|
}
|
||||||
var style = /** @type {string} */ (l['styles'][idx]['identifier']);
|
var style = /** @type {string} */ (l['styles'][idx]['identifier']);
|
||||||
@@ -214,11 +195,7 @@ ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
|
|||||||
var dimensions = {};
|
var dimensions = {};
|
||||||
goog.array.forEach(l['dimensions'], function(elt, index, array) {
|
goog.array.forEach(l['dimensions'], function(elt, index, array) {
|
||||||
var key = elt['identifier'];
|
var key = elt['identifier'];
|
||||||
// With the special value 'default', the server should automatically
|
var value = elt['default'];
|
||||||
// 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)) {
|
if (goog.isDef(value)) {
|
||||||
goog.asserts.assert(goog.array.indexOf(elt['values'], value) >= 0);
|
goog.asserts.assert(goog.array.indexOf(elt['values'], value) >= 0);
|
||||||
} else {
|
} else {
|
||||||
@@ -228,16 +205,6 @@ ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
|
|||||||
dimensions[key] = 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'];
|
var matrixSets = wmtsCap['contents']['tileMatrixSets'];
|
||||||
goog.asserts.assert(matrixSet in matrixSets);
|
goog.asserts.assert(matrixSet in matrixSets);
|
||||||
var matrixSetObj = matrixSets[matrixSet];
|
var matrixSetObj = matrixSets[matrixSet];
|
||||||
@@ -251,8 +218,7 @@ ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
|
|||||||
var encodings = goog.object.getKeys(
|
var encodings = goog.object.getKeys(
|
||||||
gets[0]['constraints']['GetEncoding']['allowedValues']);
|
gets[0]['constraints']['GetEncoding']['allowedValues']);
|
||||||
goog.asserts.assert(encodings.length > 0);
|
goog.asserts.assert(encodings.length > 0);
|
||||||
var requestEncoding = goog.isDef(opt_requestEncoding) ?
|
var requestEncoding = /** @type {ol.source.WMTSRequestEncoding} */
|
||||||
opt_requestEncoding : /** @type {ol.source.WMTSRequestEncoding} */
|
|
||||||
(encodings[0]);
|
(encodings[0]);
|
||||||
// TODO: arcgis support, quote from ol2:
|
// TODO: arcgis support, quote from ol2:
|
||||||
// The OGC documentation is not clear if we should use REST or RESTful,
|
// The OGC documentation is not clear if we should use REST or RESTful,
|
||||||
@@ -280,7 +246,7 @@ ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
|
|||||||
goog.asserts.assert(false);
|
goog.asserts.assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ol.source.WMTS({
|
return {
|
||||||
urls: urls,
|
urls: urls,
|
||||||
layer: layer,
|
layer: layer,
|
||||||
matrixSet: matrixSet,
|
matrixSet: matrixSet,
|
||||||
@@ -289,8 +255,6 @@ ol.source.WMTS.createFromCapabilities = function(wmtsCap, layer,
|
|||||||
requestEncoding: requestEncoding,
|
requestEncoding: requestEncoding,
|
||||||
tileGrid: tileGrid,
|
tileGrid: tileGrid,
|
||||||
style: style,
|
style: style,
|
||||||
crossOrigin: opt_crossOrigin,
|
|
||||||
extent: opt_extent,
|
|
||||||
dimensions: dimensions
|
dimensions: dimensions
|
||||||
});
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user