KaMapCache.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/Layer/KaMap.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.KaMapCache
  11. *
  12. * This class is designed to talk directly to a web-accessible ka-Map
  13. * cache generated by the precache2.php script.
  14. *
  15. * To create a a new KaMapCache layer, you must indicate also the "i" parameter
  16. * (that will be used to calculate the file extension), and another special
  17. * parameter, object names "metaTileSize", with "h" (height) and "w" (width)
  18. * properties.
  19. *
  20. * // Create a new kaMapCache layer.
  21. * var kamap_base = new OpenLayers.Layer.KaMapCache(
  22. * "Satellite",
  23. * "http://www.example.org/web/acessible/cache",
  24. * {g: "satellite", map: "world", i: 'png24', metaTileSize: {w: 5, h: 5} }
  25. * );
  26. *
  27. * // Create an kaMapCache overlay layer (using "isBaseLayer: false").
  28. * // Forces the output to be a "gif", using the "i" parameter.
  29. * var kamap_overlay = new OpenLayers.Layer.KaMapCache(
  30. * "Streets",
  31. * "http://www.example.org/web/acessible/cache",
  32. * {g: "streets", map: "world", i: "gif", metaTileSize: {w: 5, h: 5} },
  33. * {isBaseLayer: false}
  34. * );
  35. *
  36. * The cache URLs must look like:
  37. * var/cache/World/50000/Group_Name/def/t-440320/l20480
  38. *
  39. * This means that the cache generated via tile.php will *not* work with
  40. * this class, and should instead use the KaMap layer.
  41. *
  42. * More information is available in Ticket #1518.
  43. *
  44. * Inherits from:
  45. * - <OpenLayers.Layer.KaMap>
  46. * - <OpenLayers.Layer.Grid>
  47. */
  48. OpenLayers.Layer.KaMapCache = OpenLayers.Class(OpenLayers.Layer.KaMap, {
  49. /**
  50. * Constant: IMAGE_EXTENSIONS
  51. * {Object} Simple hash map to convert format to extension.
  52. */
  53. IMAGE_EXTENSIONS: {
  54. 'jpeg': 'jpg',
  55. 'gif' : 'gif',
  56. 'png' : 'png',
  57. 'png8' : 'png',
  58. 'png24' : 'png',
  59. 'dithered' : 'png'
  60. },
  61. /**
  62. * Constant: DEFAULT_FORMAT
  63. * {Object} Simple hash map to convert format to extension.
  64. */
  65. DEFAULT_FORMAT: 'jpeg',
  66. /**
  67. * Constructor: OpenLayers.Layer.KaMapCache
  68. *
  69. * Parameters:
  70. * name - {String}
  71. * url - {String}
  72. * params - {Object} Parameters to be sent to the HTTP server in the
  73. * query string for the tile. The format can be set via the 'i'
  74. * parameter (defaults to jpg) , and the map should be set via
  75. * the 'map' parameter. It has been reported that ka-Map may behave
  76. * inconsistently if your format parameter does not match the format
  77. * parameter configured in your config.php. (See ticket #327 for more
  78. * information.)
  79. * options - {Object} Additional options for the layer. Any of the
  80. * APIProperties listed on this layer, and any layer types it
  81. * extends, can be overridden through the options parameter.
  82. */
  83. initialize: function(name, url, params, options) {
  84. OpenLayers.Layer.KaMap.prototype.initialize.apply(this, arguments);
  85. this.extension = this.IMAGE_EXTENSIONS[this.params.i.toLowerCase() || DEFAULT_FORMAT];
  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
  96. * parameters
  97. */
  98. getURL: function (bounds) {
  99. bounds = this.adjustBounds(bounds);
  100. var mapRes = this.map.getResolution();
  101. var scale = Math.round((this.map.getScale() * 10000)) / 10000;
  102. var pX = Math.round(bounds.left / mapRes);
  103. var pY = -Math.round(bounds.top / mapRes);
  104. var metaX = Math.floor(pX / this.tileSize.w / this.params.metaTileSize.w) * this.tileSize.w * this.params.metaTileSize.w;
  105. var metaY = Math.floor(pY / this.tileSize.h / this.params.metaTileSize.h) * this.tileSize.h * this.params.metaTileSize.h;
  106. // if url is not a string, it should be an array of strings,
  107. // in which case we will deterministically select one of them in
  108. // order to evenly distribute requests to different urls.
  109. //
  110. var url = this.url;
  111. if (OpenLayers.Util.isArray(url)) {
  112. url = this.selectUrl(paramsString, url);
  113. }
  114. var components = [
  115. url,
  116. "/",
  117. this.params.map,
  118. "/",
  119. scale,
  120. "/",
  121. this.params.g.replace(/\s/g, '_'),
  122. "/def/t",
  123. metaY,
  124. "/l",
  125. metaX,
  126. "/t",
  127. pY,
  128. "l",
  129. pX,
  130. ".",
  131. this.extension
  132. ];
  133. return components.join("");
  134. },
  135. CLASS_NAME: "OpenLayers.Layer.KaMapCache"
  136. });