From 22c5e76b0b7ce0c09bec64bf481f457c7e14844b Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 1 Oct 2012 15:28:30 +0200 Subject: [PATCH 1/4] 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. --- examples/wmts-capabilities.js | 2 +- lib/OpenLayers/Format/WMTSCapabilities.js | 29 ++++++++++--- .../Format/WMTSCapabilities/v1_0_0.js | 10 ++++- tests/Format/WMTSCapabilities/v1_0_0.html | 42 +++++++++++++++---- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/examples/wmts-capabilities.js b/examples/wmts-capabilities.js index 0b2e011c99..103f5f5bcd 100644 --- a/examples/wmts-capabilities.js +++ b/examples/wmts-capabilities.js @@ -1,4 +1,4 @@ -OpenLayers.ProxyHost = "/proxy/?url="; +OpenLayers.ProxyHost = "proxy.cgi/?url="; var map, format; diff --git a/lib/OpenLayers/Format/WMTSCapabilities.js b/lib/OpenLayers/Format/WMTSCapabilities.js index b8b667437f..fb2ddd1429 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities.js +++ b/lib/OpenLayers/Format/WMTSCapabilities.js @@ -73,6 +73,8 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers * matrixSet - {String} The matrix set identifier, required if there is * more than one matrix set in the layer capabilities. * 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"} * * Returns: @@ -102,6 +104,11 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers if (!layerDef) { 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 var matrixSet; @@ -170,19 +177,30 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers } var url; - if (requestEncoding === "REST" && layerDef.resourceUrl) { - url = layerDef.resourceUrl.tile.template; + if (requestEncoding === "REST" && layerDef.resourceUrls) { + 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 { var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get; - url = httpGet[0].url; + url = []; for (var i = 0, ii = httpGet.length; i < ii; i++) { if (httpGet[i].constraints && httpGet[i].constraints. GetEncoding.allowedValues[requestEncoding]) { - url = httpGet[i].url; - break; + url.push(httpGet[i].url); } } + // fallback for backwards compatibility + if (url.length === 0) { + url = httpGet[0].url; + } } return new OpenLayers.Layer.WMTS( @@ -191,6 +209,7 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers requestEncoding: requestEncoding, name: layerDef.title, style: style.identifier, + format: format, matrixIds: matrixSet.matrixIds, matrixSet: matrixSet.identifier, projection: projection, diff --git a/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js b/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js index d65409eb73..413363b652 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js +++ b/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js @@ -195,10 +195,16 @@ OpenLayers.Format.WMTSCapabilities.v1_0_0 = OpenLayers.Class( }, "ResourceURL": function(node, obj) { 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"), - 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 /*"Themes": function(node, obj) { diff --git a/tests/Format/WMTSCapabilities/v1_0_0.html b/tests/Format/WMTSCapabilities/v1_0_0.html index 9017e0ed71..98dc00f3c0 100644 --- a/tests/Format/WMTSCapabilities/v1_0_0.html +++ b/tests/Format/WMTSCapabilities/v1_0_0.html @@ -45,7 +45,7 @@ } function test_layers(t) { - t.plan(37); + t.plan(43); var xml = document.getElementById("ogcsample").firstChild.nodeValue; 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", "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; t.eq(dimensions.length, 1, "correct count of dimensions"); t.eq(dimensions[0].title, "Time", "first dimension title is correct"); @@ -140,7 +150,7 @@ } function test_createLayer(t) { - t.plan(38); + t.plan(41); var format = new OpenLayers.Format.WMTSCapabilities(); @@ -207,7 +217,8 @@ matrixSet: "21781" }); 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.requestEncoding, "REST", "correct requestEncoding"); t.eq(layer.name, "Agglomérations et villes isolées", "correct layer title"); @@ -272,13 +283,15 @@ matrixSet: "21781", 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: "ch.are.agglomerationen_isolierte_staedte-2000", matrixSet: "21781", 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; doc = new OpenLayers.Format.XML().read(xml); caps = format.read(doc); @@ -287,13 +300,13 @@ matrixSet: "21781", 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: "ch.are.agglomerationen_isolierte_staedte-2000", matrixSet: "21781", 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) { @@ -579,6 +592,7 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml 21781 + 21781 @@ -793,6 +807,13 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml + + + + REST + + + @@ -800,6 +821,13 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml + + + + KVP + + + From 363306bcd10027ed139633f736505fc1d990664f Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 2 Oct 2012 08:58:47 +0200 Subject: [PATCH 2/4] Multiple URLs also for unconstrained GetTile resources --- lib/OpenLayers/Format/WMTSCapabilities.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/OpenLayers/Format/WMTSCapabilities.js b/lib/OpenLayers/Format/WMTSCapabilities.js index fb2ddd1429..8efce532b2 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities.js +++ b/lib/OpenLayers/Format/WMTSCapabilities.js @@ -191,16 +191,14 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers else { var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get; url = []; + var constraint; for (var i = 0, ii = httpGet.length; i < ii; i++) { - if (httpGet[i].constraints && httpGet[i].constraints. - GetEncoding.allowedValues[requestEncoding]) { + cnstraint = httpGet[i].constraints; + if (!constraint || (constraint && constraint. + GetEncoding.allowedValues[requestEncoding])) { url.push(httpGet[i].url); } } - // fallback for backwards compatibility - if (url.length === 0) { - url = httpGet[0].url; - } } return new OpenLayers.Layer.WMTS( From f055f13a3aac755a36040197737a3acc1b039707 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 2 Oct 2012 10:56:06 +0200 Subject: [PATCH 3/4] Fixing typo Thanks @sbrunner for catching this. That's what I got from making last-minute changes before committing without testing again. --- lib/OpenLayers/Format/WMTSCapabilities.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OpenLayers/Format/WMTSCapabilities.js b/lib/OpenLayers/Format/WMTSCapabilities.js index 8efce532b2..02c7ab321c 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities.js +++ b/lib/OpenLayers/Format/WMTSCapabilities.js @@ -193,7 +193,7 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers url = []; var constraint; for (var i = 0, ii = httpGet.length; i < ii; i++) { - cnstraint = httpGet[i].constraints; + constraint = httpGet[i].constraints; if (!constraint || (constraint && constraint. GetEncoding.allowedValues[requestEncoding])) { url.push(httpGet[i].url); From ea86afec16b6d6dbe98b5de93924f02eca77ace4 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 2 Oct 2012 10:56:41 +0200 Subject: [PATCH 4/4] Addressing @sbrunner's review comment --- lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js b/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js index 413363b652..620b9ea5dc 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js +++ b/lib/OpenLayers/Format/WMTSCapabilities/v1_0_0.js @@ -199,12 +199,12 @@ OpenLayers.Format.WMTSCapabilities.v1_0_0 = OpenLayers.Class( if (!obj.resourceUrls) { obj.resourceUrls = []; } - var resourceType = obj.resourceUrl[resourceType] = { + var resourceUrl = obj.resourceUrl[resourceType] = { format: node.getAttribute("format"), template: node.getAttribute("template"), resourceType: resourceType }; - obj.resourceUrls.push(resourceType); + obj.resourceUrls.push(resourceUrl); }, // not used for now, can be added in the future though /*"Themes": function(node, obj) {