WMTSCapabilities.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Format/XML/VersionedOGC.js
  7. */
  8. /**
  9. * Class: OpenLayers.Format.WMTSCapabilities
  10. * Read WMTS Capabilities.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Format.XML.VersionedOGC>
  14. */
  15. OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
  16. /**
  17. * APIProperty: defaultVersion
  18. * {String} Version number to assume if none found. Default is "1.0.0".
  19. */
  20. defaultVersion: "1.0.0",
  21. /**
  22. * APIProperty: yx
  23. * {Object} Members in the yx object are used to determine if a CRS URN
  24. * corresponds to a CRS with y,x axis order. Member names are CRS URNs
  25. * and values are boolean. By default, the following CRS URN are
  26. * assumed to correspond to a CRS with y,x axis order:
  27. *
  28. * * urn:ogc:def:crs:EPSG::4326
  29. */
  30. yx: {
  31. "urn:ogc:def:crs:EPSG::4326": true
  32. },
  33. /**
  34. * Constructor: OpenLayers.Format.WMTSCapabilities
  35. * Create a new parser for WMTS capabilities.
  36. *
  37. * Parameters:
  38. * options - {Object} An optional object whose properties will be set on
  39. * this instance.
  40. */
  41. /**
  42. * APIMethod: read
  43. * Read capabilities data from a string, and return information about
  44. * the service (offering and observedProperty mostly).
  45. *
  46. * Parameters:
  47. * data - {String} or {DOMElement} data to read/parse.
  48. *
  49. * Returns:
  50. * {Object} Info about the WMTS Capabilities
  51. */
  52. /**
  53. * APIMethod: createLayer
  54. * Create a WMTS layer given a capabilities object.
  55. *
  56. * Parameters:
  57. * capabilities - {Object} The object returned from a <read> call to this
  58. * format.
  59. * config - {Object} Configuration properties for the layer. Defaults for
  60. * the layer will apply if not provided.
  61. *
  62. * Required config properties:
  63. * layer - {String} The layer identifier.
  64. *
  65. * Optional config properties:
  66. * matrixSet - {String} The matrix set identifier, required if there is
  67. * more than one matrix set in the layer capabilities.
  68. * style - {String} The name of the style
  69. * format - {String} Image format for the layer. Default is the first
  70. * format returned in the GetCapabilities response.
  71. * param - {Object} The dimensions values eg: {"Year": "2012"}
  72. *
  73. * Returns:
  74. * {<OpenLayers.Layer.WMTS>} A properly configured WMTS layer. Throws an
  75. * error if an incomplete config is provided. Returns undefined if no
  76. * layer could be created with the provided config.
  77. */
  78. createLayer: function(capabilities, config) {
  79. var layer;
  80. // confirm required properties are supplied in config
  81. if (!('layer' in config)) {
  82. throw new Error("Missing property 'layer' in configuration.");
  83. }
  84. var contents = capabilities.contents;
  85. // find the layer definition with the given identifier
  86. var layers = contents.layers;
  87. var layerDef;
  88. for (var i=0, ii=contents.layers.length; i<ii; ++i) {
  89. if (contents.layers[i].identifier === config.layer) {
  90. layerDef = contents.layers[i];
  91. break;
  92. }
  93. }
  94. if (!layerDef) {
  95. throw new Error("Layer not found");
  96. }
  97. var format = config.format;
  98. if (!format && layerDef.formats && layerDef.formats.length) {
  99. format = layerDef.formats[0];
  100. }
  101. // find the matrixSet definition
  102. var matrixSet;
  103. if (config.matrixSet) {
  104. matrixSet = contents.tileMatrixSets[config.matrixSet];
  105. } else if (layerDef.tileMatrixSetLinks.length >= 1) {
  106. matrixSet = contents.tileMatrixSets[
  107. layerDef.tileMatrixSetLinks[0].tileMatrixSet];
  108. }
  109. if (!matrixSet) {
  110. throw new Error("matrixSet not found");
  111. }
  112. // get the default style for the layer
  113. var style;
  114. for (var i=0, ii=layerDef.styles.length; i<ii; ++i) {
  115. style = layerDef.styles[i];
  116. if (style.isDefault) {
  117. break;
  118. }
  119. }
  120. var requestEncoding = config.requestEncoding;
  121. if (!requestEncoding) {
  122. requestEncoding = "KVP";
  123. if (capabilities.operationsMetadata.GetTile.dcp.http) {
  124. var http = capabilities.operationsMetadata.GetTile.dcp.http;
  125. // Get first get method
  126. if (http.get[0].constraints) {
  127. var constraints = http.get[0].constraints;
  128. var allowedValues = constraints.GetEncoding.allowedValues;
  129. // The OGC documentation is not clear if we should use
  130. // REST or RESTful, ArcGis use RESTful,
  131. // and OpenLayers use REST.
  132. if (!allowedValues.KVP &&
  133. (allowedValues.REST || allowedValues.RESTful)) {
  134. requestEncoding = "REST";
  135. }
  136. }
  137. }
  138. }
  139. var dimensions = [];
  140. var params = config.params || {};
  141. // to don't overwrite the changes in the applyDefaults
  142. delete config.params;
  143. for (var id = 0, ld = layerDef.dimensions.length ; id < ld ; id++) {
  144. var dimension = layerDef.dimensions[id];
  145. dimensions.push(dimension.identifier);
  146. if (!params.hasOwnProperty(dimension.identifier)) {
  147. params[dimension.identifier] = dimension['default'];
  148. }
  149. }
  150. var projection = config.projection || matrixSet.supportedCRS.replace(
  151. /urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, "$1:$3");
  152. var units = config.units ||
  153. (projection === "EPSG:4326" ? "degrees" : "m");
  154. var resolutions = [];
  155. for (var mid in matrixSet.matrixIds) {
  156. if (matrixSet.matrixIds.hasOwnProperty(mid)) {
  157. resolutions.push(
  158. matrixSet.matrixIds[mid].scaleDenominator * 0.28E-3 /
  159. OpenLayers.METERS_PER_INCH /
  160. OpenLayers.INCHES_PER_UNIT[units]);
  161. }
  162. }
  163. var url;
  164. if (requestEncoding === "REST" && layerDef.resourceUrls) {
  165. url = [];
  166. var resourceUrls = layerDef.resourceUrls,
  167. resourceUrl;
  168. for (var t = 0, tt = layerDef.resourceUrls.length; t < tt; ++t) {
  169. resourceUrl = layerDef.resourceUrls[t];
  170. if (resourceUrl.format === format && resourceUrl.resourceType === "tile") {
  171. url.push(resourceUrl.template);
  172. }
  173. }
  174. }
  175. else {
  176. var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get;
  177. url = [];
  178. var constraint;
  179. for (var i = 0, ii = httpGet.length; i < ii; i++) {
  180. constraint = httpGet[i].constraints;
  181. if (!constraint || (constraint && constraint.
  182. GetEncoding.allowedValues[requestEncoding])) {
  183. url.push(httpGet[i].url);
  184. }
  185. }
  186. }
  187. return new OpenLayers.Layer.WMTS(
  188. OpenLayers.Util.applyDefaults(config, {
  189. url: url,
  190. requestEncoding: requestEncoding,
  191. name: layerDef.title,
  192. style: style.identifier,
  193. format: format,
  194. matrixIds: matrixSet.matrixIds,
  195. matrixSet: matrixSet.identifier,
  196. projection: projection,
  197. units: units,
  198. resolutions: config.isBaseLayer === false ? undefined :
  199. resolutions,
  200. serverResolutions: resolutions,
  201. tileFullExtent: matrixSet.bounds,
  202. dimensions: dimensions,
  203. params: params
  204. })
  205. );
  206. },
  207. CLASS_NAME: "OpenLayers.Format.WMTSCapabilities"
  208. });