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