GoogleEarthEnterpriseMetadata.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. import protobuf from "../ThirdParty/protobufjs.js";
  2. import buildModuleUrl from "./buildModuleUrl.js";
  3. import Check from "./Check.js";
  4. import Credit from "./Credit.js";
  5. import defaultValue from "./defaultValue.js";
  6. import defined from "./defined.js";
  7. import GoogleEarthEnterpriseTileInformation from "./GoogleEarthEnterpriseTileInformation.js";
  8. import isBitSet from "./isBitSet.js";
  9. import loadAndExecuteScript from "./loadAndExecuteScript.js";
  10. import CesiumMath from "./Math.js";
  11. import Request from "./Request.js";
  12. import Resource from "./Resource.js";
  13. import RuntimeError from "./RuntimeError.js";
  14. import TaskProcessor from "./TaskProcessor.js";
  15. function stringToBuffer(str) {
  16. const len = str.length;
  17. const buffer = new ArrayBuffer(len);
  18. const ui8 = new Uint8Array(buffer);
  19. for (let i = 0; i < len; ++i) {
  20. ui8[i] = str.charCodeAt(i);
  21. }
  22. return buffer;
  23. }
  24. // Decodes packet with a key that has been around since the beginning of Google Earth Enterprise
  25. const defaultKey = stringToBuffer(
  26. "\x45\xf4\xbd\x0b\x79\xe2\x6a\x45\x22\x05\x92\x2c\x17\xcd\x06\x71\xf8\x49\x10\x46\x67\x51\x00\x42\x25\xc6\xe8\x61\x2c\x66\x29\x08\xc6\x34\xdc\x6a\x62\x25\x79\x0a\x77\x1d\x6d\x69\xd6\xf0\x9c\x6b\x93\xa1\xbd\x4e\x75\xe0\x41\x04\x5b\xdf\x40\x56\x0c\xd9\xbb\x72\x9b\x81\x7c\x10\x33\x53\xee\x4f\x6c\xd4\x71\x05\xb0\x7b\xc0\x7f\x45\x03\x56\x5a\xad\x77\x55\x65\x0b\x33\x92\x2a\xac\x19\x6c\x35\x14\xc5\x1d\x30\x73\xf8\x33\x3e\x6d\x46\x38\x4a\xb4\xdd\xf0\x2e\xdd\x17\x75\x16\xda\x8c\x44\x74\x22\x06\xfa\x61\x22\x0c\x33\x22\x53\x6f\xaf\x39\x44\x0b\x8c\x0e\x39\xd9\x39\x13\x4c\xb9\xbf\x7f\xab\x5c\x8c\x50\x5f\x9f\x22\x75\x78\x1f\xe9\x07\x71\x91\x68\x3b\xc1\xc4\x9b\x7f\xf0\x3c\x56\x71\x48\x82\x05\x27\x55\x66\x59\x4e\x65\x1d\x98\x75\xa3\x61\x46\x7d\x61\x3f\x15\x41\x00\x9f\x14\x06\xd7\xb4\x34\x4d\xce\x13\x87\x46\xb0\x1a\xd5\x05\x1c\xb8\x8a\x27\x7b\x8b\xdc\x2b\xbb\x4d\x67\x30\xc8\xd1\xf6\x5c\x8f\x50\xfa\x5b\x2f\x46\x9b\x6e\x35\x18\x2f\x27\x43\x2e\xeb\x0a\x0c\x5e\x10\x05\x10\xa5\x73\x1b\x65\x34\xe5\x6c\x2e\x6a\x43\x27\x63\x14\x23\x55\xa9\x3f\x71\x7b\x67\x43\x7d\x3a\xaf\xcd\xe2\x54\x55\x9c\xfd\x4b\xc6\xe2\x9f\x2f\x28\xed\xcb\x5c\xc6\x2d\x66\x07\x88\xa7\x3b\x2f\x18\x2a\x22\x4e\x0e\xb0\x6b\x2e\xdd\x0d\x95\x7d\x7d\x47\xba\x43\xb2\x11\xb2\x2b\x3e\x4d\xaa\x3e\x7d\xe6\xce\x49\x89\xc6\xe6\x78\x0c\x61\x31\x05\x2d\x01\xa4\x4f\xa5\x7e\x71\x20\x88\xec\x0d\x31\xe8\x4e\x0b\x00\x6e\x50\x68\x7d\x17\x3d\x08\x0d\x17\x95\xa6\x6e\xa3\x68\x97\x24\x5b\x6b\xf3\x17\x23\xf3\xb6\x73\xb3\x0d\x0b\x40\xc0\x9f\xd8\x04\x51\x5d\xfa\x1a\x17\x22\x2e\x15\x6a\xdf\x49\x00\xb9\xa0\x77\x55\xc6\xef\x10\x6a\xbf\x7b\x47\x4c\x7f\x83\x17\x05\xee\xdc\xdc\x46\x85\xa9\xad\x53\x07\x2b\x53\x34\x06\x07\xff\x14\x94\x59\x19\x02\xe4\x38\xe8\x31\x83\x4e\xb9\x58\x46\x6b\xcb\x2d\x23\x86\x92\x70\x00\x35\x88\x22\xcf\x31\xb2\x26\x2f\xe7\xc3\x75\x2d\x36\x2c\x72\x74\xb0\x23\x47\xb7\xd3\xd1\x26\x16\x85\x37\x72\xe2\x00\x8c\x44\xcf\x10\xda\x33\x2d\x1a\xde\x60\x86\x69\x23\x69\x2a\x7c\xcd\x4b\x51\x0d\x95\x54\x39\x77\x2e\x29\xea\x1b\xa6\x50\xa2\x6a\x8f\x6f\x50\x99\x5c\x3e\x54\xfb\xef\x50\x5b\x0b\x07\x45\x17\x89\x6d\x28\x13\x77\x37\x1d\xdb\x8e\x1e\x4a\x05\x66\x4a\x6f\x99\x20\xe5\x70\xe2\xb9\x71\x7e\x0c\x6d\x49\x04\x2d\x7a\xfe\x72\xc7\xf2\x59\x30\x8f\xbb\x02\x5d\x73\xe5\xc9\x20\xea\x78\xec\x20\x90\xf0\x8a\x7f\x42\x17\x7c\x47\x19\x60\xb0\x16\xbd\x26\xb7\x71\xb6\xc7\x9f\x0e\xd1\x33\x82\x3d\xd3\xab\xee\x63\x99\xc8\x2b\x53\xa0\x44\x5c\x71\x01\xc6\xcc\x44\x1f\x32\x4f\x3c\xca\xc0\x29\x3d\x52\xd3\x61\x19\x58\xa9\x7d\x65\xb4\xdc\xcf\x0d\xf4\x3d\xf1\x08\xa9\x42\xda\x23\x09\xd8\xbf\x5e\x50\x49\xf8\x4d\xc0\xcb\x47\x4c\x1c\x4f\xf7\x7b\x2b\xd8\x16\x18\xc5\x31\x92\x3b\xb5\x6f\xdc\x6c\x0d\x92\x88\x16\xd1\x9e\xdb\x3f\xe2\xe9\xda\x5f\xd4\x84\xe2\x46\x61\x5a\xde\x1c\x55\xcf\xa4\x00\xbe\xfd\xce\x67\xf1\x4a\x69\x1c\x97\xe6\x20\x48\xd8\x5d\x7f\x7e\xae\x71\x20\x0e\x4e\xae\xc0\x56\xa9\x91\x01\x3c\x82\x1d\x0f\x72\xe7\x76\xec\x29\x49\xd6\x5d\x2d\x83\xe3\xdb\x36\x06\xa9\x3b\x66\x13\x97\x87\x6a\xd5\xb6\x3d\x50\x5e\x52\xb9\x4b\xc7\x73\x57\x78\xc9\xf4\x2e\x59\x07\x95\x93\x6f\xd0\x4b\x17\x57\x19\x3e\x27\x27\xc7\x60\xdb\x3b\xed\x9a\x0e\x53\x44\x16\x3e\x3f\x8d\x92\x6d\x77\xa2\x0a\xeb\x3f\x52\xa8\xc6\x55\x5e\x31\x49\x37\x85\xf4\xc5\x1f\x26\x2d\xa9\x1c\xbf\x8b\x27\x54\xda\xc3\x6a\x20\xe5\x2a\x78\x04\xb0\xd6\x90\x70\x72\xaa\x8b\x68\xbd\x88\xf7\x02\x5f\x48\xb1\x7e\xc0\x58\x4c\x3f\x66\x1a\xf9\x3e\xe1\x65\xc0\x70\xa7\xcf\x38\x69\xaf\xf0\x56\x6c\x64\x49\x9c\x27\xad\x78\x74\x4f\xc2\x87\xde\x56\x39\x00\xda\x77\x0b\xcb\x2d\x1b\x89\xfb\x35\x4f\x02\xf5\x08\x51\x13\x60\xc1\x0a\x5a\x47\x4d\x26\x1c\x33\x30\x78\xda\xc0\x9c\x46\x47\xe2\x5b\x79\x60\x49\x6e\x37\x67\x53\x0a\x3e\xe9\xec\x46\x39\xb2\xf1\x34\x0d\xc6\x84\x53\x75\x6e\xe1\x0c\x59\xd9\x1e\xde\x29\x85\x10\x7b\x49\x49\xa5\x77\x79\xbe\x49\x56\x2e\x36\xe7\x0b\x3a\xbb\x4f\x03\x62\x7b\xd2\x4d\x31\x95\x2f\xbd\x38\x7b\xa8\x4f\x21\xe1\xec\x46\x70\x76\x95\x7d\x29\x22\x78\x88\x0a\x90\xdd\x9d\x5c\xda\xde\x19\x51\xcf\xf0\xfc\x59\x52\x65\x7c\x33\x13\xdf\xf3\x48\xda\xbb\x2a\x75\xdb\x60\xb2\x02\x15\xd4\xfc\x19\xed\x1b\xec\x7f\x35\xa8\xff\x28\x31\x07\x2d\x12\xc8\xdc\x88\x46\x7c\x8a\x5b\x22"
  27. );
  28. /**
  29. * Provides metadata using the Google Earth Enterprise REST API. This is used by the GoogleEarthEnterpriseImageryProvider
  30. * and GoogleEarthEnterpriseTerrainProvider to share metadata requests.
  31. *
  32. * @alias GoogleEarthEnterpriseMetadata
  33. * @constructor
  34. *
  35. * @param {Resource|String} resourceOrUrl The url of the Google Earth Enterprise server hosting the imagery
  36. *
  37. * @see GoogleEarthEnterpriseImageryProvider
  38. * @see GoogleEarthEnterpriseTerrainProvider
  39. *
  40. */
  41. function GoogleEarthEnterpriseMetadata(resourceOrUrl) {
  42. //>>includeStart('debug', pragmas.debug);
  43. Check.defined("resourceOrUrl", resourceOrUrl);
  44. //>>includeEnd('debug');
  45. let url = resourceOrUrl;
  46. if (typeof url !== "string" && !(url instanceof Resource)) {
  47. //>>includeStart('debug', pragmas.debug);
  48. Check.typeOf.string("resourceOrUrl.url", resourceOrUrl.url);
  49. //>>includeEnd('debug');
  50. url = resourceOrUrl.url;
  51. }
  52. const resource = Resource.createIfNeeded(url);
  53. resource.appendForwardSlash();
  54. this._resource = resource;
  55. /**
  56. * True if imagery is available.
  57. * @type {Boolean}
  58. * @default true
  59. */
  60. this.imageryPresent = true;
  61. /**
  62. * True if imagery is sent as a protocol buffer, false if sent as plain images. If undefined we will try both.
  63. * @type {Boolean}
  64. * @default undefined
  65. */
  66. this.protoImagery = undefined;
  67. /**
  68. * True if terrain is available.
  69. * @type {Boolean}
  70. * @default true
  71. */
  72. this.terrainPresent = true;
  73. /**
  74. * Exponent used to compute constant to calculate negative height values.
  75. * @type {Number}
  76. * @default 32
  77. */
  78. this.negativeAltitudeExponentBias = 32;
  79. /**
  80. * Threshold where any numbers smaller are actually negative values. They are multiplied by -2^negativeAltitudeExponentBias.
  81. * @type {Number}
  82. * @default EPSILON12
  83. */
  84. this.negativeAltitudeThreshold = CesiumMath.EPSILON12;
  85. /**
  86. * Dictionary of provider id to copyright strings.
  87. * @type {Object}
  88. * @default {}
  89. */
  90. this.providers = {};
  91. /**
  92. * Key used to decode packets
  93. * @type {ArrayBuffer}
  94. */
  95. this.key = undefined;
  96. this._quadPacketVersion = 1;
  97. this._tileInfo = {};
  98. this._subtreePromises = {};
  99. const that = this;
  100. this._readyPromise = requestDbRoot(this)
  101. .then(function () {
  102. return that.getQuadTreePacket("", that._quadPacketVersion);
  103. })
  104. .then(function () {
  105. return true;
  106. })
  107. .catch(function (e) {
  108. const message = `An error occurred while accessing ${
  109. getMetadataResource(that, "", 1).url
  110. }.`;
  111. return Promise.reject(new RuntimeError(message));
  112. });
  113. }
  114. Object.defineProperties(GoogleEarthEnterpriseMetadata.prototype, {
  115. /**
  116. * Gets the name of the Google Earth Enterprise server.
  117. * @memberof GoogleEarthEnterpriseMetadata.prototype
  118. * @type {String}
  119. * @readonly
  120. */
  121. url: {
  122. get: function () {
  123. return this._resource.url;
  124. },
  125. },
  126. /**
  127. * Gets the proxy used for metadata requests.
  128. * @memberof GoogleEarthEnterpriseMetadata.prototype
  129. * @type {Proxy}
  130. * @readonly
  131. */
  132. proxy: {
  133. get: function () {
  134. return this._resource.proxy;
  135. },
  136. },
  137. /**
  138. * Gets the resource used for metadata requests.
  139. * @memberof GoogleEarthEnterpriseMetadata.prototype
  140. * @type {Resource}
  141. * @readonly
  142. */
  143. resource: {
  144. get: function () {
  145. return this._resource;
  146. },
  147. },
  148. /**
  149. * Gets a promise that resolves to true when the metadata is ready for use.
  150. * @memberof GoogleEarthEnterpriseMetadata.prototype
  151. * @type {Promise.<Boolean>}
  152. * @readonly
  153. */
  154. readyPromise: {
  155. get: function () {
  156. return this._readyPromise;
  157. },
  158. },
  159. });
  160. /**
  161. * Converts a tiles (x, y, level) position into a quadkey used to request an image
  162. * from a Google Earth Enterprise server.
  163. *
  164. * @param {Number} x The tile's x coordinate.
  165. * @param {Number} y The tile's y coordinate.
  166. * @param {Number} level The tile's zoom level.
  167. *
  168. * @see GoogleEarthEnterpriseMetadata#quadKeyToTileXY
  169. */
  170. GoogleEarthEnterpriseMetadata.tileXYToQuadKey = function (x, y, level) {
  171. let quadkey = "";
  172. for (let i = level; i >= 0; --i) {
  173. const bitmask = 1 << i;
  174. let digit = 0;
  175. // Tile Layout
  176. // ___ ___
  177. //| | |
  178. //| 3 | 2 |
  179. //|-------|
  180. //| 0 | 1 |
  181. //|___|___|
  182. //
  183. if (!isBitSet(y, bitmask)) {
  184. // Top Row
  185. digit |= 2;
  186. if (!isBitSet(x, bitmask)) {
  187. // Right to left
  188. digit |= 1;
  189. }
  190. } else if (isBitSet(x, bitmask)) {
  191. // Left to right
  192. digit |= 1;
  193. }
  194. quadkey += digit;
  195. }
  196. return quadkey;
  197. };
  198. /**
  199. * Converts a tile's quadkey used to request an image from a Google Earth Enterprise server into the
  200. * (x, y, level) position.
  201. *
  202. * @param {String} quadkey The tile's quad key
  203. *
  204. * @see GoogleEarthEnterpriseMetadata#tileXYToQuadKey
  205. */
  206. GoogleEarthEnterpriseMetadata.quadKeyToTileXY = function (quadkey) {
  207. let x = 0;
  208. let y = 0;
  209. const level = quadkey.length - 1;
  210. for (let i = level; i >= 0; --i) {
  211. const bitmask = 1 << i;
  212. const digit = +quadkey[level - i];
  213. if (isBitSet(digit, 2)) {
  214. // Top Row
  215. if (!isBitSet(digit, 1)) {
  216. // // Right to left
  217. x |= bitmask;
  218. }
  219. } else {
  220. y |= bitmask;
  221. if (isBitSet(digit, 1)) {
  222. // Left to right
  223. x |= bitmask;
  224. }
  225. }
  226. }
  227. return {
  228. x: x,
  229. y: y,
  230. level: level,
  231. };
  232. };
  233. GoogleEarthEnterpriseMetadata.prototype.isValid = function (quadKey) {
  234. let info = this.getTileInformationFromQuadKey(quadKey);
  235. if (defined(info)) {
  236. return info !== null;
  237. }
  238. let valid = true;
  239. let q = quadKey;
  240. let last;
  241. while (q.length > 1) {
  242. last = q.substring(q.length - 1);
  243. q = q.substring(0, q.length - 1);
  244. info = this.getTileInformationFromQuadKey(q);
  245. if (defined(info)) {
  246. if (!info.hasSubtree() && !info.hasChild(parseInt(last))) {
  247. // We have no subtree or child available at some point in this node's ancestry
  248. valid = false;
  249. }
  250. break;
  251. } else if (info === null) {
  252. // Some node in the ancestry was loaded and said there wasn't a subtree
  253. valid = false;
  254. break;
  255. }
  256. }
  257. return valid;
  258. };
  259. const taskProcessor = new TaskProcessor("decodeGoogleEarthEnterprisePacket");
  260. /**
  261. * Retrieves a Google Earth Enterprise quadtree packet.
  262. *
  263. * @param {String} [quadKey=''] The quadkey to retrieve the packet for.
  264. * @param {Number} [version=1] The cnode version to be used in the request.
  265. * @param {Request} [request] The request object. Intended for internal use only.
  266. *
  267. * @private
  268. */
  269. GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket = function (
  270. quadKey,
  271. version,
  272. request
  273. ) {
  274. version = defaultValue(version, 1);
  275. quadKey = defaultValue(quadKey, "");
  276. const resource = getMetadataResource(this, quadKey, version, request);
  277. const promise = resource.fetchArrayBuffer();
  278. if (!defined(promise)) {
  279. return undefined; // Throttled
  280. }
  281. const tileInfo = this._tileInfo;
  282. const key = this.key;
  283. return promise.then(function (metadata) {
  284. const decodePromise = taskProcessor.scheduleTask(
  285. {
  286. buffer: metadata,
  287. quadKey: quadKey,
  288. type: "Metadata",
  289. key: key,
  290. },
  291. [metadata]
  292. );
  293. return decodePromise.then(function (result) {
  294. let root;
  295. let topLevelKeyLength = -1;
  296. if (quadKey !== "") {
  297. // Root tile has no data except children bits, so put them into the tile info
  298. topLevelKeyLength = quadKey.length + 1;
  299. const top = result[quadKey];
  300. root = tileInfo[quadKey];
  301. root._bits |= top._bits;
  302. delete result[quadKey];
  303. }
  304. // Copy the resulting objects into tileInfo
  305. // Make sure we start with shorter quadkeys first, so we know the parents have
  306. // already been processed. Otherwise we can lose ancestorHasTerrain along the way.
  307. const keys = Object.keys(result);
  308. keys.sort(function (a, b) {
  309. return a.length - b.length;
  310. });
  311. const keysLength = keys.length;
  312. for (let i = 0; i < keysLength; ++i) {
  313. const key = keys[i];
  314. const r = result[key];
  315. if (r !== null) {
  316. const info = GoogleEarthEnterpriseTileInformation.clone(result[key]);
  317. const keyLength = key.length;
  318. if (keyLength === topLevelKeyLength) {
  319. info.setParent(root);
  320. } else if (keyLength > 1) {
  321. const parent = tileInfo[key.substring(0, key.length - 1)];
  322. info.setParent(parent);
  323. }
  324. tileInfo[key] = info;
  325. } else {
  326. tileInfo[key] = null;
  327. }
  328. }
  329. });
  330. });
  331. };
  332. /**
  333. * Populates the metadata subtree down to the specified tile.
  334. *
  335. * @param {Number} x The tile X coordinate.
  336. * @param {Number} y The tile Y coordinate.
  337. * @param {Number} level The tile level.
  338. * @param {Request} [request] The request object. Intended for internal use only.
  339. *
  340. * @returns {Promise<GoogleEarthEnterpriseTileInformation>} A promise that resolves to the tile info for the requested quad key
  341. *
  342. * @private
  343. */
  344. GoogleEarthEnterpriseMetadata.prototype.populateSubtree = function (
  345. x,
  346. y,
  347. level,
  348. request
  349. ) {
  350. const quadkey = GoogleEarthEnterpriseMetadata.tileXYToQuadKey(x, y, level);
  351. return populateSubtree(this, quadkey, request);
  352. };
  353. function populateSubtree(that, quadKey, request) {
  354. const tileInfo = that._tileInfo;
  355. let q = quadKey;
  356. let t = tileInfo[q];
  357. // If we have tileInfo make sure sure it is not a node with a subtree that's not loaded
  358. if (defined(t) && (!t.hasSubtree() || t.hasChildren())) {
  359. return t;
  360. }
  361. while (t === undefined && q.length > 1) {
  362. q = q.substring(0, q.length - 1);
  363. t = tileInfo[q];
  364. }
  365. let subtreeRequest;
  366. const subtreePromises = that._subtreePromises;
  367. let promise = subtreePromises[q];
  368. if (defined(promise)) {
  369. return promise.then(function () {
  370. // Recursively call this in case we need multiple subtree requests
  371. subtreeRequest = new Request({
  372. throttle: request.throttle,
  373. throttleByServer: request.throttleByServer,
  374. type: request.type,
  375. priorityFunction: request.priorityFunction,
  376. });
  377. return populateSubtree(that, quadKey, subtreeRequest);
  378. });
  379. }
  380. // t is either
  381. // null so one of its parents was a leaf node, so this tile doesn't exist
  382. // exists but doesn't have a subtree to request
  383. // undefined so no parent exists - this shouldn't ever happen once the provider is ready
  384. if (!defined(t) || !t.hasSubtree()) {
  385. return Promise.reject(
  386. new RuntimeError(`Couldn't load metadata for tile ${quadKey}`)
  387. );
  388. }
  389. // We need to split up the promise here because when will execute syncronously if getQuadTreePacket
  390. // is already resolved (like in the tests), so subtreePromises will never get cleared out.
  391. // Only the initial request will also remove the promise from subtreePromises.
  392. promise = that.getQuadTreePacket(q, t.cnodeVersion, request);
  393. if (!defined(promise)) {
  394. return undefined;
  395. }
  396. subtreePromises[q] = promise;
  397. return promise
  398. .then(function () {
  399. // Recursively call this in case we need multiple subtree requests
  400. subtreeRequest = new Request({
  401. throttle: request.throttle,
  402. throttleByServer: request.throttleByServer,
  403. type: request.type,
  404. priorityFunction: request.priorityFunction,
  405. });
  406. return populateSubtree(that, quadKey, subtreeRequest);
  407. })
  408. .finally(function () {
  409. delete subtreePromises[q];
  410. });
  411. }
  412. /**
  413. * Gets information about a tile
  414. *
  415. * @param {Number} x The tile X coordinate.
  416. * @param {Number} y The tile Y coordinate.
  417. * @param {Number} level The tile level.
  418. * @returns {GoogleEarthEnterpriseTileInformation|undefined} Information about the tile or undefined if it isn't loaded.
  419. *
  420. * @private
  421. */
  422. GoogleEarthEnterpriseMetadata.prototype.getTileInformation = function (
  423. x,
  424. y,
  425. level
  426. ) {
  427. const quadkey = GoogleEarthEnterpriseMetadata.tileXYToQuadKey(x, y, level);
  428. return this._tileInfo[quadkey];
  429. };
  430. /**
  431. * Gets information about a tile from a quadKey
  432. *
  433. * @param {String} quadkey The quadkey for the tile
  434. * @returns {GoogleEarthEnterpriseTileInformation|undefined} Information about the tile or undefined if it isn't loaded.
  435. *
  436. * @private
  437. */
  438. GoogleEarthEnterpriseMetadata.prototype.getTileInformationFromQuadKey = function (
  439. quadkey
  440. ) {
  441. return this._tileInfo[quadkey];
  442. };
  443. function getMetadataResource(that, quadKey, version, request) {
  444. return that._resource.getDerivedResource({
  445. url: `flatfile?q2-0${quadKey}-q.${version.toString()}`,
  446. request: request,
  447. });
  448. }
  449. let dbrootParser;
  450. let dbrootParserPromise;
  451. function requestDbRoot(that) {
  452. const resource = that._resource.getDerivedResource({
  453. url: "dbRoot.v5",
  454. queryParameters: {
  455. output: "proto",
  456. },
  457. });
  458. if (!defined(dbrootParserPromise)) {
  459. const url = buildModuleUrl("ThirdParty/google-earth-dbroot-parser.js");
  460. const oldValue = window.cesiumGoogleEarthDbRootParser;
  461. dbrootParserPromise = loadAndExecuteScript(url).then(function () {
  462. dbrootParser = window.cesiumGoogleEarthDbRootParser(protobuf);
  463. if (defined(oldValue)) {
  464. window.cesiumGoogleEarthDbRootParser = oldValue;
  465. } else {
  466. delete window.cesiumGoogleEarthDbRootParser;
  467. }
  468. });
  469. }
  470. return dbrootParserPromise
  471. .then(function () {
  472. return resource.fetchArrayBuffer();
  473. })
  474. .then(function (buf) {
  475. const encryptedDbRootProto = dbrootParser.EncryptedDbRootProto.decode(
  476. new Uint8Array(buf)
  477. );
  478. let byteArray = encryptedDbRootProto.encryptionData;
  479. let offset = byteArray.byteOffset;
  480. let end = offset + byteArray.byteLength;
  481. const key = (that.key = byteArray.buffer.slice(offset, end));
  482. byteArray = encryptedDbRootProto.dbrootData;
  483. offset = byteArray.byteOffset;
  484. end = offset + byteArray.byteLength;
  485. const dbRootCompressed = byteArray.buffer.slice(offset, end);
  486. return taskProcessor.scheduleTask(
  487. {
  488. buffer: dbRootCompressed,
  489. type: "DbRoot",
  490. key: key,
  491. },
  492. [dbRootCompressed]
  493. );
  494. })
  495. .then(function (result) {
  496. const dbRoot = dbrootParser.DbRootProto.decode(
  497. new Uint8Array(result.buffer)
  498. );
  499. that.imageryPresent = defaultValue(
  500. dbRoot.imageryPresent,
  501. that.imageryPresent
  502. );
  503. that.protoImagery = dbRoot.protoImagery;
  504. that.terrainPresent = defaultValue(
  505. dbRoot.terrainPresent,
  506. that.terrainPresent
  507. );
  508. if (defined(dbRoot.endSnippet) && defined(dbRoot.endSnippet.model)) {
  509. const model = dbRoot.endSnippet.model;
  510. that.negativeAltitudeExponentBias = defaultValue(
  511. model.negativeAltitudeExponentBias,
  512. that.negativeAltitudeExponentBias
  513. );
  514. that.negativeAltitudeThreshold = defaultValue(
  515. model.compressedNegativeAltitudeThreshold,
  516. that.negativeAltitudeThreshold
  517. );
  518. }
  519. if (defined(dbRoot.databaseVersion)) {
  520. that._quadPacketVersion = defaultValue(
  521. dbRoot.databaseVersion.quadtreeVersion,
  522. that._quadPacketVersion
  523. );
  524. }
  525. const providers = that.providers;
  526. const providerInfo = defaultValue(dbRoot.providerInfo, []);
  527. const count = providerInfo.length;
  528. for (let i = 0; i < count; ++i) {
  529. const provider = providerInfo[i];
  530. const copyrightString = provider.copyrightString;
  531. if (defined(copyrightString)) {
  532. providers[provider.providerId] = new Credit(copyrightString.value);
  533. }
  534. }
  535. })
  536. .catch(function () {
  537. // Just eat the error and use the default values.
  538. console.log(`Failed to retrieve ${resource.url}. Using defaults.`);
  539. that.key = defaultKey;
  540. });
  541. }
  542. export default GoogleEarthEnterpriseMetadata;