UTFGrid.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/XYZ.js
  7. * @requires OpenLayers/Tile/UTFGrid.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.UTFGrid
  11. * This Layer reads from UTFGrid tiled data sources. Since UTFGrids are
  12. * essentially JSON-based ASCII art with attached attributes, they are not
  13. * visibly rendered. In order to use them in the map, you must add a
  14. * <OpenLayers.Control.UTFGrid> control as well.
  15. *
  16. * Example:
  17. *
  18. * (start code)
  19. * var world_utfgrid = new OpenLayers.Layer.UTFGrid({
  20. * url: "/tiles/world_utfgrid/${z}/${x}/${y}.json",
  21. * utfgridResolution: 4,
  22. * displayInLayerSwitcher: false
  23. * );
  24. * map.addLayer(world_utfgrid);
  25. *
  26. * var control = new OpenLayers.Control.UTFGrid({
  27. * layers: [world_utfgrid],
  28. * handlerMode: 'move',
  29. * callback: function(dataLookup) {
  30. * // do something with returned data
  31. * }
  32. * })
  33. * (end code)
  34. *
  35. *
  36. * Inherits from:
  37. * - <OpenLayers.Layer.XYZ>
  38. */
  39. OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.XYZ, {
  40. /**
  41. * APIProperty: isBaseLayer
  42. * Default is false, as UTFGrids are designed to be a transparent overlay layer.
  43. */
  44. isBaseLayer: false,
  45. /**
  46. * APIProperty: projection
  47. * {<OpenLayers.Projection>}
  48. * Source projection for the UTFGrids. Default is "EPSG:900913".
  49. */
  50. projection: new OpenLayers.Projection("EPSG:900913"),
  51. /**
  52. * Property: useJSONP
  53. * {Boolean}
  54. * Should we use a JSONP script approach instead of a standard AJAX call?
  55. *
  56. * Set to true for using utfgrids from another server.
  57. * Avoids same-domain policy restrictions.
  58. * Note that this only works if the server accepts
  59. * the callback GET parameter and dynamically
  60. * wraps the returned json in a function call.
  61. *
  62. * Default is false
  63. */
  64. useJSONP: false,
  65. /**
  66. * APIProperty: url
  67. * {String}
  68. * URL tempate for UTFGrid tiles. Include x, y, and z parameters.
  69. * E.g. "/tiles/${z}/${x}/${y}.json"
  70. */
  71. /**
  72. * APIProperty: utfgridResolution
  73. * {Number}
  74. * Ratio of the pixel width to the width of a UTFGrid data point. If an
  75. * entry in the grid represents a 4x4 block of pixels, the
  76. * utfgridResolution would be 4. Default is 2 (specified in
  77. * <OpenLayers.Tile.UTFGrid>).
  78. */
  79. /**
  80. * Property: tileClass
  81. * {<OpenLayers.Tile>} The tile class to use for this layer.
  82. * Defaults is <OpenLayers.Tile.UTFGrid>.
  83. */
  84. tileClass: OpenLayers.Tile.UTFGrid,
  85. /**
  86. * Constructor: OpenLayers.Layer.UTFGrid
  87. * Create a new UTFGrid layer.
  88. *
  89. * Parameters:
  90. * config - {Object} Configuration properties for the layer.
  91. *
  92. * Required configuration properties:
  93. * url - {String} The url template for UTFGrid tiles. See the <url> property.
  94. */
  95. initialize: function(options) {
  96. OpenLayers.Layer.Grid.prototype.initialize.apply(
  97. this, [options.name, options.url, {}, options]
  98. );
  99. this.tileOptions = OpenLayers.Util.extend({
  100. utfgridResolution: this.utfgridResolution
  101. }, this.tileOptions);
  102. },
  103. /**
  104. * Method: createBackBuffer
  105. * The UTFGrid cannot create a back buffer, so this method is overriden.
  106. */
  107. createBackBuffer: function() {},
  108. /**
  109. * APIMethod: clone
  110. * Create a clone of this layer
  111. *
  112. * Parameters:
  113. * obj - {Object} Only used by a subclass of this layer.
  114. *
  115. * Returns:
  116. * {<OpenLayers.Layer.UTFGrid>} An exact clone of this OpenLayers.Layer.UTFGrid
  117. */
  118. clone: function (obj) {
  119. if (obj == null) {
  120. obj = new OpenLayers.Layer.UTFGrid(this.getOptions());
  121. }
  122. // get all additions from superclasses
  123. obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
  124. return obj;
  125. },
  126. /**
  127. * APIProperty: getFeatureInfo
  128. * Get details about a feature associated with a map location. The object
  129. * returned will have id and data properties. If the given location
  130. * doesn't correspond to a feature, null will be returned.
  131. *
  132. * Parameters:
  133. * location - {<OpenLayers.LonLat>} map location
  134. *
  135. * Returns:
  136. * {Object} Object representing the feature id and UTFGrid data
  137. * corresponding to the given map location. Returns null if the given
  138. * location doesn't hit a feature.
  139. */
  140. getFeatureInfo: function(location) {
  141. var info = null;
  142. var tileInfo = this.getTileData(location);
  143. if (tileInfo && tileInfo.tile) {
  144. info = tileInfo.tile.getFeatureInfo(tileInfo.i, tileInfo.j);
  145. }
  146. return info;
  147. },
  148. /**
  149. * APIMethod: getFeatureId
  150. * Get the identifier for the feature associated with a map location.
  151. *
  152. * Parameters:
  153. * location - {<OpenLayers.LonLat>} map location
  154. *
  155. * Returns:
  156. * {String} The feature identifier corresponding to the given map location.
  157. * Returns null if the location doesn't hit a feature.
  158. */
  159. getFeatureId: function(location) {
  160. var id = null;
  161. var info = this.getTileData(location);
  162. if (info.tile) {
  163. id = info.tile.getFeatureId(info.i, info.j);
  164. }
  165. return id;
  166. },
  167. CLASS_NAME: "OpenLayers.Layer.UTFGrid"
  168. });