KaMap.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. */
  8. /**
  9. * Class: OpenLayers.Layer.KaMap
  10. *
  11. * Inherits from:
  12. * - <OpenLayers.Layer.Grid>
  13. */
  14. OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
  15. /**
  16. * APIProperty: isBaseLayer
  17. * {Boolean} KaMap Layer is always a base layer
  18. */
  19. isBaseLayer: true,
  20. /**
  21. * APIProperty: units
  22. * {?}
  23. */
  24. units: null,
  25. /**
  26. * APIProperty: resolution
  27. * {Float}
  28. */
  29. resolution: OpenLayers.DOTS_PER_INCH,
  30. /**
  31. * Constant: DEFAULT_PARAMS
  32. * {Object} parameters set by default. The default parameters set
  33. * the format via the 'i' parameter to 'jpeg'.
  34. */
  35. DEFAULT_PARAMS: {
  36. i: 'jpeg',
  37. map: ''
  38. },
  39. /**
  40. * Constructor: OpenLayers.Layer.KaMap
  41. *
  42. * Parameters:
  43. * name - {String}
  44. * url - {String}
  45. * params - {Object} Parameters to be sent to the HTTP server in the
  46. * query string for the tile. The format can be set via the 'i'
  47. * parameter (defaults to jpg) , and the map should be set via
  48. * the 'map' parameter. It has been reported that ka-Map may behave
  49. * inconsistently if your format parameter does not match the format
  50. * parameter configured in your config.php. (See ticket #327 for more
  51. * information.)
  52. * options - {Object} Additional options for the layer. Any of the
  53. * APIProperties listed on this layer, and any layer types it
  54. * extends, can be overridden through the options parameter.
  55. */
  56. initialize: function(name, url, params, options) {
  57. var newArguments = [];
  58. newArguments.push(name, url, params, options);
  59. OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
  60. this.params = OpenLayers.Util.applyDefaults(
  61. this.params, this.DEFAULT_PARAMS
  62. );
  63. },
  64. /**
  65. * Method: getURL
  66. *
  67. * Parameters:
  68. * bounds - {<OpenLayers.Bounds>}
  69. *
  70. * Returns:
  71. * {String} A string with the layer's url and parameters and also the
  72. * passed-in bounds and appropriate tile size specified as
  73. * parameters
  74. */
  75. getURL: function (bounds) {
  76. bounds = this.adjustBounds(bounds);
  77. var mapRes = this.map.getResolution();
  78. var scale = Math.round((this.map.getScale() * 10000)) / 10000;
  79. var pX = Math.round(bounds.left / mapRes);
  80. var pY = -Math.round(bounds.top / mapRes);
  81. return this.getFullRequestString(
  82. { t: pY,
  83. l: pX,
  84. s: scale
  85. });
  86. },
  87. /**
  88. * Method: calculateGridLayout
  89. * ka-Map uses the center point of the map as an origin for
  90. * its tiles. Override calculateGridLayout to center tiles
  91. * correctly for this case.
  92. *
  93. * Parameters:
  94. * bounds - {<OpenLayers.Bound>}
  95. * origin - {<OpenLayers.LonLat>}
  96. * resolution - {Number}
  97. *
  98. * Returns:
  99. * Object containing properties tilelon, tilelat, tileoffsetlat,
  100. * tileoffsetlat, tileoffsetx, tileoffsety
  101. */
  102. calculateGridLayout: function(bounds, origin, resolution) {
  103. var tilelon = resolution*this.tileSize.w;
  104. var tilelat = resolution*this.tileSize.h;
  105. var offsetlon = bounds.left;
  106. var tilecol = Math.floor(offsetlon/tilelon) - this.buffer;
  107. var tilecolremain = offsetlon/tilelon - tilecol;
  108. var tileoffsetx = -tilecolremain * this.tileSize.w;
  109. var tileoffsetlon = tilecol * tilelon;
  110. var offsetlat = bounds.top;
  111. var tilerow = Math.ceil(offsetlat/tilelat) + this.buffer;
  112. var tilerowremain = tilerow - offsetlat/tilelat;
  113. var tileoffsety = -(tilerowremain+1) * this.tileSize.h;
  114. var tileoffsetlat = tilerow * tilelat;
  115. return {
  116. tilelon: tilelon, tilelat: tilelat,
  117. tileoffsetlon: tileoffsetlon, tileoffsetlat: tileoffsetlat,
  118. tileoffsetx: tileoffsetx, tileoffsety: tileoffsety
  119. };
  120. },
  121. /**
  122. * APIMethod: clone
  123. *
  124. * Parameters:
  125. * obj - {Object}
  126. *
  127. * Returns:
  128. * {<OpenLayers.Layer.Kamap>} An exact clone of this OpenLayers.Layer.KaMap
  129. */
  130. clone: function (obj) {
  131. if (obj == null) {
  132. obj = new OpenLayers.Layer.KaMap(this.name,
  133. this.url,
  134. this.params,
  135. this.getOptions());
  136. }
  137. //get all additions from superclasses
  138. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  139. // copy/set any non-init, non-simple values here
  140. if (this.tileSize != null) {
  141. obj.tileSize = this.tileSize.clone();
  142. }
  143. // we do not want to copy reference to grid, so we make a new array
  144. obj.grid = [];
  145. return obj;
  146. },
  147. /**
  148. * APIMethod: getTileBounds
  149. * Returns The tile bounds for a layer given a pixel location.
  150. *
  151. * Parameters:
  152. * viewPortPx - {<OpenLayers.Pixel>} The location in the viewport.
  153. *
  154. * Returns:
  155. * {<OpenLayers.Bounds>} Bounds of the tile at the given pixel location.
  156. */
  157. getTileBounds: function(viewPortPx) {
  158. var resolution = this.getResolution();
  159. var tileMapWidth = resolution * this.tileSize.w;
  160. var tileMapHeight = resolution * this.tileSize.h;
  161. var mapPoint = this.getLonLatFromViewPortPx(viewPortPx);
  162. var tileLeft = tileMapWidth * Math.floor(mapPoint.lon / tileMapWidth);
  163. var tileBottom = tileMapHeight * Math.floor(mapPoint.lat / tileMapHeight);
  164. return new OpenLayers.Bounds(tileLeft, tileBottom,
  165. tileLeft + tileMapWidth,
  166. tileBottom + tileMapHeight);
  167. },
  168. CLASS_NAME: "OpenLayers.Layer.KaMap"
  169. });