createOsmBuildingsAsync.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Color from "../Core/Color.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import Cesium3DTileset from "./Cesium3DTileset.js";
  5. import Cesium3DTileStyle from "./Cesium3DTileStyle.js";
  6. /**
  7. * Creates a {@link Cesium3DTileset} instance for the
  8. * {@link https://cesium.com/content/cesium-osm-buildings/|Cesium OSM Buildings}
  9. * tileset.
  10. *
  11. * @function
  12. *
  13. * @param {object} [options] Construction options. Any options allowed by the {@link Cesium3DTileset} constructor
  14. * may be specified here. In addition to those, the following properties are supported:
  15. * @param {Color} [options.defaultColor=Color.WHITE] The default color to use for buildings
  16. * that do not have a color. This parameter is ignored if <code>options.style</code> is specified.
  17. * @param {Cesium3DTileStyle} [options.style] The style to use with the tileset. If not
  18. * specified, a default style is used which gives each building or building part a
  19. * color inferred from its OpenStreetMap <code>tags</code>. If no color can be inferred,
  20. * <code>options.defaultColor</code> is used.
  21. * @param {boolean} [options.enableShowOutline=true] If true, enable rendering outlines. This can be set to false to avoid the additional processing of geometry at load time.
  22. * @param {boolean} [options.showOutline=true] Whether to show outlines around buildings. When true,
  23. * outlines are displayed. When false, outlines are not displayed.
  24. * @returns {Promise<Cesium3DTileset>}
  25. *
  26. * @see Ion
  27. *
  28. * @example
  29. * // Create Cesium OSM Buildings with default styling
  30. * const viewer = new Cesium.Viewer("cesiumContainer");
  31. * try {
  32. * const tileset = await Cesium.createOsmBuildingsAsync();
  33. * viewer.scene.primitives.add(tileset));
  34. * } catch (error) {
  35. * console.log(`Error creating tileset: ${error}`);
  36. * }
  37. *
  38. * @example
  39. * // Create Cesium OSM Buildings with a custom style highlighting
  40. * // schools and hospitals.
  41. * const viewer = new Cesium.Viewer("cesiumContainer");
  42. * try {
  43. * const tileset = await Cesium.createOsmBuildingsAsync({
  44. * style: new Cesium.Cesium3DTileStyle({
  45. * color: {
  46. * conditions: [
  47. * ["${feature['building']} === 'hospital'", "color('#0000FF')"],
  48. * ["${feature['building']} === 'school'", "color('#00FF00')"],
  49. * [true, "color('#ffffff')"]
  50. * ]
  51. * }
  52. * })
  53. * });
  54. * viewer.scene.primitives.add(tileset));
  55. * } catch (error) {
  56. * console.log(`Error creating tileset: ${error}`);
  57. * }
  58. */
  59. async function createOsmBuildingsAsync(options) {
  60. const tileset = await Cesium3DTileset.fromIonAssetId(96188, options);
  61. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  62. let style = options.style;
  63. if (!defined(style)) {
  64. const color = defaultValue(
  65. options.defaultColor,
  66. Color.WHITE
  67. ).toCssColorString();
  68. style = new Cesium3DTileStyle({
  69. color: `Boolean(\${feature['cesium#color']}) ? color(\${feature['cesium#color']}) : ${color}`,
  70. });
  71. }
  72. tileset.style = style;
  73. return tileset;
  74. }
  75. export default createOsmBuildingsAsync;