diff --git a/examples/wmts-capabilities.html b/examples/wmts-capabilities.html index 21b3cae0c7..58aabd5f20 100644 --- a/examples/wmts-capabilities.html +++ b/examples/wmts-capabilities.html @@ -20,15 +20,15 @@

Web Map Tile Service (WMTS) Capabilities Parsing

wmts, capabilities, getcapabilities -
+

The WMTS Capabilities format allows for parsing of capabilities - documents from OGC Web Map Tile Service (WMTS) version 1.0.0 + documents from OGC Web Map Tile Service (WMTS) version 1.0.0 implementations.

- +
- +

This example creates an OpenLayers.Layer.WMTS layer to based diff --git a/examples/wmts-capabilities.js b/examples/wmts-capabilities.js index dc12941058..0b2e011c99 100644 --- a/examples/wmts-capabilities.js +++ b/examples/wmts-capabilities.js @@ -3,13 +3,13 @@ OpenLayers.ProxyHost = "/proxy/?url="; var map, format; function init() { - + format = new OpenLayers.Format.WMTSCapabilities({ /** * This particular service is not in compliance with the WMTS spec and * is providing coordinates in y, x order regardless of the CRS. To - * work around this, we can provide the format a table of CRS URN that - * should be considered y, x order. These will extend the defaults on + * work around this, we can provide the format a table of CRS URN that + * should be considered y, x order. These will extend the defaults on * the format. */ yx: { @@ -38,22 +38,21 @@ function init() { isBaseLayer: false }); map.addLayer(layer); - }, + }, failure: function() { alert("Trouble getting capabilities doc"); OpenLayers.Console.error.apply(OpenLayers.Console, arguments); } }); - + map = new OpenLayers.Map({ div: "map", projection: "EPSG:900913" - }); - + }); + var osm = new OpenLayers.Layer.OSM(); map.addLayer(osm); map.addControl(new OpenLayers.Control.LayerSwitcher()); map.setCenter(new OpenLayers.LonLat(-13677832, 5213272), 13); - } diff --git a/lib/OpenLayers/Format/WMTSCapabilities.js b/lib/OpenLayers/Format/WMTSCapabilities.js index 1d2b82c4c3..157042cb55 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities.js +++ b/lib/OpenLayers/Format/WMTSCapabilities.js @@ -6,27 +6,27 @@ /** * @requires OpenLayers/Format/XML/VersionedOGC.js */ - + /** * Class: OpenLayers.Format.WMTSCapabilities * Read WMTS Capabilities. - * + * * Inherits from: * - */ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { - + /** * APIProperty: defaultVersion * {String} Version number to assume if none found. Default is "1.0.0". */ defaultVersion: "1.0.0", - + /** * APIProperty: yx * {Object} Members in the yx object are used to determine if a CRS URN * corresponds to a CRS with y,x axis order. Member names are CRS URNs - * and values are boolean. By default, the following CRS URN are + * and values are boolean. By default, the following CRS URN are * assumed to correspond to a CRS with y,x axis order: * * * urn:ogc:def:crs:EPSG::4326 @@ -48,8 +48,8 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers * APIMethod: read * Read capabilities data from a string, and return information about * the service (offering and observedProperty mostly). - * - * Parameters: + * + * Parameters: * data - {String} or {DOMElement} data to read/parse. * * Returns: @@ -61,36 +61,34 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers * Create a WMTS layer given a capabilities object. * * Parameters: - * capabilities - {Object} The object returned from a call to this + * capabilities - {Object} The object returned from a 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. + * + * Optional config properties: + * 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 + * param - {Object} The dimensions values eg: {"Year": "2012"} * * Returns: * {} 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."); - } + if (!('layer' in config)) { + throw new Error("Missing property 'layer' in configuration."); } var contents = capabilities.contents; - var matrixSet = contents.tileMatrixSets[config.matrixSet]; // find the layer definition with the given identifier var layers = contents.layers; @@ -101,32 +99,97 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers break; } } - - if (layerDef && matrixSet) { - // get the default style for the layer - var style; - for (var i=0, ii=layerDef.styles.length; i} The top left corner of the * matrix. Must be provided if different than the layer . * tileWidth - {Number} The tile width for the matrix. Must be provided diff --git a/tests/Format/WMTSCapabilities/v1_0_0.html b/tests/Format/WMTSCapabilities/v1_0_0.html index f8b5a37215..43de62a077 100644 --- a/tests/Format/WMTSCapabilities/v1_0_0.html +++ b/tests/Format/WMTSCapabilities/v1_0_0.html @@ -43,15 +43,15 @@ undefined, "ows:OperationsMetadata GetTile Constraints Get is correct"); } - + function test_layers(t) { t.plan(37); var xml = document.getElementById("ogcsample").firstChild.nodeValue; var doc = new OpenLayers.Format.XML().read(xml); - + var obj = new OpenLayers.Format.WMTSCapabilities().read(doc); var contents = obj.contents; - + var numOfLayers = contents.layers.length; t.eq(numOfLayers, 1, "correct count of layers"); @@ -89,11 +89,11 @@ t.eq(wgs84Bbox.top, 90.0, "wgs84BoudingBox top is correct"); t.eq(layer.resourceUrl.tile.format, "image/png", "resourceUrl.tile.format is correct"); - t.eq(layer.resourceUrl.tile.template, "http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png", + t.eq(layer.resourceUrl.tile.template, "http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png", "resourceUrl.tile.template is correct"); t.eq(layer.resourceUrl.FeatureInfo.format, "application/gml+xml; version=3.1", "resourceUrl.FeatureInfo.format is correct"); - 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"); var dimensions = layer.dimensions; @@ -112,7 +112,7 @@ t.plan(19); var xml = document.getElementById("ogcsample").firstChild.nodeValue; var doc = new OpenLayers.Format.XML().read(xml); - + var obj = new OpenLayers.Format.WMTSCapabilities().read(doc); var tileMatrixSets = obj.contents.tileMatrixSets; @@ -138,54 +138,123 @@ 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(7); - + t.plan(27); + 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) + // incomplete config (missing layer) 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"); - + try { + layer = format.createLayer(caps, { + layer: "foo", + matrixSet: "BigWorld" + }); + } catch (err) { + success = false; + } + t.ok(!success, "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"); - + try { + layer = format.createLayer(caps, { + layer: "coastlines", + matrixSet: "TheWorld" + }); + } catch (err) { + success = false; + } + t.ok(!success, "createLayer returns undefined given bad matrixSet identifier"); + layer = format.createLayer(caps, { layer: "coastlines", matrixSet: "BigWorld" }); t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance"); - + + // autodetect matrixSet + layer = format.createLayer(caps, { + layer: "coastlines" + }); + t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance, with autodetected matrixSet"); + t.eq(layer.matrixIds.length, 2, "correct matrixIds length"); t.eq(layer.name, "Coastlines", "correct layer title"); t.eq(layer.style, "DarkBlue", "correct style identifier"); + t.eq(layer.requestEncoding, "KVP", "correct requestEncoding"); + + xml = document.getElementById("restsample").firstChild.nodeValue; + doc = new OpenLayers.Format.XML().read(xml); + caps = format.read(doc); + layer = format.createLayer(caps, { + layer: "ch.are.agglomerationen_isolierte_staedte-2000", + 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.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"); + t.eq(layer.style, "ch.are.agglomerationen_isolierte_staedte-2000", "correct style identifier"); + t.eq(layer.projection.getCode(), "EPSG:21781", "correct projection"); + t.eq(layer.units, "m", "correct untis"); + t.eq(layer.resolutions.length, 3, "correct resolutions length"); + t.ok((layer.resolutions[0] - 4000) < 1, "correct first resolution"); + t.eq(layer.dimensions.length, 1, "correct dimensions length"); + t.eq(layer.dimensions[0], "Time", "correct dimensions"); + t.eq(layer.params['TIME'], "20090101", "correct params"); + + layer = format.createLayer(caps, { + layer: "ch.are.agglomerationen_isolierte_staedte-2000", + style: "toto", + params: {"Time": "2012"} + }); + t.eq(layer.matrixIds.length, 3, "correct matrixIds length"); + t.eq(layer.style, "toto", "correct style identifier"); + t.eq(layer.dimensions.length, 1, "correct dimensions length"); + t.eq(layer.dimensions[0], "Time", "correct dimensions"); + t.eq(layer.params['TIME'], "2012", "correct params"); + } + + function test_parse_projection(t) { + t.plan(2); + + var format = new OpenLayers.Format.WMTSCapabilities(); + + var xml = document.getElementById("restsample-alternate-proj1").firstChild.nodeValue; + var doc = new OpenLayers.Format.XML().read(xml); + var caps = format.read(doc); + var layer = format.createLayer(caps, { + layer: "ch.are.agglomerationen_isolierte_staedte-2000", + matrixSet: "21781" + }); + t.eq(layer.projection.getCode(), "EPSG:21781", "correct projection"); + + xml = document.getElementById("restsample-alternate-proj2").firstChild.nodeValue; + doc = new OpenLayers.Format.XML().read(xml); + caps = format.read(doc); + layer = format.createLayer(caps, { + layer: "ch.are.agglomerationen_isolierte_staedte-2000", + matrixSet: "21781" + }); + t.eq(layer.projection.getCode(), "EPSG:21781", "correct projection"); } - @@ -300,15 +369,15 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml BigWorld - + BigWorld urn:ogc:def:crs:OGC:1.3:CRS84 1e6 - 1e6 - -180 84 + 1e6 + -180 84 256 - 256 + 256 60000 50000 @@ -354,5 +423,284 @@ http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml -->

+ +
+ +
+ +
+