GoogleEarthEnterpriseImageryProvider.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. import Credit from '../Core/Credit.js';
  2. import decodeGoogleEarthEnterpriseData from '../Core/decodeGoogleEarthEnterpriseData.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import DeveloperError from '../Core/DeveloperError.js';
  7. import Event from '../Core/Event.js';
  8. import GeographicTilingScheme from '../Core/GeographicTilingScheme.js';
  9. import GoogleEarthEnterpriseMetadata from '../Core/GoogleEarthEnterpriseMetadata.js';
  10. import loadImageFromTypedArray from '../Core/loadImageFromTypedArray.js';
  11. import CesiumMath from '../Core/Math.js';
  12. import Rectangle from '../Core/Rectangle.js';
  13. import Request from '../Core/Request.js';
  14. import Resource from '../Core/Resource.js';
  15. import RuntimeError from '../Core/RuntimeError.js';
  16. import TileProviderError from '../Core/TileProviderError.js';
  17. import protobuf from '../ThirdParty/protobuf-minimal.js';
  18. import when from '../ThirdParty/when.js';
  19. function GoogleEarthEnterpriseDiscardPolicy() {
  20. this._image = new Image();
  21. }
  22. /**
  23. * Determines if the discard policy is ready to process images.
  24. * @returns {Boolean} True if the discard policy is ready to process images; otherwise, false.
  25. */
  26. GoogleEarthEnterpriseDiscardPolicy.prototype.isReady = function() {
  27. return true;
  28. };
  29. /**
  30. * Given a tile image, decide whether to discard that image.
  31. *
  32. * @param {Image} image An image to test.
  33. * @returns {Boolean} True if the image should be discarded; otherwise, false.
  34. */
  35. GoogleEarthEnterpriseDiscardPolicy.prototype.shouldDiscardImage = function(image) {
  36. return (image === this._image);
  37. };
  38. /**
  39. * Provides tiled imagery using the Google Earth Enterprise REST API.
  40. *
  41. * Notes: This provider is for use with the 3D Earth API of Google Earth Enterprise,
  42. * {@link GoogleEarthEnterpriseMapsProvider} should be used with 2D Maps API.
  43. *
  44. * @alias GoogleEarthEnterpriseImageryProvider
  45. * @constructor
  46. *
  47. * @param {Object} options Object with the following properties:
  48. * @param {Resource|String} options.url The url of the Google Earth Enterprise server hosting the imagery.
  49. * @param {GoogleEarthEnterpriseMetadata} options.metadata A metadata object that can be used to share metadata requests with a GoogleEarthEnterpriseTerrainProvider.
  50. * @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  51. * @param {TileDiscardPolicy} [options.tileDiscardPolicy] The policy that determines if a tile
  52. * is invalid and should be discarded. If this value is not specified, a default
  53. * is to discard tiles that fail to download.
  54. * @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
  55. *
  56. * @see GoogleEarthEnterpriseTerrainProvider
  57. * @see ArcGisMapServerImageryProvider
  58. * @see GoogleEarthEnterpriseMapsProvider
  59. * @see OpenStreetMapImageryProvider
  60. * @see SingleTileImageryProvider
  61. * @see TileMapServiceImageryProvider
  62. * @see WebMapServiceImageryProvider
  63. * @see WebMapTileServiceImageryProvider
  64. * @see UrlTemplateImageryProvider
  65. *
  66. *
  67. * @example
  68. * var geeMetadata = new GoogleEarthEnterpriseMetadata('http://www.earthenterprise.org/3d');
  69. * var gee = new Cesium.GoogleEarthEnterpriseImageryProvider({
  70. * metadata : geeMetadata
  71. * });
  72. *
  73. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  74. */
  75. function GoogleEarthEnterpriseImageryProvider(options) {
  76. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  77. //>>includeStart('debug', pragmas.debug);
  78. if (!(defined(options.url) || defined(options.metadata))) {
  79. throw new DeveloperError('options.url or options.metadata is required.');
  80. }
  81. //>>includeEnd('debug');
  82. var metadata;
  83. if (defined(options.metadata)) {
  84. metadata = options.metadata;
  85. } else {
  86. var resource = Resource.createIfNeeded(options.url);
  87. metadata = new GoogleEarthEnterpriseMetadata(resource);
  88. }
  89. this._metadata = metadata;
  90. this._tileDiscardPolicy = options.tileDiscardPolicy;
  91. this._tilingScheme = new GeographicTilingScheme({
  92. numberOfLevelZeroTilesX : 2,
  93. numberOfLevelZeroTilesY : 2,
  94. rectangle : new Rectangle(-CesiumMath.PI, -CesiumMath.PI, CesiumMath.PI, CesiumMath.PI),
  95. ellipsoid : options.ellipsoid
  96. });
  97. var credit = options.credit;
  98. if (typeof credit === 'string') {
  99. credit = new Credit(credit);
  100. }
  101. this._credit = credit;
  102. this._tileWidth = 256;
  103. this._tileHeight = 256;
  104. this._maximumLevel = 23;
  105. // Install the default tile discard policy if none has been supplied.
  106. if (!defined(this._tileDiscardPolicy)) {
  107. this._tileDiscardPolicy = new GoogleEarthEnterpriseDiscardPolicy();
  108. }
  109. this._errorEvent = new Event();
  110. this._ready = false;
  111. var that = this;
  112. var metadataError;
  113. this._readyPromise = metadata.readyPromise
  114. .then(function(result) {
  115. if (!metadata.imageryPresent) {
  116. var e = new RuntimeError('The server ' + metadata.url + ' doesn\'t have imagery');
  117. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, e.message, undefined, undefined, undefined, e);
  118. return when.reject(e);
  119. }
  120. TileProviderError.handleSuccess(metadataError);
  121. that._ready = result;
  122. return result;
  123. })
  124. .otherwise(function(e) {
  125. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, e.message, undefined, undefined, undefined, e);
  126. return when.reject(e);
  127. });
  128. }
  129. defineProperties(GoogleEarthEnterpriseImageryProvider.prototype, {
  130. /**
  131. * Gets the name of the Google Earth Enterprise server url hosting the imagery.
  132. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  133. * @type {String}
  134. * @readonly
  135. */
  136. url : {
  137. get : function() {
  138. return this._metadata.url;
  139. }
  140. },
  141. /**
  142. * Gets the proxy used by this provider.
  143. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  144. * @type {Proxy}
  145. * @readonly
  146. */
  147. proxy : {
  148. get : function() {
  149. return this._metadata.proxy;
  150. }
  151. },
  152. /**
  153. * Gets the width of each tile, in pixels. This function should
  154. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  155. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  156. * @type {Number}
  157. * @readonly
  158. */
  159. tileWidth : {
  160. get : function() {
  161. //>>includeStart('debug', pragmas.debug);
  162. if (!this._ready) {
  163. throw new DeveloperError('tileWidth must not be called before the imagery provider is ready.');
  164. }
  165. //>>includeEnd('debug');
  166. return this._tileWidth;
  167. }
  168. },
  169. /**
  170. * Gets the height of each tile, in pixels. This function should
  171. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  172. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  173. * @type {Number}
  174. * @readonly
  175. */
  176. tileHeight : {
  177. get : function() {
  178. //>>includeStart('debug', pragmas.debug);
  179. if (!this._ready) {
  180. throw new DeveloperError('tileHeight must not be called before the imagery provider is ready.');
  181. }
  182. //>>includeEnd('debug');
  183. return this._tileHeight;
  184. }
  185. },
  186. /**
  187. * Gets the maximum level-of-detail that can be requested. This function should
  188. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  189. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  190. * @type {Number}
  191. * @readonly
  192. */
  193. maximumLevel : {
  194. get : function() {
  195. //>>includeStart('debug', pragmas.debug);
  196. if (!this._ready) {
  197. throw new DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
  198. }
  199. //>>includeEnd('debug');
  200. return this._maximumLevel;
  201. }
  202. },
  203. /**
  204. * Gets the minimum level-of-detail that can be requested. This function should
  205. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  206. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  207. * @type {Number}
  208. * @readonly
  209. */
  210. minimumLevel : {
  211. get : function() {
  212. //>>includeStart('debug', pragmas.debug);
  213. if (!this._ready) {
  214. throw new DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
  215. }
  216. //>>includeEnd('debug');
  217. return 0;
  218. }
  219. },
  220. /**
  221. * Gets the tiling scheme used by this provider. This function should
  222. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  223. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  224. * @type {TilingScheme}
  225. * @readonly
  226. */
  227. tilingScheme : {
  228. get : function() {
  229. //>>includeStart('debug', pragmas.debug);
  230. if (!this._ready) {
  231. throw new DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
  232. }
  233. //>>includeEnd('debug');
  234. return this._tilingScheme;
  235. }
  236. },
  237. /**
  238. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  239. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  240. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  241. * @type {Rectangle}
  242. * @readonly
  243. */
  244. rectangle : {
  245. get : function() {
  246. //>>includeStart('debug', pragmas.debug);
  247. if (!this._ready) {
  248. throw new DeveloperError('rectangle must not be called before the imagery provider is ready.');
  249. }
  250. //>>includeEnd('debug');
  251. return this._tilingScheme.rectangle;
  252. }
  253. },
  254. /**
  255. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  256. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  257. * returns undefined, no tiles are filtered. This function should
  258. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  259. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  260. * @type {TileDiscardPolicy}
  261. * @readonly
  262. */
  263. tileDiscardPolicy : {
  264. get : function() {
  265. //>>includeStart('debug', pragmas.debug);
  266. if (!this._ready) {
  267. throw new DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
  268. }
  269. //>>includeEnd('debug');
  270. return this._tileDiscardPolicy;
  271. }
  272. },
  273. /**
  274. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  275. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  276. * are passed an instance of {@link TileProviderError}.
  277. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  278. * @type {Event}
  279. * @readonly
  280. */
  281. errorEvent : {
  282. get : function() {
  283. return this._errorEvent;
  284. }
  285. },
  286. /**
  287. * Gets a value indicating whether or not the provider is ready for use.
  288. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  289. * @type {Boolean}
  290. * @readonly
  291. */
  292. ready : {
  293. get : function() {
  294. return this._ready;
  295. }
  296. },
  297. /**
  298. * Gets a promise that resolves to true when the provider is ready for use.
  299. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  300. * @type {Promise.<Boolean>}
  301. * @readonly
  302. */
  303. readyPromise : {
  304. get : function() {
  305. return this._readyPromise;
  306. }
  307. },
  308. /**
  309. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  310. * the source of the imagery. This function should not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  311. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  312. * @type {Credit}
  313. * @readonly
  314. */
  315. credit : {
  316. get : function() {
  317. return this._credit;
  318. }
  319. },
  320. /**
  321. * Gets a value indicating whether or not the images provided by this imagery provider
  322. * include an alpha channel. If this property is false, an alpha channel, if present, will
  323. * be ignored. If this property is true, any images without an alpha channel will be treated
  324. * as if their alpha is 1.0 everywhere. Setting this property to false reduces memory usage
  325. * and texture upload time.
  326. * @memberof GoogleEarthEnterpriseImageryProvider.prototype
  327. * @type {Boolean}
  328. * @readonly
  329. */
  330. hasAlphaChannel : {
  331. get : function() {
  332. return false;
  333. }
  334. }
  335. });
  336. /**
  337. * Gets the credits to be displayed when a given tile is displayed.
  338. *
  339. * @param {Number} x The tile X coordinate.
  340. * @param {Number} y The tile Y coordinate.
  341. * @param {Number} level The tile level;
  342. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  343. *
  344. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  345. */
  346. GoogleEarthEnterpriseImageryProvider.prototype.getTileCredits = function(x, y, level) {
  347. //>>includeStart('debug', pragmas.debug);
  348. if (!this._ready) {
  349. throw new DeveloperError('getTileCredits must not be called before the imagery provider is ready.');
  350. }
  351. //>>includeEnd('debug');
  352. var metadata = this._metadata;
  353. var info = metadata.getTileInformation(x, y, level);
  354. if (defined(info)) {
  355. var credit = metadata.providers[info.imageryProvider];
  356. if (defined(credit)) {
  357. return [credit];
  358. }
  359. }
  360. return undefined;
  361. };
  362. /**
  363. * Requests the image for a given tile. This function should
  364. * not be called before {@link GoogleEarthEnterpriseImageryProvider#ready} returns true.
  365. *
  366. * @param {Number} x The tile X coordinate.
  367. * @param {Number} y The tile Y coordinate.
  368. * @param {Number} level The tile level.
  369. * @param {Request} [request] The request object. Intended for internal use only.
  370. * @returns {Promise.<Image|Canvas>|undefined} A promise for the image that will resolve when the image is available, or
  371. * undefined if there are too many active requests to the server, and the request
  372. * should be retried later. The resolved image may be either an
  373. * Image or a Canvas DOM object.
  374. *
  375. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  376. */
  377. GoogleEarthEnterpriseImageryProvider.prototype.requestImage = function(x, y, level, request) {
  378. //>>includeStart('debug', pragmas.debug);
  379. if (!this._ready) {
  380. throw new DeveloperError('requestImage must not be called before the imagery provider is ready.');
  381. }
  382. //>>includeEnd('debug');
  383. var invalidImage = this._tileDiscardPolicy._image; // Empty image or undefined depending on discard policy
  384. var metadata = this._metadata;
  385. var quadKey = GoogleEarthEnterpriseMetadata.tileXYToQuadKey(x, y, level);
  386. var info = metadata.getTileInformation(x, y, level);
  387. if (!defined(info)) {
  388. if (metadata.isValid(quadKey)) {
  389. var metadataRequest = new Request({
  390. throttle : request.throttle,
  391. throttleByServer : request.throttleByServer,
  392. type : request.type,
  393. priorityFunction : request.priorityFunction
  394. });
  395. metadata.populateSubtree(x, y, level, metadataRequest);
  396. return undefined; // No metadata so return undefined so we can be loaded later
  397. }
  398. return invalidImage; // Image doesn't exist
  399. }
  400. if (!info.hasImagery()) {
  401. // Already have info and there isn't any imagery here
  402. return invalidImage;
  403. }
  404. var promise = buildImageResource(this, info, x, y, level, request).fetchArrayBuffer();
  405. if (!defined(promise)) {
  406. return undefined; // Throttled
  407. }
  408. return promise
  409. .then(function(image) {
  410. decodeGoogleEarthEnterpriseData(metadata.key, image);
  411. var a = new Uint8Array(image);
  412. var type;
  413. var protoImagery = metadata.protoImagery;
  414. if (!defined(protoImagery) || !protoImagery) {
  415. type = getImageType(a);
  416. }
  417. if (!defined(type) && (!defined(protoImagery) || protoImagery)) {
  418. var message = decodeEarthImageryPacket(a);
  419. type = message.imageType;
  420. a = message.imageData;
  421. }
  422. if (!defined(type) || !defined(a)) {
  423. return invalidImage;
  424. }
  425. return loadImageFromTypedArray({
  426. uint8Array: a,
  427. format: type,
  428. flipY: true
  429. });
  430. });
  431. };
  432. /**
  433. * Picking features is not currently supported by this imagery provider, so this function simply returns
  434. * undefined.
  435. *
  436. * @param {Number} x The tile X coordinate.
  437. * @param {Number} y The tile Y coordinate.
  438. * @param {Number} level The tile level.
  439. * @param {Number} longitude The longitude at which to pick features.
  440. * @param {Number} latitude The latitude at which to pick features.
  441. * @return {Promise.<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  442. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  443. * instances. The array may be empty if no features are found at the given location.
  444. * It may also be undefined if picking is not supported.
  445. */
  446. GoogleEarthEnterpriseImageryProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  447. return undefined;
  448. };
  449. //
  450. // Functions to handle imagery packets
  451. //
  452. function buildImageResource(imageryProvider, info, x, y, level, request) {
  453. var quadKey = GoogleEarthEnterpriseMetadata.tileXYToQuadKey(x, y, level);
  454. var version = info.imageryVersion;
  455. version = (defined(version) && version > 0) ? version : 1;
  456. return imageryProvider._metadata.resource.getDerivedResource({
  457. url: 'flatfile?f1-0' + quadKey + '-i.' + version.toString(),
  458. request: request
  459. });
  460. }
  461. // Detects if a Uint8Array is a JPEG or PNG
  462. function getImageType(image) {
  463. var jpeg = 'JFIF';
  464. if (image[6] === jpeg.charCodeAt(0) && image[7] === jpeg.charCodeAt(1) &&
  465. image[8] === jpeg.charCodeAt(2) && image[9] === jpeg.charCodeAt(3)) {
  466. return 'image/jpeg';
  467. }
  468. var png = 'PNG';
  469. if (image[1] === png.charCodeAt(0) && image[2] === png.charCodeAt(1) && image[3] === png.charCodeAt(2)) {
  470. return 'image/png';
  471. }
  472. return undefined;
  473. }
  474. // Decodes an Imagery protobuf into the message
  475. // Partially generated with the help of protobuf.js static generator
  476. function decodeEarthImageryPacket(data) {
  477. var reader = protobuf.Reader.create(data);
  478. var end = reader.len;
  479. var message = {};
  480. while (reader.pos < end) {
  481. var tag = reader.uint32();
  482. switch (tag >>> 3) {
  483. case 1:
  484. message.imageType = reader.uint32();
  485. break;
  486. case 2:
  487. message.imageData = reader.bytes();
  488. break;
  489. case 3:
  490. message.alphaType = reader.uint32();
  491. break;
  492. case 4:
  493. message.imageAlpha = reader.bytes();
  494. break;
  495. case 5:
  496. var copyrightIds = message.copyrightIds;
  497. if (!defined(copyrightIds)) {
  498. copyrightIds = message.copyrightIds = [];
  499. }
  500. if ((tag & 7) === 2) {
  501. var end2 = reader.uint32() + reader.pos;
  502. while (reader.pos < end2) {
  503. copyrightIds.push(reader.uint32());
  504. }
  505. } else {
  506. copyrightIds.push(reader.uint32());
  507. }
  508. break;
  509. default:
  510. reader.skipType(tag & 7);
  511. break;
  512. }
  513. }
  514. var imageType = message.imageType;
  515. if (defined(imageType)) {
  516. switch (imageType) {
  517. case 0:
  518. message.imageType = 'image/jpeg';
  519. break;
  520. case 4:
  521. message.imageType = 'image/png';
  522. break;
  523. default:
  524. throw new RuntimeError('GoogleEarthEnterpriseImageryProvider: Unsupported image type.');
  525. }
  526. }
  527. var alphaType = message.alphaType;
  528. if (defined(alphaType) && alphaType !== 0) {
  529. console.log('GoogleEarthEnterpriseImageryProvider: External alpha not supported.');
  530. delete message.alphaType;
  531. delete message.imageAlpha;
  532. }
  533. return message;
  534. }
  535. export default GoogleEarthEnterpriseImageryProvider;