Making it more convenient to create WMTS layers from capabilities documents. The format now has a createLayer method that takes a capabilities response object and a layer configuration object. This returns a properly configured WMTS layer based on the layer and matrix definition found in the capabilities. Unless otherwise specified, the layer name will be derived from the advertised title, and the style identifier will be the advertised default. r=ahocevar (closes #2676)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10449 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2010-07-02 16:11:37 +00:00
parent ef9afd22cb
commit 22aa4281fa
4 changed files with 119 additions and 39 deletions

View File

@@ -74,6 +74,74 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
}
return parser.read(data);
},
/**
* APIMethod: createLayer
* Create a WMTS layer given a capabilities object.
*
* Parameters:
* capabilities - {Object} The object returned from a <read> call to this
* format.
* config - {Object} Configuration properties for the layer. Defaults for
* the layer will apply if not provided.
*
* Required config properties:
* layer - {String} The layer identifier.
* matrixSet - {String} The matrix set identifier.
*
* Returns:
* {<OpenLayers.Layer.WMTS>} A properly configured WMTS layer. Throws an
* error if an incomplete config is provided. Returns undefined if no
* layer could be created with the provided config.
*/
createLayer: function(capabilities, config) {
var layer;
// confirm required properties are supplied in config
var required = {
layer: true,
matrixSet: true
};
for (var prop in required) {
if (!(prop in config)) {
throw new Error("Missing property '" + prop + "' in layer configuration.");
}
}
var contents = capabilities.contents;
var matrixSet = contents.tileMatrixSets[config.matrixSet];
// find the layer definition with the given identifier
var layers = contents.layers;
var layerDef;
for (var i=0, ii=contents.layers.length; i<ii; ++i) {
if (contents.layers[i].identifier === config.layer) {
layerDef = contents.layers[i];
break;
}
}
if (layerDef && matrixSet) {
// get the default style for the layer
var style;
for (var i=0, ii=layerDef.styles.length; i<ii; ++i) {
style = layerDef.styles[i];
if (style.isDefault) {
break;
}
}
layer = new OpenLayers.Layer.WMTS(
OpenLayers.Util.applyDefaults(config, {
url: capabilities.operationsMetadata.GetTile.dcp.http.get,
name: layerDef.title,
style: style,
matrixIds: matrixSet.matrixIds
})
);
}
return layer;
},
CLASS_NAME: "OpenLayers.Format.WMTSCapabilities"