TMS.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/Layer/Grid.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.TMS
  10. * Create a layer for accessing tiles from services that conform with the
  11. * Tile Map Service Specification
  12. * (http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification).
  13. *
  14. * Example:
  15. * (code)
  16. * var layer = new OpenLayers.Layer.TMS(
  17. * "My Layer", // name for display in LayerSwitcher
  18. * "http://tilecache.osgeo.org/wms-c/Basic.py/", // service endpoint
  19. * {layername: "basic", type: "png"} // required properties
  20. * );
  21. * (end)
  22. *
  23. * Inherits from:
  24. * - <OpenLayers.Layer.Grid>
  25. */
  26. OpenLayers.Layer.TMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
  27. /**
  28. * APIProperty: serviceVersion
  29. * {String} Service version for tile requests. Default is "1.0.0".
  30. */
  31. serviceVersion: "1.0.0",
  32. /**
  33. * APIProperty: layername
  34. * {String} The identifier for the <TileMap> as advertised by the service.
  35. * For example, if the service advertises a <TileMap> with
  36. * 'href="http://tms.osgeo.org/1.0.0/vmap0"', the <layername> property
  37. * would be set to "vmap0".
  38. */
  39. layername: null,
  40. /**
  41. * APIProperty: type
  42. * {String} The format extension corresponding to the requested tile image
  43. * type. This is advertised in a <TileFormat> element as the
  44. * "extension" attribute. For example, if the service advertises a
  45. * <TileMap> with <TileFormat width="256" height="256" mime-type="image/jpeg" extension="jpg" />,
  46. * the <type> property would be set to "jpg".
  47. */
  48. type: null,
  49. /**
  50. * APIProperty: isBaseLayer
  51. * {Boolean} Make this layer a base layer. Default is true. Set false to
  52. * use the layer as an overlay.
  53. */
  54. isBaseLayer: true,
  55. /**
  56. * APIProperty: tileOrigin
  57. * {<OpenLayers.LonLat>} Optional origin for aligning the grid of tiles.
  58. * If provided, requests for tiles at all resolutions will be aligned
  59. * with this location (no tiles shall overlap this location). If
  60. * not provided, the grid of tiles will be aligned with the bottom-left
  61. * corner of the map's <maxExtent>. Default is ``null``.
  62. *
  63. * Example:
  64. * (code)
  65. * var layer = new OpenLayers.Layer.TMS(
  66. * "My Layer",
  67. * "http://tilecache.osgeo.org/wms-c/Basic.py/",
  68. * {
  69. * layername: "basic",
  70. * type: "png",
  71. * // set if different than the bottom left of map.maxExtent
  72. * tileOrigin: new OpenLayers.LonLat(-180, -90)
  73. * }
  74. * );
  75. * (end)
  76. */
  77. tileOrigin: null,
  78. /**
  79. * APIProperty: serverResolutions
  80. * {Array} A list of all resolutions available on the server. Only set this
  81. * property if the map resolutions differ from the server. This
  82. * property serves two purposes. (a) <serverResolutions> can include
  83. * resolutions that the server supports and that you don't want to
  84. * provide with this layer; you can also look at <zoomOffset>, which is
  85. * an alternative to <serverResolutions> for that specific purpose.
  86. * (b) The map can work with resolutions that aren't supported by
  87. * the server, i.e. that aren't in <serverResolutions>. When the
  88. * map is displayed in such a resolution data for the closest
  89. * server-supported resolution is loaded and the layer div is
  90. * stretched as necessary.
  91. */
  92. serverResolutions: null,
  93. /**
  94. * APIProperty: zoomOffset
  95. * {Number} If your cache has more zoom levels than you want to provide
  96. * access to with this layer, supply a zoomOffset. This zoom offset
  97. * is added to the current map zoom level to determine the level
  98. * for a requested tile. For example, if you supply a zoomOffset
  99. * of 3, when the map is at the zoom 0, tiles will be requested from
  100. * level 3 of your cache. Default is 0 (assumes cache level and map
  101. * zoom are equivalent). Using <zoomOffset> is an alternative to
  102. * setting <serverResolutions> if you only want to expose a subset
  103. * of the server resolutions.
  104. */
  105. zoomOffset: 0,
  106. /**
  107. * Constructor: OpenLayers.Layer.TMS
  108. *
  109. * Parameters:
  110. * name - {String} Title to be displayed in a <OpenLayers.Control.LayerSwitcher>
  111. * url - {String} Service endpoint (without the version number). E.g.
  112. * "http://tms.osgeo.org/".
  113. * options - {Object} Additional properties to be set on the layer. The
  114. * <layername> and <type> properties must be set here.
  115. */
  116. initialize: function(name, url, options) {
  117. var newArguments = [];
  118. newArguments.push(name, url, {}, options);
  119. OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
  120. },
  121. /**
  122. * APIMethod: clone
  123. * Create a complete copy of this layer.
  124. *
  125. * Parameters:
  126. * obj - {Object} Should only be provided by subclasses that call this
  127. * method.
  128. *
  129. * Returns:
  130. * {<OpenLayers.Layer.TMS>} An exact clone of this <OpenLayers.Layer.TMS>
  131. */
  132. clone: function (obj) {
  133. if (obj == null) {
  134. obj = new OpenLayers.Layer.TMS(this.name,
  135. this.url,
  136. this.getOptions());
  137. }
  138. //get all additions from superclasses
  139. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  140. // copy/set any non-init, non-simple values here
  141. return obj;
  142. },
  143. /**
  144. * Method: getURL
  145. *
  146. * Parameters:
  147. * bounds - {<OpenLayers.Bounds>}
  148. *
  149. * Returns:
  150. * {String} A string with the layer's url and parameters and also the
  151. * passed-in bounds and appropriate tile size specified as
  152. * parameters
  153. */
  154. getURL: function (bounds) {
  155. bounds = this.adjustBounds(bounds);
  156. var res = this.getServerResolution();
  157. var x = Math.round((bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w));
  158. var y = Math.round((bounds.bottom - this.tileOrigin.lat) / (res * this.tileSize.h));
  159. var z = this.getServerZoom();
  160. var path = this.serviceVersion + "/" + this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;
  161. var url = this.url;
  162. if (OpenLayers.Util.isArray(url)) {
  163. url = this.selectUrl(path, url);
  164. }
  165. return url + path;
  166. },
  167. /**
  168. * Method: setMap
  169. * When the layer is added to a map, then we can fetch our origin
  170. * (if we don't have one.)
  171. *
  172. * Parameters:
  173. * map - {<OpenLayers.Map>}
  174. */
  175. setMap: function(map) {
  176. OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
  177. if (!this.tileOrigin) {
  178. this.tileOrigin = new OpenLayers.LonLat(this.map.maxExtent.left,
  179. this.map.maxExtent.bottom);
  180. }
  181. },
  182. CLASS_NAME: "OpenLayers.Layer.TMS"
  183. });