TileCache.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.TileCache
  10. * A read only TileCache layer. Used to requests tiles cached by TileCache in
  11. * a web accessible cache. This means that you have to pre-populate your
  12. * cache before this layer can be used. It is meant only to read tiles
  13. * created by TileCache, and not to make calls to TileCache for tile
  14. * creation. Create a new instance with the
  15. * <OpenLayers.Layer.TileCache> constructor.
  16. *
  17. * Inherits from:
  18. * - <OpenLayers.Layer.Grid>
  19. */
  20. OpenLayers.Layer.TileCache = OpenLayers.Class(OpenLayers.Layer.Grid, {
  21. /**
  22. * APIProperty: isBaseLayer
  23. * {Boolean} Treat this layer as a base layer. Default is true.
  24. */
  25. isBaseLayer: true,
  26. /**
  27. * APIProperty: format
  28. * {String} Mime type of the images returned. Default is image/png.
  29. */
  30. format: 'image/png',
  31. /**
  32. * APIProperty: serverResolutions
  33. * {Array} A list of all resolutions available on the server. Only set this
  34. * property if the map resolutions differ from the server. This
  35. * property serves two purposes. (a) <serverResolutions> can include
  36. * resolutions that the server supports and that you don't want to
  37. * provide with this layer. (b) The map can work with resolutions
  38. * that aren't supported by the server, i.e. that aren't in
  39. * <serverResolutions>. When the map is displayed in such a resolution
  40. * data for the closest server-supported resolution is loaded and the
  41. * layer div is stretched as necessary.
  42. */
  43. serverResolutions: null,
  44. /**
  45. * Constructor: OpenLayers.Layer.TileCache
  46. * Create a new read only TileCache layer.
  47. *
  48. * Parameters:
  49. * name - {String} Name of the layer displayed in the interface
  50. * url - {String} Location of the web accessible cache (not the location of
  51. * your tilecache script!)
  52. * layername - {String} Layer name as defined in the TileCache
  53. * configuration
  54. * options - {Object} Optional object with properties to be set on the
  55. * layer. Note that you should speficy your resolutions to match
  56. * your TileCache configuration. This can be done by setting
  57. * the resolutions array directly (here or on the map), by setting
  58. * maxResolution and numZoomLevels, or by using scale based properties.
  59. */
  60. initialize: function(name, url, layername, options) {
  61. this.layername = layername;
  62. OpenLayers.Layer.Grid.prototype.initialize.apply(this,
  63. [name, url, {}, options]);
  64. this.extension = this.format.split('/')[1].toLowerCase();
  65. this.extension = (this.extension == 'jpg') ? 'jpeg' : this.extension;
  66. },
  67. /**
  68. * APIMethod: clone
  69. * obj - {Object}
  70. *
  71. * Returns:
  72. * {<OpenLayers.Layer.TileCache>} An exact clone of this
  73. * <OpenLayers.Layer.TileCache>
  74. */
  75. clone: function (obj) {
  76. if (obj == null) {
  77. obj = new OpenLayers.Layer.TileCache(this.name,
  78. this.url,
  79. this.layername,
  80. this.getOptions());
  81. }
  82. //get all additions from superclasses
  83. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  84. // copy/set any non-init, non-simple values here
  85. return obj;
  86. },
  87. /**
  88. * Method: getURL
  89. *
  90. * Parameters:
  91. * bounds - {<OpenLayers.Bounds>}
  92. *
  93. * Returns:
  94. * {String} A string with the layer's url and parameters and also the
  95. * passed-in bounds and appropriate tile size specified as parameters.
  96. */
  97. getURL: function(bounds) {
  98. var res = this.getServerResolution();
  99. var bbox = this.maxExtent;
  100. var size = this.tileSize;
  101. var tileX = Math.round((bounds.left - bbox.left) / (res * size.w));
  102. var tileY = Math.round((bounds.bottom - bbox.bottom) / (res * size.h));
  103. var tileZ = this.serverResolutions != null ?
  104. OpenLayers.Util.indexOf(this.serverResolutions, res) :
  105. this.map.getZoom();
  106. var components = [
  107. this.layername,
  108. OpenLayers.Number.zeroPad(tileZ, 2),
  109. OpenLayers.Number.zeroPad(parseInt(tileX / 1000000), 3),
  110. OpenLayers.Number.zeroPad((parseInt(tileX / 1000) % 1000), 3),
  111. OpenLayers.Number.zeroPad((parseInt(tileX) % 1000), 3),
  112. OpenLayers.Number.zeroPad(parseInt(tileY / 1000000), 3),
  113. OpenLayers.Number.zeroPad((parseInt(tileY / 1000) % 1000), 3),
  114. OpenLayers.Number.zeroPad((parseInt(tileY) % 1000), 3) + '.' + this.extension
  115. ];
  116. var path = components.join('/');
  117. var url = this.url;
  118. if (OpenLayers.Util.isArray(url)) {
  119. url = this.selectUrl(path, url);
  120. }
  121. url = (url.charAt(url.length - 1) == '/') ? url : url + '/';
  122. return url + path;
  123. },
  124. CLASS_NAME: "OpenLayers.Layer.TileCache"
  125. });