MetadataClass.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defined from "../Core/defined.js";
  5. import MetadataClassProperty from "./MetadataClassProperty.js";
  6. /**
  7. * A metadata class.
  8. *
  9. * <p>
  10. * See the {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata|3D Metadata Specification} for 3D Tiles
  11. * </p>
  12. *
  13. * @param {object} options Object with the following properties:
  14. * @param {string} options.id The ID of the class.
  15. * @param {string} [options.name] The name of the class.
  16. * @param {string} [options.description] The description of the class.
  17. * @param {Object<string, MetadataClassProperty>} [options.properties] The class properties, where each key is the property ID.
  18. * @param {*} [options.extras] Extra user-defined properties.
  19. * @param {object} [options.extensions] An object containing extensions.
  20. *
  21. * @alias MetadataClass
  22. * @constructor
  23. * @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.
  24. */
  25. function MetadataClass(options) {
  26. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  27. const id = options.id;
  28. //>>includeStart('debug', pragmas.debug);
  29. Check.typeOf.string("options.id", id);
  30. //>>includeEnd('debug');
  31. const properties = defaultValue(options.properties, {});
  32. const propertiesBySemantic = {};
  33. for (const propertyId in properties) {
  34. if (properties.hasOwnProperty(propertyId)) {
  35. const property = properties[propertyId];
  36. if (defined(property.semantic)) {
  37. propertiesBySemantic[property.semantic] = property;
  38. }
  39. }
  40. }
  41. this._id = id;
  42. this._name = options.name;
  43. this._description = options.description;
  44. this._properties = properties;
  45. this._propertiesBySemantic = propertiesBySemantic;
  46. this._extras = clone(options.extras, true);
  47. this._extensions = clone(options.extensions, true);
  48. }
  49. /**
  50. * Creates a {@link MetadataClass} from either 3D Tiles 1.1, 3DTILES_metadata, EXT_structural_metadata, or EXT_feature_metadata.
  51. *
  52. * @param {object} options Object with the following properties:
  53. * @param {string} options.id The ID of the class.
  54. * @param {object} options.class The class JSON object.
  55. * @param {Object<string, MetadataEnum>} [options.enums] A dictionary of enums.
  56. *
  57. * @returns {MetadataClass} The newly created metadata class.
  58. *
  59. * @private
  60. * @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.
  61. */
  62. MetadataClass.fromJson = function (options) {
  63. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  64. const id = options.id;
  65. const classDefinition = options.class;
  66. //>>includeStart('debug', pragmas.debug);
  67. Check.typeOf.string("options.id", id);
  68. Check.typeOf.object("options.class", classDefinition);
  69. //>>includeEnd('debug');
  70. const properties = {};
  71. for (const propertyId in classDefinition.properties) {
  72. if (classDefinition.properties.hasOwnProperty(propertyId)) {
  73. const property = MetadataClassProperty.fromJson({
  74. id: propertyId,
  75. property: classDefinition.properties[propertyId],
  76. enums: options.enums,
  77. });
  78. properties[propertyId] = property;
  79. }
  80. }
  81. return new MetadataClass({
  82. id: id,
  83. name: classDefinition.name,
  84. description: classDefinition.description,
  85. properties: properties,
  86. extras: classDefinition.extras,
  87. extensions: classDefinition.extensions,
  88. });
  89. };
  90. Object.defineProperties(MetadataClass.prototype, {
  91. /**
  92. * The class properties.
  93. *
  94. * @memberof MetadataClass.prototype
  95. * @type {Object<string, MetadataClassProperty>}
  96. * @readonly
  97. */
  98. properties: {
  99. get: function () {
  100. return this._properties;
  101. },
  102. },
  103. /**
  104. * A dictionary mapping semantics to class properties.
  105. *
  106. * @memberof MetadataClass.prototype
  107. * @type {Object<string, MetadataClassProperty>}
  108. * @readonly
  109. *
  110. * @private
  111. */
  112. propertiesBySemantic: {
  113. get: function () {
  114. return this._propertiesBySemantic;
  115. },
  116. },
  117. /**
  118. * The ID of the class.
  119. *
  120. * @memberof MetadataClass.prototype
  121. * @type {string}
  122. * @readonly
  123. */
  124. id: {
  125. get: function () {
  126. return this._id;
  127. },
  128. },
  129. /**
  130. * The name of the class.
  131. *
  132. * @memberof MetadataClass.prototype
  133. * @type {string}
  134. * @readonly
  135. */
  136. name: {
  137. get: function () {
  138. return this._name;
  139. },
  140. },
  141. /**
  142. * The description of the class.
  143. *
  144. * @memberof MetadataClass.prototype
  145. * @type {string}
  146. * @readonly
  147. */
  148. description: {
  149. get: function () {
  150. return this._description;
  151. },
  152. },
  153. /**
  154. * Extra user-defined properties.
  155. *
  156. * @memberof MetadataClass.prototype
  157. * @type {*}
  158. * @readonly
  159. */
  160. extras: {
  161. get: function () {
  162. return this._extras;
  163. },
  164. },
  165. /**
  166. * An object containing extensions.
  167. *
  168. * @memberof MetadataClass.prototype
  169. * @type {object}
  170. * @readonly
  171. */
  172. extensions: {
  173. get: function () {
  174. return this._extensions;
  175. },
  176. },
  177. });
  178. /**
  179. * The class name given to the metadata class when a batch
  180. * table is loaded from 3D Tiles 1.0 formats.
  181. *
  182. * @private
  183. */
  184. MetadataClass.BATCH_TABLE_CLASS_NAME = "_batchTable";
  185. export default MetadataClass;