GridImageryProvider.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import Color from '../Core/Color.js';
  2. import defaultValue from '../Core/defaultValue.js';
  3. import defined from '../Core/defined.js';
  4. import defineProperties from '../Core/defineProperties.js';
  5. import Event from '../Core/Event.js';
  6. import GeographicTilingScheme from '../Core/GeographicTilingScheme.js';
  7. import when from '../ThirdParty/when.js';
  8. var defaultColor = new Color(1.0, 1.0, 1.0, 0.4);
  9. var defaultGlowColor = new Color(0.0, 1.0, 0.0, 0.05);
  10. var defaultBackgroundColor = new Color(0.0, 0.5, 0.0, 0.2);
  11. /**
  12. * An {@link ImageryProvider} that draws a wireframe grid on every tile with controllable background and glow.
  13. * May be useful for custom rendering effects or debugging terrain.
  14. *
  15. * @alias GridImageryProvider
  16. * @constructor
  17. *
  18. * @param {Object} [options] Object with the following properties:
  19. * @param {TilingScheme} [options.tilingScheme=new GeographicTilingScheme()] The tiling scheme for which to draw tiles.
  20. * @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If the tilingScheme is specified,
  21. * this parameter is ignored and the tiling scheme's ellipsoid is used instead. If neither
  22. * parameter is specified, the WGS84 ellipsoid is used.
  23. * @param {Number} [options.cells=8] The number of grids cells.
  24. * @param {Color} [options.color=Color(1.0, 1.0, 1.0, 0.4)] The color to draw grid lines.
  25. * @param {Color} [options.glowColor=Color(0.0, 1.0, 0.0, 0.05)] The color to draw glow for grid lines.
  26. * @param {Number} [options.glowWidth=6] The width of lines used for rendering the line glow effect.
  27. * @param {Color} [options.backgroundColor=Color(0.0, 0.5, 0.0, 0.2)] Background fill color.
  28. * @param {Number} [options.tileWidth=256] The width of the tile for level-of-detail selection purposes.
  29. * @param {Number} [options.tileHeight=256] The height of the tile for level-of-detail selection purposes.
  30. * @param {Number} [options.canvasSize=256] The size of the canvas used for rendering.
  31. */
  32. function GridImageryProvider(options) {
  33. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  34. this._tilingScheme = defined(options.tilingScheme) ? options.tilingScheme : new GeographicTilingScheme({ ellipsoid : options.ellipsoid });
  35. this._cells = defaultValue(options.cells, 8);
  36. this._color = defaultValue(options.color, defaultColor);
  37. this._glowColor = defaultValue(options.glowColor, defaultGlowColor);
  38. this._glowWidth = defaultValue(options.glowWidth, 6);
  39. this._backgroundColor = defaultValue(options.backgroundColor, defaultBackgroundColor);
  40. this._errorEvent = new Event();
  41. this._tileWidth = defaultValue(options.tileWidth, 256);
  42. this._tileHeight = defaultValue(options.tileHeight, 256);
  43. // A little larger than tile size so lines are sharper
  44. // Note: can't be too much difference otherwise texture blowout
  45. this._canvasSize = defaultValue(options.canvasSize, 256);
  46. // We only need a single canvas since all tiles will be the same
  47. this._canvas = this._createGridCanvas();
  48. this._readyPromise = when.resolve(true);
  49. }
  50. defineProperties(GridImageryProvider.prototype, {
  51. /**
  52. * Gets the proxy used by this provider.
  53. * @memberof GridImageryProvider.prototype
  54. * @type {Proxy}
  55. * @readonly
  56. */
  57. proxy : {
  58. get : function() {
  59. return undefined;
  60. }
  61. },
  62. /**
  63. * Gets the width of each tile, in pixels. This function should
  64. * not be called before {@link GridImageryProvider#ready} returns true.
  65. * @memberof GridImageryProvider.prototype
  66. * @type {Number}
  67. * @readonly
  68. */
  69. tileWidth : {
  70. get : function() {
  71. return this._tileWidth;
  72. }
  73. },
  74. /**
  75. * Gets the height of each tile, in pixels. This function should
  76. * not be called before {@link GridImageryProvider#ready} returns true.
  77. * @memberof GridImageryProvider.prototype
  78. * @type {Number}
  79. * @readonly
  80. */
  81. tileHeight : {
  82. get : function() {
  83. return this._tileHeight;
  84. }
  85. },
  86. /**
  87. * Gets the maximum level-of-detail that can be requested. This function should
  88. * not be called before {@link GridImageryProvider#ready} returns true.
  89. * @memberof GridImageryProvider.prototype
  90. * @type {Number}
  91. * @readonly
  92. */
  93. maximumLevel : {
  94. get : function() {
  95. return undefined;
  96. }
  97. },
  98. /**
  99. * Gets the minimum level-of-detail that can be requested. This function should
  100. * not be called before {@link GridImageryProvider#ready} returns true.
  101. * @memberof GridImageryProvider.prototype
  102. * @type {Number}
  103. * @readonly
  104. */
  105. minimumLevel : {
  106. get : function() {
  107. return undefined;
  108. }
  109. },
  110. /**
  111. * Gets the tiling scheme used by this provider. This function should
  112. * not be called before {@link GridImageryProvider#ready} returns true.
  113. * @memberof GridImageryProvider.prototype
  114. * @type {TilingScheme}
  115. * @readonly
  116. */
  117. tilingScheme : {
  118. get : function() {
  119. return this._tilingScheme;
  120. }
  121. },
  122. /**
  123. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  124. * not be called before {@link GridImageryProvider#ready} returns true.
  125. * @memberof GridImageryProvider.prototype
  126. * @type {Rectangle}
  127. * @readonly
  128. */
  129. rectangle : {
  130. get : function() {
  131. return this._tilingScheme.rectangle;
  132. }
  133. },
  134. /**
  135. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  136. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  137. * returns undefined, no tiles are filtered. This function should
  138. * not be called before {@link GridImageryProvider#ready} returns true.
  139. * @memberof GridImageryProvider.prototype
  140. * @type {TileDiscardPolicy}
  141. * @readonly
  142. */
  143. tileDiscardPolicy : {
  144. get : function() {
  145. return undefined;
  146. }
  147. },
  148. /**
  149. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  150. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  151. * are passed an instance of {@link TileProviderError}.
  152. * @memberof GridImageryProvider.prototype
  153. * @type {Event}
  154. * @readonly
  155. */
  156. errorEvent : {
  157. get : function() {
  158. return this._errorEvent;
  159. }
  160. },
  161. /**
  162. * Gets a value indicating whether or not the provider is ready for use.
  163. * @memberof GridImageryProvider.prototype
  164. * @type {Boolean}
  165. * @readonly
  166. */
  167. ready : {
  168. get : function() {
  169. return true;
  170. }
  171. },
  172. /**
  173. * Gets a promise that resolves to true when the provider is ready for use.
  174. * @memberof GridImageryProvider.prototype
  175. * @type {Promise.<Boolean>}
  176. * @readonly
  177. */
  178. readyPromise : {
  179. get : function() {
  180. return this._readyPromise;
  181. }
  182. },
  183. /**
  184. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  185. * the source of the imagery. This function should not be called before {@link GridImageryProvider#ready} returns true.
  186. * @memberof GridImageryProvider.prototype
  187. * @type {Credit}
  188. * @readonly
  189. */
  190. credit : {
  191. get : function() {
  192. return undefined;
  193. }
  194. },
  195. /**
  196. * Gets a value indicating whether or not the images provided by this imagery provider
  197. * include an alpha channel. If this property is false, an alpha channel, if present, will
  198. * be ignored. If this property is true, any images without an alpha channel will be treated
  199. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  200. * and texture upload time are reduced.
  201. * @memberof GridImageryProvider.prototype
  202. * @type {Boolean}
  203. * @readonly
  204. */
  205. hasAlphaChannel : {
  206. get : function() {
  207. return true;
  208. }
  209. }
  210. });
  211. /**
  212. * Draws a grid of lines into a canvas.
  213. */
  214. GridImageryProvider.prototype._drawGrid = function(context) {
  215. var minPixel = 0;
  216. var maxPixel = this._canvasSize;
  217. for (var x = 0; x <= this._cells; ++x) {
  218. var nx = x / this._cells;
  219. var val = 1 + nx * (maxPixel - 1);
  220. context.moveTo(val, minPixel);
  221. context.lineTo(val, maxPixel);
  222. context.moveTo(minPixel, val);
  223. context.lineTo(maxPixel, val);
  224. }
  225. context.stroke();
  226. };
  227. /**
  228. * Render a grid into a canvas with background and glow
  229. */
  230. GridImageryProvider.prototype._createGridCanvas = function() {
  231. var canvas = document.createElement('canvas');
  232. canvas.width = this._canvasSize;
  233. canvas.height = this._canvasSize;
  234. var minPixel = 0;
  235. var maxPixel = this._canvasSize;
  236. var context = canvas.getContext('2d');
  237. // Fill the background
  238. var cssBackgroundColor = this._backgroundColor.toCssColorString();
  239. context.fillStyle = cssBackgroundColor;
  240. context.fillRect(minPixel, minPixel, maxPixel, maxPixel);
  241. // Glow for grid lines
  242. var cssGlowColor = this._glowColor.toCssColorString();
  243. context.strokeStyle = cssGlowColor;
  244. // Wide
  245. context.lineWidth = this._glowWidth;
  246. context.strokeRect(minPixel, minPixel, maxPixel, maxPixel);
  247. this._drawGrid(context);
  248. // Narrow
  249. context.lineWidth = this._glowWidth * 0.5;
  250. context.strokeRect(minPixel, minPixel, maxPixel, maxPixel);
  251. this._drawGrid(context);
  252. // Grid lines
  253. var cssColor = this._color.toCssColorString();
  254. // Border
  255. context.strokeStyle = cssColor;
  256. context.lineWidth = 2;
  257. context.strokeRect(minPixel, minPixel, maxPixel, maxPixel);
  258. // Inner
  259. context.lineWidth = 1;
  260. this._drawGrid(context);
  261. return canvas;
  262. };
  263. /**
  264. * Gets the credits to be displayed when a given tile is displayed.
  265. *
  266. * @param {Number} x The tile X coordinate.
  267. * @param {Number} y The tile Y coordinate.
  268. * @param {Number} level The tile level;
  269. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  270. *
  271. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  272. */
  273. GridImageryProvider.prototype.getTileCredits = function(x, y, level) {
  274. return undefined;
  275. };
  276. /**
  277. * Requests the image for a given tile. This function should
  278. * not be called before {@link GridImageryProvider#ready} returns true.
  279. *
  280. * @param {Number} x The tile X coordinate.
  281. * @param {Number} y The tile Y coordinate.
  282. * @param {Number} level The tile level.
  283. * @param {Request} [request] The request object. Intended for internal use only.
  284. * @returns {Promise.<Image|Canvas>|undefined} A promise for the image that will resolve when the image is available, or
  285. * undefined if there are too many active requests to the server, and the request
  286. * should be retried later. The resolved image may be either an
  287. * Image or a Canvas DOM object.
  288. */
  289. GridImageryProvider.prototype.requestImage = function(x, y, level, request) {
  290. return this._canvas;
  291. };
  292. /**
  293. * Picking features is not currently supported by this imagery provider, so this function simply returns
  294. * undefined.
  295. *
  296. * @param {Number} x The tile X coordinate.
  297. * @param {Number} y The tile Y coordinate.
  298. * @param {Number} level The tile level.
  299. * @param {Number} longitude The longitude at which to pick features.
  300. * @param {Number} latitude The latitude at which to pick features.
  301. * @return {Promise.<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  302. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  303. * instances. The array may be empty if no features are found at the given location.
  304. * It may also be undefined if picking is not supported.
  305. */
  306. GridImageryProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  307. return undefined;
  308. };
  309. export default GridImageryProvider;