PropertyTable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import defined from "../Core/defined.js";
  5. import JsonMetadataTable from "./JsonMetadataTable.js";
  6. /**
  7. * A property table for use with the <code>EXT_structural_metadata</code> extension or
  8. * legacy <code>EXT_feature_metadata</code> glTF extension. It also includes some
  9. * options to be compatible with the 3D Tiles 1.0 batch table.
  10. * <p>
  11. * For batch tables, properties are resolved in the following order:
  12. * </p>
  13. * <ol>
  14. * <li>binary properties from options.metadataTable</li>
  15. * <li>JSON properties from options.jsonMetadataTable</li>
  16. * <li>batch table hierarchy properties from options.batchTableHierarchy</li>
  17. * </ol>
  18. * <p>
  19. * See the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata Extension} as well as the
  20. * previous {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension} for glTF.
  21. * </p>
  22. *
  23. * @param {object} options Object with the following properties:
  24. * @param {string} [options.name] Human-readable name to describe the table
  25. * @param {string|number} [options.id] A unique id to identify the property table, useful for debugging. For <code>EXT_structural_metadata</code>, this is the array index in the property tables array, for <code>EXT_feature_metadata</code> this is the dictionary key in the property tables dictionary.
  26. * @param {number} options.count The number of features in the table.
  27. * @param {MetadataTable} [options.metadataTable] A table of binary properties.
  28. * @param {JsonMetadataTable} [options.jsonMetadataTable] For compatibility with the old batch table, free-form JSON properties can be passed in.
  29. * @param {BatchTableHierarchy} [options.batchTableHierarchy] For compatibility with the <code>3DTILES_batch_table_hierarchy</code> extension, a hierarchy can be provided.
  30. * @param {object} [options.extras] Extra user-defined properties
  31. * @param {object} [options.extensions] An object containing extensions
  32. *
  33. * @alias PropertyTable
  34. * @constructor
  35. *
  36. * @private
  37. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  38. */
  39. function PropertyTable(options) {
  40. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.typeOf.number("options.count", options.count);
  43. //>>includeEnd('debug');
  44. this._name = options.name;
  45. this._id = options.id;
  46. this._count = options.count;
  47. this._extras = options.extras;
  48. this._extensions = options.extensions;
  49. this._metadataTable = options.metadataTable;
  50. this._jsonMetadataTable = options.jsonMetadataTable;
  51. this._batchTableHierarchy = options.batchTableHierarchy;
  52. }
  53. Object.defineProperties(PropertyTable.prototype, {
  54. /**
  55. * A human-readable name for this table
  56. *
  57. * @memberof PropertyTable.prototype
  58. * @type {string}
  59. * @readonly
  60. * @private
  61. */
  62. name: {
  63. get: function () {
  64. return this._name;
  65. },
  66. },
  67. /**
  68. * An identifier for this table. Useful for debugging.
  69. *
  70. * @memberof PropertyTable.prototype
  71. * @type {string|number}
  72. * @readonly
  73. * @private
  74. */
  75. id: {
  76. get: function () {
  77. return this._id;
  78. },
  79. },
  80. /**
  81. * The number of features in the table.
  82. *
  83. * @memberof PropertyTable.prototype
  84. * @type {number}
  85. * @readonly
  86. * @private
  87. */
  88. count: {
  89. get: function () {
  90. return this._count;
  91. },
  92. },
  93. /**
  94. * The class that properties conform to.
  95. *
  96. * @memberof PropertyTable.prototype
  97. * @type {MetadataClass}
  98. * @readonly
  99. */
  100. class: {
  101. get: function () {
  102. if (defined(this._metadataTable)) {
  103. return this._metadataTable.class;
  104. }
  105. return undefined;
  106. },
  107. },
  108. /**
  109. * Extra user-defined properties.
  110. *
  111. * @memberof PropertyTable.prototype
  112. * @type {*}
  113. * @readonly
  114. * @private
  115. */
  116. extras: {
  117. get: function () {
  118. return this._extras;
  119. },
  120. },
  121. /**
  122. * An object containing extensions.
  123. *
  124. * @memberof PropertyTable.prototype
  125. * @type {object}
  126. * @readonly
  127. * @private
  128. */
  129. extensions: {
  130. get: function () {
  131. return this._extensions;
  132. },
  133. },
  134. /**
  135. * Get the total amount of binary metadata stored in memory. This does
  136. * not include JSON metadata
  137. *
  138. * @memberof PropertyTable.prototype
  139. * @type {number}
  140. * @readonly
  141. * @private
  142. */
  143. byteLength: {
  144. get: function () {
  145. let totalByteLength = 0;
  146. if (defined(this._metadataTable)) {
  147. totalByteLength += this._metadataTable.byteLength;
  148. }
  149. if (defined(this._batchTableHierarchy)) {
  150. totalByteLength += this._batchTableHierarchy.byteLength;
  151. }
  152. return totalByteLength;
  153. },
  154. },
  155. });
  156. /**
  157. * Returns whether the feature has this property. For compatibility with the <code>3DTILES_batch_table_hierarchy</code> extension, this is computed for a specific feature.
  158. *
  159. * @param {number} index The index of the feature.
  160. * @param {string} propertyId The case-sensitive ID of the property.
  161. * @returns {boolean} Whether the feature has this property.
  162. * @private
  163. */
  164. PropertyTable.prototype.hasProperty = function (index, propertyId) {
  165. //>>includeStart('debug', pragmas.debug);
  166. Check.typeOf.number("index", index);
  167. Check.typeOf.string("propertyId", propertyId);
  168. //>>includeEnd('debug');
  169. if (
  170. defined(this._metadataTable) &&
  171. this._metadataTable.hasProperty(propertyId)
  172. ) {
  173. return true;
  174. }
  175. if (
  176. defined(this._batchTableHierarchy) &&
  177. this._batchTableHierarchy.hasProperty(index, propertyId)
  178. ) {
  179. return true;
  180. }
  181. if (
  182. defined(this._jsonMetadataTable) &&
  183. this._jsonMetadataTable.hasProperty(propertyId)
  184. ) {
  185. return true;
  186. }
  187. return false;
  188. };
  189. /**
  190. * Returns whether the feature has a property with the given semantic.
  191. *
  192. * @param {string} semantic The case-sensitive semantic of the property.
  193. * @returns {boolean} Whether the feature has a property with the given semantic.
  194. * @private
  195. */
  196. PropertyTable.prototype.hasPropertyBySemantic = function (index, semantic) {
  197. //>>includeStart('debug', pragmas.debug);
  198. Check.typeOf.number("index", index);
  199. Check.typeOf.string("semantic", semantic);
  200. //>>includeEnd('debug');
  201. if (defined(this._metadataTable)) {
  202. return this._metadataTable.hasPropertyBySemantic(semantic);
  203. }
  204. return false;
  205. };
  206. /**
  207. * Returns whether any feature has this property.
  208. * This is mainly useful for checking whether a property exists in the class
  209. * hierarchy when using the <code>3DTILES_batch_table_hierarchy</code> extension.
  210. *
  211. * @param {string} propertyId The case-sensitive ID of the property.
  212. * @returns {boolean} Whether any feature has this property.
  213. * @private
  214. */
  215. PropertyTable.prototype.propertyExists = function (propertyId) {
  216. //>>includeStart('debug', pragmas.debug);
  217. Check.typeOf.string("propertyId", propertyId);
  218. //>>includeEnd('debug');
  219. if (
  220. defined(this._metadataTable) &&
  221. this._metadataTable.hasProperty(propertyId)
  222. ) {
  223. return true;
  224. }
  225. if (
  226. defined(this._batchTableHierarchy) &&
  227. this._batchTableHierarchy.propertyExists(propertyId)
  228. ) {
  229. return true;
  230. }
  231. if (
  232. defined(this._jsonMetadataTable) &&
  233. this._jsonMetadataTable.hasProperty(propertyId)
  234. ) {
  235. return true;
  236. }
  237. return false;
  238. };
  239. /**
  240. * Returns whether any feature has a property with the given semantic.
  241. *
  242. * @param {string} semantic The case-sensitive semantic of the property.
  243. * @returns {boolean} Whether any feature has a property with the given semantic.
  244. * @private
  245. */
  246. PropertyTable.prototype.propertyExistsBySemantic = function (semantic) {
  247. //>>includeStart('debug', pragmas.debug);
  248. Check.typeOf.string("semantic", semantic);
  249. //>>includeEnd('debug');
  250. if (defined(this._metadataTable)) {
  251. return this._metadataTable.hasPropertyBySemantic(semantic);
  252. }
  253. return false;
  254. };
  255. const scratchResults = [];
  256. /**
  257. * Returns an array of property IDs. For compatibility with the <code>3DTILES_batch_table_hierarchy</code> extension, this is computed for a specific feature.
  258. *
  259. * @param {number} index The index of the feature.
  260. * @param {string[]} [results] An array into which to store the results.
  261. * @returns {string[]} The property IDs.
  262. * @private
  263. */
  264. PropertyTable.prototype.getPropertyIds = function (index, results) {
  265. results = defined(results) ? results : [];
  266. results.length = 0;
  267. if (defined(this._metadataTable)) {
  268. // concat in place to avoid unnecessary array allocation
  269. results.push.apply(
  270. results,
  271. this._metadataTable.getPropertyIds(scratchResults)
  272. );
  273. }
  274. if (defined(this._batchTableHierarchy)) {
  275. results.push.apply(
  276. results,
  277. this._batchTableHierarchy.getPropertyIds(index, scratchResults)
  278. );
  279. }
  280. if (defined(this._jsonMetadataTable)) {
  281. results.push.apply(
  282. results,
  283. this._jsonMetadataTable.getPropertyIds(scratchResults)
  284. );
  285. }
  286. return results;
  287. };
  288. /**
  289. * Returns a copy of the value of the property with the given ID.
  290. * <p>
  291. * If the property is normalized the normalized value is returned.
  292. * </p>
  293. *
  294. * @param {number} index The index of the feature.
  295. * @param {string} propertyId The case-sensitive ID of the property.
  296. * @returns {*} The value of the property or <code>undefined</code> if the feature does not have this property.
  297. * @private
  298. */
  299. PropertyTable.prototype.getProperty = function (index, propertyId) {
  300. let result;
  301. if (defined(this._metadataTable)) {
  302. result = this._metadataTable.getProperty(index, propertyId);
  303. if (defined(result)) {
  304. return result;
  305. }
  306. }
  307. if (defined(this._batchTableHierarchy)) {
  308. result = this._batchTableHierarchy.getProperty(index, propertyId);
  309. if (defined(result)) {
  310. return result;
  311. }
  312. }
  313. if (defined(this._jsonMetadataTable)) {
  314. result = this._jsonMetadataTable.getProperty(index, propertyId);
  315. if (defined(result)) {
  316. return result;
  317. }
  318. }
  319. return undefined;
  320. };
  321. /**
  322. * Sets the value of the property with the given ID. If the property did not
  323. * exist, it will be created as a JSON metadata property
  324. *
  325. * <p>
  326. * If the property is normalized a normalized value must be provided to this function.
  327. * </p>
  328. *
  329. * @param {number} index The index of the feature.
  330. * @param {string} propertyId The case-sensitive ID of the property.
  331. * @param {*} value The value of the property that will be copied.
  332. * @private
  333. */
  334. PropertyTable.prototype.setProperty = function (index, propertyId, value) {
  335. if (
  336. defined(this._metadataTable) &&
  337. this._metadataTable.setProperty(index, propertyId, value)
  338. ) {
  339. return;
  340. }
  341. if (
  342. defined(this._batchTableHierarchy) &&
  343. this._batchTableHierarchy.setProperty(index, propertyId, value)
  344. ) {
  345. return;
  346. }
  347. // Ensure we have a table for JSON properties
  348. if (!defined(this._jsonMetadataTable)) {
  349. this._jsonMetadataTable = new JsonMetadataTable({
  350. count: this._count,
  351. properties: {},
  352. });
  353. }
  354. // JsonMetadataTable will handle creating a new property at runtime.
  355. this._jsonMetadataTable.setProperty(index, propertyId, value);
  356. };
  357. /**
  358. * Returns a copy of the value of the property with the given semantic.
  359. * <p>
  360. * This only operates on the underlying {@link MetadataTable} (if present) as
  361. * {@link JsonMetadataTable} and {@link BatchTableHierarchy} do not have
  362. * semantics.
  363. * </p>
  364. *
  365. * @param {number} index The index of the feature.
  366. * @param {string} semantic The case-sensitive semantic of the property.
  367. * @returns {*} The value of the property or <code>undefined</code> if the feature does not have this semantic.
  368. * @private
  369. */
  370. PropertyTable.prototype.getPropertyBySemantic = function (index, semantic) {
  371. if (defined(this._metadataTable)) {
  372. return this._metadataTable.getPropertyBySemantic(index, semantic);
  373. }
  374. return undefined;
  375. };
  376. /**
  377. * Sets the value of the property with the given semantic.
  378. * <p>
  379. * This only operates on the underlying {@link MetadataTable} (if present) as
  380. * {@link JsonMetadataTable} and {@link BatchTableHierarchy} do not have
  381. * semantics.
  382. * </p>
  383. *
  384. * @param {number} index The index of the feature.
  385. * @param {string} semantic The case-sensitive semantic of the property.
  386. * @param {*} value The value of the property that will be copied.
  387. * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  388. * @private
  389. */
  390. PropertyTable.prototype.setPropertyBySemantic = function (
  391. index,
  392. semantic,
  393. value
  394. ) {
  395. if (defined(this._metadataTable)) {
  396. return this._metadataTable.setPropertyBySemantic(index, semantic, value);
  397. }
  398. return false;
  399. };
  400. /**
  401. * Returns a typed array containing the property values for a given propertyId.
  402. * <p>
  403. * This only operates on the underlying {@link MetadataTable} (if present) as
  404. * {@link JsonMetadataTable} and {@link BatchTableHierarchy} do not store
  405. * values in typed arrays.
  406. * </p>
  407. *
  408. * @param {string} propertyId The case-sensitive ID of the property.
  409. * @returns {*} The typed array containing the property values or <code>undefined</code> if the property values are not stored in a typed array.
  410. *
  411. * @private
  412. */
  413. PropertyTable.prototype.getPropertyTypedArray = function (propertyId) {
  414. //>>includeStart('debug', pragmas.debug);
  415. Check.typeOf.string("propertyId", propertyId);
  416. //>>includeEnd('debug');
  417. if (defined(this._metadataTable)) {
  418. return this._metadataTable.getPropertyTypedArray(propertyId);
  419. }
  420. return undefined;
  421. };
  422. /**
  423. * Returns a typed array containing the property values for the property with the given semantic.
  424. * <p>
  425. * This only operates on the underlying {@link MetadataTable} (if present) as
  426. * {@link JsonMetadataTable} and {@link BatchTableHierarchy} do not have
  427. * semantics.
  428. * </p>
  429. *
  430. * @param {string} semantic The case-sensitive semantic of the property.
  431. * @returns {*} The typed array containing the property values or <code>undefined</code> if the property values are not stored in a typed array.
  432. *
  433. * @private
  434. */
  435. PropertyTable.prototype.getPropertyTypedArrayBySemantic = function (semantic) {
  436. //>>includeStart('debug', pragmas.debug);
  437. Check.typeOf.string("semantic", semantic);
  438. //>>includeEnd('debug');
  439. if (defined(this._metadataTable)) {
  440. return this._metadataTable.getPropertyTypedArrayBySemantic(semantic);
  441. }
  442. return undefined;
  443. };
  444. function checkFeatureId(featureId, featuresLength) {
  445. if (!defined(featureId) || featureId < 0 || featureId >= featuresLength) {
  446. throw new DeveloperError(
  447. `featureId is required and must be between zero and featuresLength - 1 (${featuresLength}` -
  448. +")."
  449. );
  450. }
  451. }
  452. PropertyTable.prototype.isClass = function (featureId, className) {
  453. //>>includeStart('debug', pragmas.debug);
  454. checkFeatureId(featureId, this.count);
  455. Check.typeOf.string("className", className);
  456. //>>includeEnd('debug');
  457. const hierarchy = this._batchTableHierarchy;
  458. if (!defined(hierarchy)) {
  459. return false;
  460. }
  461. return hierarchy.isClass(featureId, className);
  462. };
  463. PropertyTable.prototype.isExactClass = function (featureId, className) {
  464. //>>includeStart('debug', pragmas.debug);
  465. checkFeatureId(featureId, this.count);
  466. Check.typeOf.string("className", className);
  467. //>>includeEnd('debug');
  468. return this.getExactClassName(featureId) === className;
  469. };
  470. PropertyTable.prototype.getExactClassName = function (featureId) {
  471. //>>includeStart('debug', pragmas.debug);
  472. checkFeatureId(featureId, this.count);
  473. //>>includeEnd('debug');
  474. const hierarchy = this._batchTableHierarchy;
  475. if (!defined(hierarchy)) {
  476. return undefined;
  477. }
  478. return hierarchy.getClassName(featureId);
  479. };
  480. export default PropertyTable;