JsonMetadataTable.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import MetadataEntity from "./MetadataEntity.js";
  6. /**
  7. * A table for storing free-form JSON metadata, as in the 3D Tiles batch table.
  8. *
  9. * @param {object} options Object with the following properties:
  10. * @param {number} options.count The number of entities in the table.
  11. * @param {Object<string, Array>} options.properties The JSON representation of the metadata table. All the arrays must have exactly options.count elements.
  12. *
  13. * @alias JsonMetadataTable
  14. * @constructor
  15. * @private
  16. */
  17. // An empty class is used because JsonMetadataTable is an older type of metadata table
  18. // that does not have a class definition.
  19. const emptyClass = {};
  20. function JsonMetadataTable(options) {
  21. //>>includeStart('debug', pragmas.debug);
  22. Check.typeOf.number.greaterThan("options.count", options.count, 0);
  23. Check.typeOf.object("options.properties", options.properties);
  24. //>>includeEnd('debug');
  25. this._count = options.count;
  26. this._properties = clone(options.properties, true);
  27. }
  28. /**
  29. * Returns whether the table has this property.
  30. *
  31. * @param {string} propertyId The case-sensitive ID of the property.
  32. * @returns {boolean} Whether the table has this property.
  33. * @private
  34. */
  35. JsonMetadataTable.prototype.hasProperty = function (propertyId) {
  36. return MetadataEntity.hasProperty(propertyId, this._properties, emptyClass);
  37. };
  38. /**
  39. * Returns an array of property IDs.
  40. *
  41. * @param {string[]} [results] An array into which to store the results.
  42. * @returns {string[]} The property IDs.
  43. * @private
  44. */
  45. JsonMetadataTable.prototype.getPropertyIds = function (results) {
  46. return MetadataEntity.getPropertyIds(this._properties, emptyClass, results);
  47. };
  48. /**
  49. * Returns a copy of the value of the property with the given ID.
  50. *
  51. * @param {number} index The index of the entity.
  52. * @param {string} propertyId The case-sensitive ID of the property.
  53. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  54. *
  55. * @exception {DeveloperError} index is out of bounds
  56. * @private
  57. */
  58. JsonMetadataTable.prototype.getProperty = function (index, propertyId) {
  59. //>>includeStart('debug', pragmas.debug);
  60. Check.typeOf.number("index", index);
  61. Check.typeOf.string("propertyId", propertyId);
  62. if (index < 0 || index >= this._count) {
  63. throw new DeveloperError(`index must be in the range [0, ${this._count})`);
  64. }
  65. //>>includeEnd('debug');
  66. const property = this._properties[propertyId];
  67. if (defined(property)) {
  68. return clone(property[index], true);
  69. }
  70. return undefined;
  71. };
  72. /**
  73. * Sets the value of the property with the given ID. If the property did not
  74. * exist, it will be created.
  75. *
  76. * @param {number} index The index of the entity.
  77. * @param {string} propertyId The case-sensitive ID of the property.
  78. * @param {*} value The value of the property that will be copied.
  79. *
  80. * @exception {DeveloperError} index is out of bounds
  81. * @private
  82. */
  83. JsonMetadataTable.prototype.setProperty = function (index, propertyId, value) {
  84. //>>includeStart('debug', pragmas.debug);
  85. Check.typeOf.number("index", index);
  86. Check.typeOf.string("propertyId", propertyId);
  87. if (index < 0 || index >= this._count) {
  88. throw new DeveloperError(`index must be in the range [0, ${this._count})`);
  89. }
  90. //>>includeEnd('debug');
  91. let property = this._properties[propertyId];
  92. if (!defined(property)) {
  93. // Property does not exist. Create it.
  94. property = new Array(this._count);
  95. this._properties[propertyId] = property;
  96. }
  97. property[index] = clone(value, true);
  98. };
  99. export default JsonMetadataTable;