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:
@@ -18,7 +18,7 @@ function init() {
|
||||
doc = request.responseText;
|
||||
}
|
||||
var capabilities = format.read(doc);
|
||||
var layer = createLayer(capabilities, {
|
||||
var layer = format.createLayer(capabilities, {
|
||||
layer: "medford:buildings",
|
||||
matrixSet: "EPSG:900913",
|
||||
format: "image/png",
|
||||
@@ -50,39 +50,3 @@ function init() {
|
||||
map.setCenter(new OpenLayers.LonLat(-13677832, 5213272), 13);
|
||||
|
||||
}
|
||||
|
||||
function createLayer(capabilities, config) {
|
||||
|
||||
var contents = capabilities.contents;
|
||||
var matrixSet = contents.tileMatrixSets[config.matrixSet];
|
||||
|
||||
// find the layer definition with the given identifier
|
||||
var layers = contents.layers;
|
||||
var layer;
|
||||
for (var i=0, ii=layers.length; i<ii; ++i) {
|
||||
if (layers[i].identifier === config.layer) {
|
||||
layer = layers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// get the default style for the layer
|
||||
var style;
|
||||
for (var i=0, ii=layer.styles.length; i<ii; ++i) {
|
||||
style = layer.styles[i];
|
||||
if (style.isDefault === "true") { // TODO: change this to boolean
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// create the layer
|
||||
return new OpenLayers.Layer.WMTS(
|
||||
OpenLayers.Util.applyDefaults(config, {
|
||||
url: capabilities.operationsMetadata.GetTile.dcp.http.get,
|
||||
name: layer.title,
|
||||
style: style,
|
||||
matrixIds: matrixSet.matrixIds
|
||||
})
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ OpenLayers.Format.WMTSCapabilities.v1_0_0 = OpenLayers.Class(
|
||||
},
|
||||
"Style": function(node, obj) {
|
||||
var style = {};
|
||||
style.isDefault = node.getAttribute("isDefault");
|
||||
style.isDefault = (node.getAttribute("isDefault") === "true");
|
||||
this.readChildNodes(node, style);
|
||||
obj.styles.push(style);
|
||||
},
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
var numOfStyles = layer.styles.length;
|
||||
t.eq(numOfStyles, 2, "correct count of styles");
|
||||
t.eq(layer.styles[0].identifier, "DarkBlue", "style 0 identifier is correct");
|
||||
t.eq(layer.styles[0].isDefault, "true", "style 0 isDefault is correct");
|
||||
t.eq(layer.styles[0].isDefault, true, "style 0 isDefault is correct");
|
||||
t.eq(layer.styles[0].title, "Dark Blue", "style 0 title is correct");
|
||||
t.eq(layer.styles[1].identifier, "thickAndRed", "style 1 identifier is correct");
|
||||
t.ok(!layer.styles[1].isDefault, "style 1 isDefault is correct");
|
||||
@@ -108,6 +108,54 @@
|
||||
t.eq(bigWorld.matrixIds[1].topLeftCorner.lon, -180, "tileMatrix 1 topLeftCorner.lon is correct");
|
||||
t.eq(bigWorld.matrixIds[1].topLeftCorner.lat, 84, "tileMatrix 1 topLeftCorner.lat is correct");
|
||||
}
|
||||
|
||||
function test_createLayer(t) {
|
||||
t.plan(6);
|
||||
|
||||
var format = new OpenLayers.Format.WMTSCapabilities();
|
||||
|
||||
var xml = document.getElementById("ogcsample").firstChild.nodeValue;
|
||||
var doc = new OpenLayers.Format.XML().read(xml);
|
||||
|
||||
var caps = format.read(doc);
|
||||
var layer;
|
||||
|
||||
var success = true;
|
||||
try {
|
||||
// incomplete config (missing matrixSet)
|
||||
layer = format.createLayer(caps, {
|
||||
layer: "coastlines"
|
||||
});
|
||||
} catch (err) {
|
||||
success = false;
|
||||
}
|
||||
t.ok(!success, "createLayer throws error if provided incomplete layer config");
|
||||
|
||||
// bogus layer identifier
|
||||
layer = format.createLayer(caps, {
|
||||
layer: "foo",
|
||||
matrixSet: "BigWorld"
|
||||
});
|
||||
t.eq(layer, undefined, "createLayer returns undefined given bad layer identifier");
|
||||
|
||||
// bogus matrixSet identifier
|
||||
layer = format.createLayer(caps, {
|
||||
layer: "coastlines",
|
||||
matrixSet: "TheWorld"
|
||||
});
|
||||
t.eq(layer, undefined, "createLayer returns undefined given bad matrixSet identifier");
|
||||
|
||||
layer = format.createLayer(caps, {
|
||||
layer: "coastlines",
|
||||
matrixSet: "BigWorld"
|
||||
});
|
||||
t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance");
|
||||
|
||||
t.eq(layer.matrixIds.length, 2, "correct matrixIds length");
|
||||
t.eq(layer.name, "Coastlines", "correct layer title");
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
Reference in New Issue
Block a user