createOsmBuildings.js 2.6 KB

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