PropertyTable.js 13 KB

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