TileCache.js 5.2 KB

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