XYZ.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.XYZ
  11. * The XYZ class is designed to make it easier for people who have tiles
  12. * arranged by a standard XYZ grid.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Layer.Grid>
  16. */
  17. OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
  18. /**
  19. * APIProperty: isBaseLayer
  20. * Default is true, as this is designed to be a base tile source.
  21. */
  22. isBaseLayer: true,
  23. /**
  24. * APIProperty: sphericalMecator
  25. * Whether the tile extents should be set to the defaults for
  26. * spherical mercator. Useful for things like OpenStreetMap.
  27. * Default is false, except for the OSM subclass.
  28. */
  29. sphericalMercator: false,
  30. /**
  31. * APIProperty: zoomOffset
  32. * {Number} If your cache has more zoom levels than you want to provide
  33. * access to with this layer, supply a zoomOffset. This zoom offset
  34. * is added to the current map zoom level to determine the level
  35. * for a requested tile. For example, if you supply a zoomOffset
  36. * of 3, when the map is at the zoom 0, tiles will be requested from
  37. * level 3 of your cache. Default is 0 (assumes cache level and map
  38. * zoom are equivalent). Using <zoomOffset> is an alternative to
  39. * setting <serverResolutions> if you only want to expose a subset
  40. * of the server resolutions.
  41. */
  42. zoomOffset: 0,
  43. /**
  44. * APIProperty: serverResolutions
  45. * {Array} A list of all resolutions available on the server. Only set this
  46. * property if the map resolutions differs from the server.
  47. */
  48. serverResolutions: null,
  49. /**
  50. * Constructor: OpenLayers.Layer.XYZ
  51. *
  52. * Parameters:
  53. * name - {String}
  54. * url - {String}
  55. * options - {Object} Hashtable of extra options to tag onto the layer
  56. */
  57. initialize: function(name, url, options) {
  58. if (options && options.sphericalMercator || this.sphericalMercator) {
  59. options = OpenLayers.Util.extend({
  60. maxExtent: new OpenLayers.Bounds(
  61. -128 * 156543.03390625,
  62. -128 * 156543.03390625,
  63. 128 * 156543.03390625,
  64. 128 * 156543.03390625
  65. ),
  66. maxResolution: 156543.03390625,
  67. numZoomLevels: 19,
  68. units: "m",
  69. projection: "EPSG:900913"
  70. }, options);
  71. }
  72. url = url || this.url;
  73. name = name || this.name;
  74. var newArguments = [name, url, {}, options];
  75. OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
  76. },
  77. /**
  78. * APIMethod: clone
  79. * Create a clone of this layer
  80. *
  81. * Parameters:
  82. * obj - {Object} Is this ever used?
  83. *
  84. * Returns:
  85. * {<OpenLayers.Layer.XYZ>} An exact clone of this OpenLayers.Layer.XYZ
  86. */
  87. clone: function (obj) {
  88. if (obj == null) {
  89. obj = new OpenLayers.Layer.XYZ(this.name,
  90. this.url,
  91. this.getOptions());
  92. }
  93. //get all additions from superclasses
  94. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  95. return obj;
  96. },
  97. /**
  98. * Method: getURL
  99. *
  100. * Parameters:
  101. * bounds - {<OpenLayers.Bounds>}
  102. *
  103. * Returns:
  104. * {String} A string with the layer's url and parameters and also the
  105. * passed-in bounds and appropriate tile size specified as
  106. * parameters
  107. */
  108. getURL: function (bounds) {
  109. var xyz = this.getXYZ(bounds);
  110. var url = this.url;
  111. if (OpenLayers.Util.isArray(url)) {
  112. var s = '' + xyz.x + xyz.y + xyz.z;
  113. url = this.selectUrl(s, url);
  114. }
  115. return OpenLayers.String.format(url, xyz);
  116. },
  117. /**
  118. * Method: getXYZ
  119. * Calculates x, y and z for the given bounds.
  120. *
  121. * Parameters:
  122. * bounds - {<OpenLayers.Bounds>}
  123. *
  124. * Returns:
  125. * {Object} - an object with x, y and z properties.
  126. */
  127. getXYZ: function(bounds) {
  128. var res = this.map.getResolution();
  129. var x = Math.round((bounds.left - this.maxExtent.left) /
  130. (res * this.tileSize.w));
  131. var y = Math.round((this.maxExtent.top - bounds.top) /
  132. (res * this.tileSize.h));
  133. var z = this.serverResolutions != null ?
  134. OpenLayers.Util.indexOf(this.serverResolutions, res) :
  135. this.map.getZoom() + this.zoomOffset;
  136. var limit = Math.pow(2, z);
  137. if (this.wrapDateLine)
  138. {
  139. x = ((x % limit) + limit) % limit;
  140. }
  141. return {'x': x, 'y': y, 'z': z};
  142. },
  143. /* APIMethod: setMap
  144. * When the layer is added to a map, then we can fetch our origin
  145. * (if we don't have one.)
  146. *
  147. * Parameters:
  148. * map - {<OpenLayers.Map>}
  149. */
  150. setMap: function(map) {
  151. OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
  152. if (!this.tileOrigin) {
  153. this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
  154. this.maxExtent.bottom);
  155. }
  156. },
  157. CLASS_NAME: "OpenLayers.Layer.XYZ"
  158. });
  159. /**
  160. * Class: OpenLayers.Layer.OSM
  161. * A class to access OpenStreetMap tiles. By default, uses the OpenStreetMap
  162. * hosted tile.openstreetmap.org 'Mapnik' tileset. If you wish to use
  163. * tiles@home / osmarender layer instead, you can pass a layer like:
  164. *
  165. * (code)
  166. * new OpenLayers.Layer.OSM("t@h",
  167. * "http://tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png");
  168. * (end)
  169. *
  170. * This layer defaults to Spherical Mercator.
  171. *
  172. * Inherits from:
  173. * - <OpenLayers.Layer.XYZ>
  174. */
  175. OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
  176. name: "OpenStreetMap",
  177. attribution: "Data CC-By-SA by <a href='http://openstreetmap.org/'>OpenStreetMap</a>",
  178. sphericalMercator: true,
  179. url: 'http://tile.openstreetmap.org/${z}/${x}/${y}.png',
  180. clone: function(obj) {
  181. if (obj == null) {
  182. obj = new OpenLayers.Layer.OSM(
  183. this.name, this.url, this.getOptions());
  184. }
  185. obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]);
  186. return obj;
  187. },
  188. wrapDateLine: true,
  189. CLASS_NAME: "OpenLayers.Layer.OSM"
  190. });