智慧水务管理系统 - 精河县供水工程综合管理平台

GoogleEarthEnterpriseMapsProvider.js 18KB

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