WMTS.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Layer/Grid.js
  7. * @requires OpenLayers/Tile/Image.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.WMTS
  11. * Instances of the WMTS class allow viewing of tiles from a service that
  12. * implements the OGC WMTS specification version 1.0.0.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Layer.Grid>
  16. */
  17. OpenLayers.Layer.WMTS = OpenLayers.Class(OpenLayers.Layer.Grid, {
  18. /**
  19. * APIProperty: isBaseLayer
  20. * {Boolean} The layer will be considered a base layer. Default is true.
  21. */
  22. isBaseLayer: true,
  23. /**
  24. * Property: version
  25. * {String} WMTS version. Default is "1.0.0".
  26. */
  27. version: "1.0.0",
  28. /**
  29. * APIProperty: requestEncoding
  30. * {String} Request encoding. Can be "REST" or "KVP". Default is "KVP".
  31. */
  32. requestEncoding: "KVP",
  33. /**
  34. * APIProperty: url
  35. * {String} The base URL for the WMTS service. Must be provided.
  36. */
  37. url: null,
  38. /**
  39. * APIProperty: layer
  40. * {String} The layer identifier advertised by the WMTS service. Must be
  41. * provided.
  42. */
  43. layer: null,
  44. /**
  45. * APIProperty: matrixSet
  46. * {String} One of the advertised matrix set identifiers. Must be provided.
  47. */
  48. matrixSet: null,
  49. /**
  50. * APIProperty: style
  51. * {String} One of the advertised layer styles. Must be provided.
  52. */
  53. style: null,
  54. /**
  55. * APIProperty: format
  56. * {String} The image MIME type. Default is "image/jpeg".
  57. */
  58. format: "image/jpeg",
  59. /**
  60. * APIProperty: tileOrigin
  61. * {<OpenLayers.LonLat>} The top-left corner of the tile matrix in map
  62. * units. If the tile origin for each matrix in a set is different,
  63. * the <matrixIds> should include a topLeftCorner property. If
  64. * not provided, the tile origin will default to the top left corner
  65. * of the layer <maxExtent>.
  66. */
  67. tileOrigin: null,
  68. /**
  69. * APIProperty: tileFullExtent
  70. * {<OpenLayers.Bounds>} The full extent of the tile set. If not supplied,
  71. * the layer's <maxExtent> property will be used.
  72. */
  73. tileFullExtent: null,
  74. /**
  75. * APIProperty: formatSuffix
  76. * {String} For REST request encoding, an image format suffix must be
  77. * included in the request. If not provided, the suffix will be derived
  78. * from the <format> property.
  79. */
  80. formatSuffix: null,
  81. /**
  82. * APIProperty: matrixIds
  83. * {Array} A list of tile matrix identifiers. If not provided, the matrix
  84. * identifiers will be assumed to be integers corresponding to the
  85. * map zoom level. If a list of strings is provided, each item should
  86. * be the matrix identifier that corresponds to the map zoom level.
  87. * Additionally, a list of objects can be provided. Each object should
  88. * describe the matrix as presented in the WMTS capabilities. These
  89. * objects should have the propertes shown below.
  90. *
  91. * Matrix properties:
  92. * identifier - {String} The matrix identifier (required).
  93. * topLeftCorner - {<OpenLayers.LonLat>} The top left corner of the
  94. * matrix. Must be provided if different than the layer <tileOrigin>.
  95. * tileWidth - {Number} The tile width for the matrix. Must be provided
  96. * if different than the width given in the layer <tileSize>.
  97. * tileHeight - {Number} The tile height for the matrix. Must be provided
  98. * if different than the height given in the layer <tileSize>.
  99. */
  100. matrixIds: null,
  101. /**
  102. * APIProperty: dimensions
  103. * {Array} For RESTful request encoding, extra dimensions may be specified.
  104. * Items in this list should be property names in the <params> object.
  105. * Values of extra dimensions will be determined from the corresponding
  106. * values in the <params> object.
  107. */
  108. dimensions: null,
  109. /**
  110. * APIProperty: params
  111. * {Object} Extra parameters to include in tile requests. For KVP
  112. * <requestEncoding>, these properties will be encoded in the request
  113. * query string. For REST <requestEncoding>, these properties will
  114. * become part of the request path, with order determined by the
  115. * <dimensions> list.
  116. */
  117. params: null,
  118. /**
  119. * APIProperty: zoomOffset
  120. * {Number} If your cache has more levels than you want to provide
  121. * access to with this layer, supply a zoomOffset. This zoom offset
  122. * is added to the current map zoom level to determine the level
  123. * for a requested tile. For example, if you supply a zoomOffset
  124. * of 3, when the map is at the zoom 0, tiles will be requested from
  125. * level 3 of your cache. Default is 0 (assumes cache level and map
  126. * zoom are equivalent). Additionally, if this layer is to be used
  127. * as an overlay and the cache has fewer zoom levels than the base
  128. * layer, you can supply a negative zoomOffset. For example, if a
  129. * map zoom level of 1 corresponds to your cache level zero, you would
  130. * supply a -1 zoomOffset (and set the maxResolution of the layer
  131. * appropriately). The zoomOffset value has no effect if complete
  132. * matrix definitions (including scaleDenominator) are supplied in
  133. * the <matrixIds> property. Defaults to 0 (no zoom offset).
  134. */
  135. zoomOffset: 0,
  136. /**
  137. * Property: formatSuffixMap
  138. * {Object} a map between WMTS 'format' request parameter and tile image file suffix
  139. */
  140. formatSuffixMap: {
  141. "image/png": "png",
  142. "image/png8": "png",
  143. "image/png24": "png",
  144. "image/png32": "png",
  145. "png": "png",
  146. "image/jpeg": "jpg",
  147. "image/jpg": "jpg",
  148. "jpeg": "jpg",
  149. "jpg": "jpg"
  150. },
  151. /**
  152. * Property: matrix
  153. * {Object} Matrix definition for the current map resolution. Updated by
  154. * the <updateMatrixProperties> method.
  155. */
  156. matrix: null,
  157. /**
  158. * Constructor: OpenLayers.Layer.WMTS
  159. * Create a new WMTS layer.
  160. *
  161. * Example:
  162. * (code)
  163. * var wmts = new OpenLayers.Layer.WMTS({
  164. * name: "My WMTS Layer",
  165. * url: "http://example.com/wmts",
  166. * layer: "layer_id",
  167. * style: "default",
  168. * matrixSet: "matrix_id"
  169. * });
  170. * (end)
  171. *
  172. * Parameters:
  173. * config - {Object} Configuration properties for the layer.
  174. *
  175. * Required configuration properties:
  176. * url - {String} The base url for the service. See the <url> property.
  177. * layer - {String} The layer identifier. See the <layer> property.
  178. * style - {String} The layer style identifier. See the <style> property.
  179. * matrixSet - {String} The tile matrix set identifier. See the <matrixSet>
  180. * property.
  181. *
  182. * Any other documented layer properties can be provided in the config object.
  183. */
  184. initialize: function(config) {
  185. // confirm required properties are supplied
  186. var required = {
  187. url: true,
  188. layer: true,
  189. style: true,
  190. matrixSet: true
  191. };
  192. for (var prop in required) {
  193. if (!(prop in config)) {
  194. throw new Error("Missing property '" + prop + "' in layer configuration.");
  195. }
  196. }
  197. config.params = OpenLayers.Util.upperCaseObject(config.params);
  198. var args = [config.name, config.url, config.params, config];
  199. OpenLayers.Layer.Grid.prototype.initialize.apply(this, args);
  200. // determine format suffix (for REST)
  201. if (!this.formatSuffix) {
  202. this.formatSuffix = this.formatSuffixMap[this.format] || this.format.split("/").pop();
  203. }
  204. // expand matrixIds (may be array of string or array of object)
  205. if (this.matrixIds) {
  206. var len = this.matrixIds.length;
  207. if (len && typeof this.matrixIds[0] === "string") {
  208. var ids = this.matrixIds;
  209. this.matrixIds = new Array(len);
  210. for (var i=0; i<len; ++i) {
  211. this.matrixIds[i] = {identifier: ids[i]};
  212. }
  213. }
  214. }
  215. },
  216. /**
  217. * Method: setMap
  218. */
  219. setMap: function() {
  220. OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
  221. this.updateMatrixProperties();
  222. },
  223. /**
  224. * Method: updateMatrixProperties
  225. * Called when map resolution changes to update matrix related properties.
  226. */
  227. updateMatrixProperties: function() {
  228. this.matrix = this.getMatrix();
  229. if (this.matrix) {
  230. if (this.matrix.topLeftCorner) {
  231. this.tileOrigin = this.matrix.topLeftCorner;
  232. }
  233. if (this.matrix.tileWidth && this.matrix.tileHeight) {
  234. this.tileSize = new OpenLayers.Size(
  235. this.matrix.tileWidth, this.matrix.tileHeight
  236. );
  237. }
  238. if (!this.tileOrigin) {
  239. this.tileOrigin = new OpenLayers.LonLat(
  240. this.maxExtent.left, this.maxExtent.top
  241. );
  242. }
  243. if (!this.tileFullExtent) {
  244. this.tileFullExtent = this.maxExtent;
  245. }
  246. }
  247. },
  248. /**
  249. * Method: moveTo
  250. *
  251. * Parameters:
  252. * bound - {<OpenLayers.Bounds>}
  253. * zoomChanged - {Boolean} Tells when zoom has changed, as layers have to
  254. * do some init work in that case.
  255. * dragging - {Boolean}
  256. */
  257. moveTo:function(bounds, zoomChanged, dragging) {
  258. if (zoomChanged || !this.matrix) {
  259. this.updateMatrixProperties();
  260. }
  261. return OpenLayers.Layer.Grid.prototype.moveTo.apply(this, arguments);
  262. },
  263. /**
  264. * APIMethod: clone
  265. *
  266. * Parameters:
  267. * obj - {Object}
  268. *
  269. * Returns:
  270. * {<OpenLayers.Layer.WMTS>} An exact clone of this <OpenLayers.Layer.WMTS>
  271. */
  272. clone: function(obj) {
  273. if (obj == null) {
  274. obj = new OpenLayers.Layer.WMTS(this.options);
  275. }
  276. //get all additions from superclasses
  277. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  278. // copy/set any non-init, non-simple values here
  279. return obj;
  280. },
  281. /**
  282. * Method: getMatrix
  283. * Get the appropriate matrix definition for the current map resolution.
  284. */
  285. getMatrix: function() {
  286. var matrix;
  287. if (!this.matrixIds || this.matrixIds.length === 0) {
  288. matrix = {identifier: this.map.getZoom() + this.zoomOffset};
  289. } else {
  290. // get appropriate matrix given the map scale if possible
  291. if ("scaleDenominator" in this.matrixIds[0]) {
  292. // scale denominator calculation based on WMTS spec
  293. var denom =
  294. OpenLayers.METERS_PER_INCH *
  295. OpenLayers.INCHES_PER_UNIT[this.units] *
  296. this.map.getResolution() / 0.28E-3;
  297. var diff = Number.POSITIVE_INFINITY;
  298. var delta;
  299. for (var i=0, ii=this.matrixIds.length; i<ii; ++i) {
  300. delta = Math.abs(1 - (this.matrixIds[i].scaleDenominator / denom));
  301. if (delta < diff) {
  302. diff = delta;
  303. matrix = this.matrixIds[i];
  304. }
  305. }
  306. } else {
  307. // fall back on zoom as index
  308. matrix = this.matrixIds[this.map.getZoom() + this.zoomOffset];
  309. }
  310. }
  311. return matrix;
  312. },
  313. /**
  314. * Method: getTileInfo
  315. * Get tile information for a given location at the current map resolution.
  316. *
  317. * Parameters:
  318. * loc - {<OpenLayers.LonLat} A location in map coordinates.
  319. *
  320. * Returns:
  321. * {Object} An object with "col", "row", "i", and "j" properties. The col
  322. * and row values are zero based tile indexes from the top left. The
  323. * i and j values are the number of pixels to the left and top
  324. * (respectively) of the given location within the target tile.
  325. */
  326. getTileInfo: function(loc) {
  327. var res = this.map.getResolution();
  328. var fx = (loc.lon - this.tileOrigin.lon) / (res * this.tileSize.w);
  329. var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h);
  330. var col = Math.floor(fx);
  331. var row = Math.floor(fy);
  332. return {
  333. col: col,
  334. row: row,
  335. i: Math.floor((fx - col) * this.tileSize.w),
  336. j: Math.floor((fy - row) * this.tileSize.h)
  337. };
  338. },
  339. /**
  340. * Method: getURL
  341. *
  342. * Parameters:
  343. * bounds - {<OpenLayers.Bounds>}
  344. *
  345. * Returns:
  346. * {String} A URL for the tile corresponding to the given bounds.
  347. */
  348. getURL: function(bounds) {
  349. bounds = this.adjustBounds(bounds);
  350. var url = "";
  351. if (!this.tileFullExtent || this.tileFullExtent.intersectsBounds(bounds)) {
  352. var center = bounds.getCenterLonLat();
  353. var info = this.getTileInfo(center);
  354. var matrixId = this.matrix.identifier;
  355. if (this.requestEncoding.toUpperCase() === "REST") {
  356. // include 'version', 'layer' and 'style' in tile resource url
  357. var path = this.version + "/" + this.layer + "/" + this.style + "/";
  358. // append optional dimension path elements
  359. if (this.dimensions) {
  360. for (var i=0; i<this.dimensions.length; i++) {
  361. if (this.params[this.dimensions[i]]) {
  362. path = path + this.params[this.dimensions[i]] + "/";
  363. }
  364. }
  365. }
  366. // append other required path elements
  367. path = path + this.matrixSet + "/" + this.matrix.identifier +
  368. "/" + info.row + "/" + info.col + "." + this.formatSuffix;
  369. if (OpenLayers.Util.isArray(this.url)) {
  370. url = this.selectUrl(path, this.url);
  371. } else {
  372. url = this.url;
  373. }
  374. if (!url.match(/\/$/)) {
  375. url = url + "/";
  376. }
  377. url = url + path;
  378. } else if (this.requestEncoding.toUpperCase() === "KVP") {
  379. // assemble all required parameters
  380. var params = {
  381. SERVICE: "WMTS",
  382. REQUEST: "GetTile",
  383. VERSION: this.version,
  384. LAYER: this.layer,
  385. STYLE: this.style,
  386. TILEMATRIXSET: this.matrixSet,
  387. TILEMATRIX: this.matrix.identifier,
  388. TILEROW: info.row,
  389. TILECOL: info.col,
  390. FORMAT: this.format
  391. };
  392. url = OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(this, [params]);
  393. }
  394. }
  395. return url;
  396. },
  397. /**
  398. * APIMethod: mergeNewParams
  399. * Extend the existing layer <params> with new properties. Tiles will be
  400. * reloaded with updated params in the request.
  401. *
  402. * Parameters:
  403. * newParams - {Object} Properties to extend to existing <params>.
  404. */
  405. mergeNewParams: function(newParams) {
  406. if (this.requestEncoding.toUpperCase() === "KVP") {
  407. return OpenLayers.Layer.Grid.prototype.mergeNewParams.apply(
  408. this, [OpenLayers.Util.upperCaseObject(newParams)]
  409. );
  410. }
  411. },
  412. CLASS_NAME: "OpenLayers.Layer.WMTS"
  413. });