Support for multiple resource urls

With this change, createLayer generates url arrays for both KVP and REST
encoding if multiple resource urls are provided. To make this work, the
WMTSCapabilities format got a new resourceUrls property, because
previously only the first resourceUrl for a format was stored.
This commit is contained in:
ahocevar
2012-10-01 15:28:30 +02:00
parent 51ae75a54f
commit 22c5e76b0b
4 changed files with 68 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
OpenLayers.ProxyHost = "/proxy/?url="; OpenLayers.ProxyHost = "proxy.cgi/?url=";
var map, format; var map, format;

View File

@@ -73,6 +73,8 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
* matrixSet - {String} The matrix set identifier, required if there is * matrixSet - {String} The matrix set identifier, required if there is
* more than one matrix set in the layer capabilities. * more than one matrix set in the layer capabilities.
* style - {String} The name of the style * style - {String} The name of the style
* format - {String} Image format for the layer. Default is the first
* format returned in the GetCapabilities response.
* param - {Object} The dimensions values eg: {"Year": "2012"} * param - {Object} The dimensions values eg: {"Year": "2012"}
* *
* Returns: * Returns:
@@ -102,6 +104,11 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
if (!layerDef) { if (!layerDef) {
throw new Error("Layer not found"); throw new Error("Layer not found");
} }
var format = config.format;
if (!format && layerDef.formats && layerDef.formats.length) {
format = layerDef.formats[0];
}
// find the matrixSet definition // find the matrixSet definition
var matrixSet; var matrixSet;
@@ -170,19 +177,30 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
} }
var url; var url;
if (requestEncoding === "REST" && layerDef.resourceUrl) { if (requestEncoding === "REST" && layerDef.resourceUrls) {
url = layerDef.resourceUrl.tile.template; url = [];
var resourceUrls = layerDef.resourceUrls,
resourceUrl;
for (var t = 0, tt = layerDef.resourceUrls.length; t < tt; ++t) {
resourceUrl = layerDef.resourceUrls[t];
if (resourceUrl.format === format && resourceUrl.resourceType === "tile") {
url.push(resourceUrl.template);
}
}
} }
else { else {
var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get; var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get;
url = httpGet[0].url; url = [];
for (var i = 0, ii = httpGet.length; i < ii; i++) { for (var i = 0, ii = httpGet.length; i < ii; i++) {
if (httpGet[i].constraints && httpGet[i].constraints. if (httpGet[i].constraints && httpGet[i].constraints.
GetEncoding.allowedValues[requestEncoding]) { GetEncoding.allowedValues[requestEncoding]) {
url = httpGet[i].url; url.push(httpGet[i].url);
break;
} }
} }
// fallback for backwards compatibility
if (url.length === 0) {
url = httpGet[0].url;
}
} }
return new OpenLayers.Layer.WMTS( return new OpenLayers.Layer.WMTS(
@@ -191,6 +209,7 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
requestEncoding: requestEncoding, requestEncoding: requestEncoding,
name: layerDef.title, name: layerDef.title,
style: style.identifier, style: style.identifier,
format: format,
matrixIds: matrixSet.matrixIds, matrixIds: matrixSet.matrixIds,
matrixSet: matrixSet.identifier, matrixSet: matrixSet.identifier,
projection: projection, projection: projection,

View File

@@ -195,10 +195,16 @@ OpenLayers.Format.WMTSCapabilities.v1_0_0 = OpenLayers.Class(
}, },
"ResourceURL": function(node, obj) { "ResourceURL": function(node, obj) {
obj.resourceUrl = obj.resourceUrl || {}; obj.resourceUrl = obj.resourceUrl || {};
obj.resourceUrl[node.getAttribute("resourceType")] = { var resourceType = node.getAttribute("resourceType");
if (!obj.resourceUrls) {
obj.resourceUrls = [];
}
var resourceType = obj.resourceUrl[resourceType] = {
format: node.getAttribute("format"), format: node.getAttribute("format"),
template: node.getAttribute("template") template: node.getAttribute("template"),
resourceType: resourceType
}; };
obj.resourceUrls.push(resourceType);
}, },
// not used for now, can be added in the future though // not used for now, can be added in the future though
/*"Themes": function(node, obj) { /*"Themes": function(node, obj) {

View File

@@ -45,7 +45,7 @@
} }
function test_layers(t) { function test_layers(t) {
t.plan(37); t.plan(43);
var xml = document.getElementById("ogcsample").firstChild.nodeValue; var xml = document.getElementById("ogcsample").firstChild.nodeValue;
var doc = new OpenLayers.Format.XML().read(xml); var doc = new OpenLayers.Format.XML().read(xml);
@@ -96,6 +96,16 @@
t.eq(layer.resourceUrl.FeatureInfo.template, "http://www.example.com/wmts/coastlines/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}.xml", t.eq(layer.resourceUrl.FeatureInfo.template, "http://www.example.com/wmts/coastlines/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}.xml",
"resourceUrl.FeatureInfo.template is correct"); "resourceUrl.FeatureInfo.template is correct");
t.eq(layer.resourceUrls[0].format, "image/png", "resourceUrls[0].format is correct");
t.eq(layer.resourceUrls[0].resourceType, "tile", "resourceUrls[0].resourceType is correct");
t.eq(layer.resourceUrls[0].template, "http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png",
"resourceUrls[0].template is correct");
t.eq(layer.resourceUrls[1].format, "application/gml+xml; version=3.1", "resourceUrls[0].format is correct");
t.eq(layer.resourceUrls[1].resourceType, "FeatureInfo", "resourceUrls[0].resourceType is correct");
t.eq(layer.resourceUrls[1].template, "http://www.example.com/wmts/coastlines/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}.xml",
"resourceUrls[0].template is correct");
var dimensions = layer.dimensions; var dimensions = layer.dimensions;
t.eq(dimensions.length, 1, "correct count of dimensions"); t.eq(dimensions.length, 1, "correct count of dimensions");
t.eq(dimensions[0].title, "Time", "first dimension title is correct"); t.eq(dimensions[0].title, "Time", "first dimension title is correct");
@@ -140,7 +150,7 @@
} }
function test_createLayer(t) { function test_createLayer(t) {
t.plan(38); t.plan(41);
var format = new OpenLayers.Format.WMTSCapabilities(); var format = new OpenLayers.Format.WMTSCapabilities();
@@ -207,7 +217,8 @@
matrixSet: "21781" matrixSet: "21781"
}); });
t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance"); t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance");
t.eq(layer.url, "http://wmts.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png", "correct url"); t.eq(layer.url[0], "http://wmts.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png", "correct url");
t.eq(layer.url[1], "http://wmts1.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png", "correct url");
t.eq(layer.matrixIds.length, 3, "correct matrixIds length"); t.eq(layer.matrixIds.length, 3, "correct matrixIds length");
t.eq(layer.requestEncoding, "REST", "correct requestEncoding"); t.eq(layer.requestEncoding, "REST", "correct requestEncoding");
t.eq(layer.name, "Agglomérations et villes isolées", "correct layer title"); t.eq(layer.name, "Agglomérations et villes isolées", "correct layer title");
@@ -272,13 +283,15 @@
matrixSet: "21781", matrixSet: "21781",
requestEncoding: 'REST' requestEncoding: 'REST'
}); });
t.eq(layer.url, "http://wmts.geo.admin.ch/rest", "correct rest url 1"); t.eq(layer.url[0], "http://wmts.geo.admin.ch/rest", "correct rest url 1");
t.eq(layer.url[1], "http://wmts1.geo.admin.ch/rest", "correct rest url 1");
layer = format.createLayer(caps, { layer = format.createLayer(caps, {
layer: "ch.are.agglomerationen_isolierte_staedte-2000", layer: "ch.are.agglomerationen_isolierte_staedte-2000",
matrixSet: "21781", matrixSet: "21781",
requestEncoding: 'KVP' requestEncoding: 'KVP'
}); });
t.eq(layer.url, "http://wmts.geo.admin.ch/kvp", "correct kvp url 2"); t.eq(layer.url[0], "http://wmts.geo.admin.ch/kvp", "correct kvp url 2");
t.eq(layer.url[1], "http://wmts1.geo.admin.ch/kvp", "correct kvp url 2");
xml = document.getElementById("multi-getile-2").firstChild.nodeValue; xml = document.getElementById("multi-getile-2").firstChild.nodeValue;
doc = new OpenLayers.Format.XML().read(xml); doc = new OpenLayers.Format.XML().read(xml);
caps = format.read(doc); caps = format.read(doc);
@@ -287,13 +300,13 @@
matrixSet: "21781", matrixSet: "21781",
requestEncoding: 'REST' requestEncoding: 'REST'
}); });
t.eq(layer.url, "http://wmts.geo.admin.ch/rest", "correct rest url 2"); t.eq(layer.url[0], "http://wmts.geo.admin.ch/rest", "correct rest url 2");
layer = format.createLayer(caps, { layer = format.createLayer(caps, {
layer: "ch.are.agglomerationen_isolierte_staedte-2000", layer: "ch.are.agglomerationen_isolierte_staedte-2000",
matrixSet: "21781", matrixSet: "21781",
requestEncoding: 'KVP' requestEncoding: 'KVP'
}); });
t.eq(layer.url, "http://wmts.geo.admin.ch/kvp", "correct kvp url 2"); t.eq(layer.url[0], "http://wmts.geo.admin.ch/kvp", "correct kvp url 2");
} }
function test_parse_projection(t) { function test_parse_projection(t) {
@@ -579,6 +592,7 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml
<TileMatrixSet>21781</TileMatrixSet> <TileMatrixSet>21781</TileMatrixSet>
</TileMatrixSetLink> </TileMatrixSetLink>
<ResourceURL format="image/png" resourceType="tile" template="http://wmts.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"/> <ResourceURL format="image/png" resourceType="tile" template="http://wmts.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"/>
<ResourceURL format="image/png" resourceType="tile" template="http://wmts1.geo.admin.ch/1.0.0/ch.are.agglomerationen_isolierte_staedte-2000/default/{Time}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"/>
</Layer> </Layer>
<TileMatrixSet> <TileMatrixSet>
<ows:Identifier>21781</ows:Identifier> <ows:Identifier>21781</ows:Identifier>
@@ -793,6 +807,13 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml
</ows:AllowedValues> </ows:AllowedValues>
</ows:Constraint> </ows:Constraint>
</ows:Get> </ows:Get>
<ows:Get xlink:href="http://wmts1.geo.admin.ch/rest">
<ows:Constraint name="GetEncoding">
<ows:AllowedValues>
<ows:Value>REST</ows:Value>
</ows:AllowedValues>
</ows:Constraint>
</ows:Get>
<ows:Get xlink:href="http://wmts.geo.admin.ch/kvp"> <ows:Get xlink:href="http://wmts.geo.admin.ch/kvp">
<ows:Constraint name="GetEncoding"> <ows:Constraint name="GetEncoding">
<ows:AllowedValues> <ows:AllowedValues>
@@ -800,6 +821,13 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml
</ows:AllowedValues> </ows:AllowedValues>
</ows:Constraint> </ows:Constraint>
</ows:Get> </ows:Get>
<ows:Get xlink:href="http://wmts1.geo.admin.ch/kvp">
<ows:Constraint name="GetEncoding">
<ows:AllowedValues>
<ows:Value>KVP</ows:Value>
</ows:AllowedValues>
</ows:Constraint>
</ows:Get>
</ows:HTTP> </ows:HTTP>
</ows:DCP> </ows:DCP>
</ows:Operation> </ows:Operation>