findContentMetadata.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import ContentMetadata from "./ContentMetadata.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import hasExtension from "./hasExtension.js";
  5. import oneTimeWarning from "../Core/oneTimeWarning.js";
  6. /**
  7. * Check if a content has metadata, either defined in its metadata field (3D Tiles 1.1) or in
  8. * the <code>3DTILES_metadata</code> extension. If defined, get the content metadata
  9. * with the corresponding class.
  10. *
  11. * @function
  12. *
  13. * @param {Cesium3DTileset} tileset The tileset to query for content metadata
  14. * @param {object} contentHeader the JSON header for a {@link Cesium3DTileContent}
  15. * @return {ContentMetadata} the content metadata, or <code>undefined</code> if not found
  16. * @private
  17. * @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.
  18. */
  19. function findContentMetadata(tileset, contentHeader) {
  20. const metadataJson = hasExtension(contentHeader, "3DTILES_metadata")
  21. ? contentHeader.extensions["3DTILES_metadata"]
  22. : contentHeader.metadata;
  23. if (!defined(metadataJson)) {
  24. return undefined;
  25. }
  26. if (!defined(tileset.schema)) {
  27. findContentMetadata._oneTimeWarning(
  28. "findContentMetadata-missing-root-schema",
  29. "Could not find a metadata schema for content metadata. For tilesets that contain external tilesets, make sure the schema is added to the root tileset.json."
  30. );
  31. return undefined;
  32. }
  33. const classes = defaultValue(
  34. tileset.schema.classes,
  35. defaultValue.EMPTY_OBJECT
  36. );
  37. if (defined(metadataJson.class)) {
  38. const contentClass = classes[metadataJson.class];
  39. return new ContentMetadata({
  40. content: metadataJson,
  41. class: contentClass,
  42. });
  43. }
  44. return undefined;
  45. }
  46. // Exposed for testing
  47. findContentMetadata._oneTimeWarning = oneTimeWarning;
  48. export default findContentMetadata;