TMS.js 7.0 KB

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