XYZ.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.XYZ
  10. * The XYZ class is designed to make it easier for people who have tiles
  11. * arranged by a standard XYZ grid.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Layer.Grid>
  15. */
  16. OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
  17. /**
  18. * APIProperty: isBaseLayer
  19. * Default is true, as this is designed to be a base tile source.
  20. */
  21. isBaseLayer: true,
  22. /**
  23. * APIProperty: sphericalMercator
  24. * Whether the tile extents should be set to the defaults for
  25. * spherical mercator. Useful for things like OpenStreetMap.
  26. * Default is false, except for the OSM subclass.
  27. */
  28. sphericalMercator: false,
  29. /**
  30. * APIProperty: zoomOffset
  31. * {Number} If your cache has more zoom levels than you want to provide
  32. * access to with this layer, supply a zoomOffset. This zoom offset
  33. * is added to the current map zoom level to determine the level
  34. * for a requested tile. For example, if you supply a zoomOffset
  35. * of 3, when the map is at the zoom 0, tiles will be requested from
  36. * level 3 of your cache. Default is 0 (assumes cache level and map
  37. * zoom are equivalent). Using <zoomOffset> is an alternative to
  38. * setting <serverResolutions> if you only want to expose a subset
  39. * of the server resolutions.
  40. */
  41. zoomOffset: 0,
  42. /**
  43. * APIProperty: serverResolutions
  44. * {Array} A list of all resolutions available on the server. Only set this
  45. * property if the map resolutions differ from the server. This
  46. * property serves two purposes. (a) <serverResolutions> can include
  47. * resolutions that the server supports and that you don't want to
  48. * provide with this layer; you can also look at <zoomOffset>, which is
  49. * an alternative to <serverResolutions> for that specific purpose.
  50. * (b) The map can work with resolutions that aren't supported by
  51. * the server, i.e. that aren't in <serverResolutions>. When the
  52. * map is displayed in such a resolution data for the closest
  53. * server-supported resolution is loaded and the layer div is
  54. * stretched as necessary.
  55. */
  56. serverResolutions: null,
  57. /**
  58. * Constructor: OpenLayers.Layer.XYZ
  59. *
  60. * Parameters:
  61. * name - {String}
  62. * url - {String}
  63. * options - {Object} Hashtable of extra options to tag onto the layer
  64. */
  65. initialize: function(name, url, options) {
  66. if (options && options.sphericalMercator || this.sphericalMercator) {
  67. options = OpenLayers.Util.extend({
  68. projection: "EPSG:900913",
  69. numZoomLevels: 19
  70. }, options);
  71. }
  72. OpenLayers.Layer.Grid.prototype.initialize.apply(this, [
  73. name || this.name, url || this.url, {}, options
  74. ]);
  75. },
  76. /**
  77. * APIMethod: clone
  78. * Create a clone of this layer
  79. *
  80. * Parameters:
  81. * obj - {Object} Is this ever used?
  82. *
  83. * Returns:
  84. * {<OpenLayers.Layer.XYZ>} An exact clone of this OpenLayers.Layer.XYZ
  85. */
  86. clone: function (obj) {
  87. if (obj == null) {
  88. obj = new OpenLayers.Layer.XYZ(this.name,
  89. this.url,
  90. this.getOptions());
  91. }
  92. //get all additions from superclasses
  93. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  94. return obj;
  95. },
  96. /**
  97. * Method: getURL
  98. *
  99. * Parameters:
  100. * bounds - {<OpenLayers.Bounds>}
  101. *
  102. * Returns:
  103. * {String} A string with the layer's url and parameters and also the
  104. * passed-in bounds and appropriate tile size specified as
  105. * parameters
  106. */
  107. getURL: function (bounds) {
  108. var xyz = this.getXYZ(bounds);
  109. var url = this.url;
  110. if (OpenLayers.Util.isArray(url)) {
  111. var s = '' + xyz.x + xyz.y + xyz.z;
  112. url = this.selectUrl(s, url);
  113. }
  114. return OpenLayers.String.format(url, xyz);
  115. },
  116. /**
  117. * Method: getXYZ
  118. * Calculates x, y and z for the given bounds.
  119. *
  120. * Parameters:
  121. * bounds - {<OpenLayers.Bounds>}
  122. *
  123. * Returns:
  124. * {Object} - an object with x, y and z properties.
  125. */
  126. getXYZ: function(bounds) {
  127. var res = this.getServerResolution();
  128. var x = Math.round((bounds.left - this.maxExtent.left) /
  129. (res * this.tileSize.w));
  130. var y = Math.round((this.maxExtent.top - bounds.top) /
  131. (res * this.tileSize.h));
  132. var z = this.getServerZoom();
  133. if (this.wrapDateLine) {
  134. var limit = Math.pow(2, z);
  135. x = ((x % limit) + limit) % limit;
  136. }
  137. return {'x': x, 'y': y, 'z': z};
  138. },
  139. /* APIMethod: setMap
  140. * When the layer is added to a map, then we can fetch our origin
  141. * (if we don't have one.)
  142. *
  143. * Parameters:
  144. * map - {<OpenLayers.Map>}
  145. */
  146. setMap: function(map) {
  147. OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
  148. if (!this.tileOrigin) {
  149. this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
  150. this.maxExtent.bottom);
  151. }
  152. },
  153. CLASS_NAME: "OpenLayers.Layer.XYZ"
  154. });