GoogleEarthEnterpriseMapsProvider.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. import buildModuleUrl from '../Core/buildModuleUrl.js';
  2. import Check from '../Core/Check.js';
  3. import Credit from '../Core/Credit.js';
  4. import defaultValue from '../Core/defaultValue.js';
  5. import defined from '../Core/defined.js';
  6. import defineProperties from '../Core/defineProperties.js';
  7. import DeveloperError from '../Core/DeveloperError.js';
  8. import Event from '../Core/Event.js';
  9. import GeographicTilingScheme from '../Core/GeographicTilingScheme.js';
  10. import Rectangle from '../Core/Rectangle.js';
  11. import Resource from '../Core/Resource.js';
  12. import RuntimeError from '../Core/RuntimeError.js';
  13. import TileProviderError from '../Core/TileProviderError.js';
  14. import WebMercatorTilingScheme from '../Core/WebMercatorTilingScheme.js';
  15. import when from '../ThirdParty/when.js';
  16. import ImageryProvider from './ImageryProvider.js';
  17. /**
  18. * Provides tiled imagery using the Google Earth Imagery API.
  19. *
  20. * Notes: This imagery provider does not work with the public Google Earth servers. It works with the
  21. * Google Earth Enterprise Server.
  22. *
  23. * By default the Google Earth Enterprise server does not set the
  24. * {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing} headers. You can either
  25. * use a proxy server which adds these headers, or in the /opt/google/gehttpd/conf/gehttpd.conf
  26. * and add the 'Header set Access-Control-Allow-Origin "*"' option to the '<Directory />' and
  27. * '<Directory "/opt/google/gehttpd/htdocs">' directives.
  28. *
  29. * This provider is for use with 2D Maps API as part of Google Earth Enterprise. For 3D Earth API uses, it
  30. * is necessary to use {@link GoogleEarthEnterpriseImageryProvider}
  31. *
  32. * @alias GoogleEarthEnterpriseMapsProvider
  33. * @constructor
  34. *
  35. * @param {Object} options Object with the following properties:
  36. * @param {Resource|String} options.url The url of the Google Earth server hosting the imagery.
  37. * @param {Number} options.channel The channel (id) to be used when requesting data from the server.
  38. * The channel number can be found by looking at the json file located at:
  39. * earth.localdomain/default_map/query?request=Json&vars=geeServerDefs The /default_map path may
  40. * differ depending on your Google Earth Enterprise server configuration. Look for the "id" that
  41. * is associated with a "ImageryMaps" requestType. There may be more than one id available.
  42. * Example:
  43. * {
  44. * layers: [
  45. * {
  46. * id: 1002,
  47. * requestType: "ImageryMaps"
  48. * },
  49. * {
  50. * id: 1007,
  51. * requestType: "VectorMapsRaster"
  52. * }
  53. * ]
  54. * }
  55. * @param {String} [options.path="/default_map"] The path of the Google Earth server hosting the imagery.
  56. * @param {Number} [options.maximumLevel] The maximum level-of-detail supported by the Google Earth
  57. * Enterprise server, or undefined if there is no limit.
  58. * @param {TileDiscardPolicy} [options.tileDiscardPolicy] The policy that determines if a tile
  59. * is invalid and should be discarded. To ensure that no tiles are discarded, construct and pass
  60. * a {@link NeverTileDiscardPolicy} for this parameter.
  61. * @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  62. *
  63. * @exception {RuntimeError} Could not find layer with channel (id) of <code>options.channel</code>.
  64. * @exception {RuntimeError} Could not find a version in channel (id) <code>options.channel</code>.
  65. * @exception {RuntimeError} Unsupported projection <code>data.projection</code>.
  66. *
  67. * @see ArcGisMapServerImageryProvider
  68. * @see BingMapsImageryProvider
  69. * @see OpenStreetMapImageryProvider
  70. * @see SingleTileImageryProvider
  71. * @see TileMapServiceImageryProvider
  72. * @see WebMapServiceImageryProvider
  73. * @see WebMapTileServiceImageryProvider
  74. * @see UrlTemplateImageryProvider
  75. *
  76. *
  77. * @example
  78. * var google = new Cesium.GoogleEarthEnterpriseMapsProvider({
  79. * url : 'https://earth.localdomain',
  80. * channel : 1008
  81. * });
  82. *
  83. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  84. */
  85. function GoogleEarthEnterpriseMapsProvider(options) {
  86. options = defaultValue(options, {});
  87. //>>includeStart('debug', pragmas.debug);
  88. if (!defined(options.url)) {
  89. throw new DeveloperError('options.url is required.');
  90. }
  91. if (!defined(options.channel)) {
  92. throw new DeveloperError('options.channel is required.');
  93. }
  94. //>>includeEnd('debug');
  95. var url = options.url;
  96. var path = defaultValue(options.path, '/default_map');
  97. var resource = Resource.createIfNeeded(url).getDerivedResource({
  98. // We used to just append path to url, so now that we do proper URI resolution, removed the /
  99. url : (path[0] === '/') ? path.substring(1) : path
  100. });
  101. resource.appendForwardSlash();
  102. this._resource = resource;
  103. this._url = url;
  104. this._path = path;
  105. this._tileDiscardPolicy = options.tileDiscardPolicy;
  106. this._channel = options.channel;
  107. this._requestType = 'ImageryMaps';
  108. this._credit = new Credit('<a href="http://www.google.com/enterprise/mapsearth/products/earthenterprise.html"><img src="' + GoogleEarthEnterpriseMapsProvider.logoUrl + '" title="Google Imagery"/></a>');
  109. /**
  110. * The default {@link ImageryLayer#gamma} to use for imagery layers created for this provider.
  111. * By default, this is set to 1.9. Changing this value after creating an {@link ImageryLayer} for this provider will have
  112. * no effect. Instead, set the layer's {@link ImageryLayer#gamma} property.
  113. *
  114. * @type {Number}
  115. * @default 1.9
  116. */
  117. this.defaultGamma = 1.9;
  118. this._tilingScheme = undefined;
  119. this._version = undefined;
  120. this._tileWidth = 256;
  121. this._tileHeight = 256;
  122. this._maximumLevel = options.maximumLevel;
  123. this._errorEvent = new Event();
  124. this._ready = false;
  125. this._readyPromise = when.defer();
  126. var metadataResource = resource.getDerivedResource({
  127. url: 'query',
  128. queryParameters: {
  129. request: 'Json',
  130. vars: 'geeServerDefs',
  131. is2d: 't'
  132. }
  133. });
  134. var that = this;
  135. var metadataError;
  136. function metadataSuccess(text) {
  137. var data;
  138. // The Google Earth server sends malformed JSON data currently...
  139. try {
  140. // First, try parsing it like normal in case a future version sends correctly formatted JSON
  141. data = JSON.parse(text);
  142. } catch (e) {
  143. // Quote object strings manually, then try parsing again
  144. data = JSON.parse(text.replace(/([\[\{,])[\n\r ]*([A-Za-z0-9]+)[\n\r ]*:/g, '$1"$2":'));
  145. }
  146. var layer;
  147. for (var i = 0; i < data.layers.length; i++) {
  148. if (data.layers[i].id === that._channel) {
  149. layer = data.layers[i];
  150. break;
  151. }
  152. }
  153. var message;
  154. if (!defined(layer)) {
  155. message = 'Could not find layer with channel (id) of ' + that._channel + '.';
  156. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  157. throw new RuntimeError(message);
  158. }
  159. if (!defined(layer.version)) {
  160. message = 'Could not find a version in channel (id) ' + that._channel + '.';
  161. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  162. throw new RuntimeError(message);
  163. }
  164. that._version = layer.version;
  165. if (defined(data.projection) && data.projection === 'flat') {
  166. that._tilingScheme = new GeographicTilingScheme({
  167. numberOfLevelZeroTilesX : 2,
  168. numberOfLevelZeroTilesY : 2,
  169. rectangle : new Rectangle(-Math.PI, -Math.PI, Math.PI, Math.PI),
  170. ellipsoid : options.ellipsoid
  171. });
  172. // Default to mercator projection when projection is undefined
  173. } else if (!defined(data.projection) || data.projection === 'mercator') {
  174. that._tilingScheme = new WebMercatorTilingScheme({
  175. numberOfLevelZeroTilesX : 2,
  176. numberOfLevelZeroTilesY : 2,
  177. ellipsoid : options.ellipsoid
  178. });
  179. } else {
  180. message = 'Unsupported projection ' + data.projection + '.';
  181. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  182. throw new RuntimeError(message);
  183. }
  184. that._ready = true;
  185. that._readyPromise.resolve(true);
  186. TileProviderError.handleSuccess(metadataError);
  187. }
  188. function metadataFailure(e) {
  189. var message = 'An error occurred while accessing ' + metadataResource.url + '.';
  190. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  191. that._readyPromise.reject(new RuntimeError(message));
  192. }
  193. function requestMetadata() {
  194. var metadata = metadataResource.fetchText();
  195. when(metadata, metadataSuccess, metadataFailure);
  196. }
  197. requestMetadata();
  198. }
  199. defineProperties(GoogleEarthEnterpriseMapsProvider.prototype, {
  200. /**
  201. * Gets the URL of the Google Earth MapServer.
  202. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  203. * @type {String}
  204. * @readonly
  205. */
  206. url : {
  207. get : function() {
  208. return this._url;
  209. }
  210. },
  211. /**
  212. * Gets the url path of the data on the Google Earth server.
  213. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  214. * @type {String}
  215. * @readonly
  216. */
  217. path : {
  218. get : function() {
  219. return this._path;
  220. }
  221. },
  222. /**
  223. * Gets the proxy used by this provider.
  224. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  225. * @type {Proxy}
  226. * @readonly
  227. */
  228. proxy : {
  229. get : function() {
  230. return this._resource.proxy;
  231. }
  232. },
  233. /**
  234. * Gets the imagery channel (id) currently being used.
  235. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  236. * @type {Number}
  237. * @readonly
  238. */
  239. channel : {
  240. get : function() {
  241. return this._channel;
  242. }
  243. },
  244. /**
  245. * Gets the width of each tile, in pixels. This function should
  246. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  247. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  248. * @type {Number}
  249. * @readonly
  250. */
  251. tileWidth : {
  252. get : function() {
  253. //>>includeStart('debug', pragmas.debug);
  254. if (!this._ready) {
  255. throw new DeveloperError('tileWidth must not be called before the imagery provider is ready.');
  256. }
  257. //>>includeEnd('debug');
  258. return this._tileWidth;
  259. }
  260. },
  261. /**
  262. * Gets the height of each tile, in pixels. This function should
  263. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  264. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  265. * @type {Number}
  266. * @readonly
  267. */
  268. tileHeight : {
  269. get : function() {
  270. //>>includeStart('debug', pragmas.debug);
  271. if (!this._ready) {
  272. throw new DeveloperError('tileHeight must not be called before the imagery provider is ready.');
  273. }
  274. //>>includeEnd('debug');
  275. return this._tileHeight;
  276. }
  277. },
  278. /**
  279. * Gets the maximum level-of-detail that can be requested. This function should
  280. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  281. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  282. * @type {Number}
  283. * @readonly
  284. */
  285. maximumLevel : {
  286. get : function() {
  287. //>>includeStart('debug', pragmas.debug);
  288. if (!this._ready) {
  289. throw new DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
  290. }
  291. //>>includeEnd('debug');
  292. return this._maximumLevel;
  293. }
  294. },
  295. /**
  296. * Gets the minimum level-of-detail that can be requested. This function should
  297. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  298. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  299. * @type {Number}
  300. * @readonly
  301. */
  302. minimumLevel : {
  303. get : function() {
  304. //>>includeStart('debug', pragmas.debug);
  305. if (!this._ready) {
  306. throw new DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
  307. }
  308. //>>includeEnd('debug');
  309. return 0;
  310. }
  311. },
  312. /**
  313. * Gets the tiling scheme used by this provider. This function should
  314. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  315. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  316. * @type {TilingScheme}
  317. * @readonly
  318. */
  319. tilingScheme : {
  320. get : function() {
  321. //>>includeStart('debug', pragmas.debug);
  322. if (!this._ready) {
  323. throw new DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
  324. }
  325. //>>includeEnd('debug');
  326. return this._tilingScheme;
  327. }
  328. },
  329. /**
  330. * Gets the version of the data used by this provider. This function should
  331. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  332. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  333. * @type {Number}
  334. * @readonly
  335. */
  336. version : {
  337. get : function() {
  338. //>>includeStart('debug', pragmas.debug);
  339. if (!this._ready) {
  340. throw new DeveloperError('version must not be called before the imagery provider is ready.');
  341. }
  342. //>>includeEnd('debug');
  343. return this._version;
  344. }
  345. },
  346. /**
  347. * Gets the type of data that is being requested from the provider. This function should
  348. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  349. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  350. * @type {String}
  351. * @readonly
  352. */
  353. requestType : {
  354. get : function() {
  355. //>>includeStart('debug', pragmas.debug);
  356. if (!this._ready) {
  357. throw new DeveloperError('requestType must not be called before the imagery provider is ready.');
  358. }
  359. //>>includeEnd('debug');
  360. return this._requestType;
  361. }
  362. },
  363. /**
  364. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  365. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  366. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  367. * @type {Rectangle}
  368. * @readonly
  369. */
  370. rectangle : {
  371. get : function() {
  372. //>>includeStart('debug', pragmas.debug);
  373. if (!this._ready) {
  374. throw new DeveloperError('rectangle must not be called before the imagery provider is ready.');
  375. }
  376. //>>includeEnd('debug');
  377. return this._tilingScheme.rectangle;
  378. }
  379. },
  380. /**
  381. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  382. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  383. * returns undefined, no tiles are filtered. This function should
  384. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  385. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  386. * @type {TileDiscardPolicy}
  387. * @readonly
  388. */
  389. tileDiscardPolicy : {
  390. get : function() {
  391. //>>includeStart('debug', pragmas.debug);
  392. if (!this._ready) {
  393. throw new DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
  394. }
  395. //>>includeEnd('debug');
  396. return this._tileDiscardPolicy;
  397. }
  398. },
  399. /**
  400. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  401. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  402. * are passed an instance of {@link TileProviderError}.
  403. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  404. * @type {Event}
  405. * @readonly
  406. */
  407. errorEvent : {
  408. get : function() {
  409. return this._errorEvent;
  410. }
  411. },
  412. /**
  413. * Gets a value indicating whether or not the provider is ready for use.
  414. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  415. * @type {Boolean}
  416. * @readonly
  417. */
  418. ready : {
  419. get : function() {
  420. return this._ready;
  421. }
  422. },
  423. /**
  424. * Gets a promise that resolves to true when the provider is ready for use.
  425. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  426. * @type {Promise.<Boolean>}
  427. * @readonly
  428. */
  429. readyPromise : {
  430. get : function() {
  431. return this._readyPromise.promise;
  432. }
  433. },
  434. /**
  435. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  436. * the source of the imagery. This function should not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  437. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  438. * @type {Credit}
  439. * @readonly
  440. */
  441. credit : {
  442. get : function() {
  443. return this._credit;
  444. }
  445. },
  446. /**
  447. * Gets a value indicating whether or not the images provided by this imagery provider
  448. * include an alpha channel. If this property is false, an alpha channel, if present, will
  449. * be ignored. If this property is true, any images without an alpha channel will be treated
  450. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  451. * and texture upload time are reduced.
  452. * @memberof GoogleEarthEnterpriseMapsProvider.prototype
  453. * @type {Boolean}
  454. * @readonly
  455. */
  456. hasAlphaChannel : {
  457. get : function() {
  458. return true;
  459. }
  460. }
  461. });
  462. /**
  463. * Gets the credits to be displayed when a given tile is displayed.
  464. *
  465. * @param {Number} x The tile X coordinate.
  466. * @param {Number} y The tile Y coordinate.
  467. * @param {Number} level The tile level;
  468. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  469. *
  470. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  471. */
  472. GoogleEarthEnterpriseMapsProvider.prototype.getTileCredits = function(x, y, level) {
  473. return undefined;
  474. };
  475. /**
  476. * Requests the image for a given tile. This function should
  477. * not be called before {@link GoogleEarthEnterpriseMapsProvider#ready} returns true.
  478. *
  479. * @param {Number} x The tile X coordinate.
  480. * @param {Number} y The tile Y coordinate.
  481. * @param {Number} level The tile level.
  482. * @param {Request} [request] The request object. Intended for internal use only.
  483. * @returns {Promise.<Image|Canvas>|undefined} A promise for the image that will resolve when the image is available, or
  484. * undefined if there are too many active requests to the server, and the request
  485. * should be retried later. The resolved image may be either an
  486. * Image or a Canvas DOM object.
  487. *
  488. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  489. */
  490. GoogleEarthEnterpriseMapsProvider.prototype.requestImage = function(x, y, level, request) {
  491. //>>includeStart('debug', pragmas.debug);
  492. if (!this._ready) {
  493. throw new DeveloperError('requestImage must not be called before the imagery provider is ready.');
  494. }
  495. //>>includeEnd('debug');
  496. var resource = this._resource.getDerivedResource({
  497. url: 'query',
  498. request: request,
  499. queryParameters: {
  500. request: this._requestType,
  501. channel: this._channel,
  502. version: this._version,
  503. x: x,
  504. y: y,
  505. z: level + 1 // Google Earth starts with a zoom level of 1, not 0
  506. }
  507. });
  508. return ImageryProvider.loadImage(this, resource);
  509. };
  510. /**
  511. * Picking features is not currently supported by this imagery provider, so this function simply returns
  512. * undefined.
  513. *
  514. * @param {Number} x The tile X coordinate.
  515. * @param {Number} y The tile Y coordinate.
  516. * @param {Number} level The tile level.
  517. * @param {Number} longitude The longitude at which to pick features.
  518. * @param {Number} latitude The latitude at which to pick features.
  519. * @return {Promise.<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  520. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  521. * instances. The array may be empty if no features are found at the given location.
  522. * It may also be undefined if picking is not supported.
  523. */
  524. GoogleEarthEnterpriseMapsProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  525. return undefined;
  526. };
  527. GoogleEarthEnterpriseMapsProvider._logoUrl = undefined;
  528. defineProperties(GoogleEarthEnterpriseMapsProvider, {
  529. /**
  530. * Gets or sets the URL to the Google Earth logo for display in the credit.
  531. * @memberof GoogleEarthEnterpriseMapsProvider
  532. * @type {String}
  533. */
  534. logoUrl: {
  535. get: function() {
  536. if (!defined(GoogleEarthEnterpriseMapsProvider._logoUrl)) {
  537. GoogleEarthEnterpriseMapsProvider._logoUrl = buildModuleUrl('Assets/Images/google_earth_credit.png');
  538. }
  539. return GoogleEarthEnterpriseMapsProvider._logoUrl;
  540. },
  541. set: function(value) {
  542. //>>includeStart('debug', pragmas.debug);
  543. Check.defined('value', value);
  544. //>>includeEnd('debug');
  545. GoogleEarthEnterpriseMapsProvider._logoUrl = value;
  546. }
  547. }
  548. });
  549. export default GoogleEarthEnterpriseMapsProvider;