TimeIntervalCollectionProperty.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import Event from "../Core/Event.js";
  4. import TimeIntervalCollection from "../Core/TimeIntervalCollection.js";
  5. import Property from "./Property.js";
  6. /**
  7. * A {@link Property} which is defined by a {@link TimeIntervalCollection}, where the
  8. * data property of each {@link TimeInterval} represents the value at time.
  9. *
  10. * @alias TimeIntervalCollectionProperty
  11. * @constructor
  12. *
  13. * @example
  14. * //Create a Cartesian2 interval property which contains data on August 1st, 2012
  15. * //and uses a different value every 6 hours.
  16. * const composite = new Cesium.TimeIntervalCollectionProperty();
  17. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  18. * iso8601 : '2012-08-01T00:00:00.00Z/2012-08-01T06:00:00.00Z',
  19. * isStartIncluded : true,
  20. * isStopIncluded : false,
  21. * data : new Cesium.Cartesian2(2.0, 3.4)
  22. * }));
  23. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  24. * iso8601 : '2012-08-01T06:00:00.00Z/2012-08-01T12:00:00.00Z',
  25. * isStartIncluded : true,
  26. * isStopIncluded : false,
  27. * data : new Cesium.Cartesian2(12.0, 2.7)
  28. * }));
  29. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  30. * iso8601 : '2012-08-01T12:00:00.00Z/2012-08-01T18:00:00.00Z',
  31. * isStartIncluded : true,
  32. * isStopIncluded : false,
  33. * data : new Cesium.Cartesian2(5.0, 12.4)
  34. * }));
  35. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  36. * iso8601 : '2012-08-01T18:00:00.00Z/2012-08-02T00:00:00.00Z',
  37. * isStartIncluded : true,
  38. * isStopIncluded : true,
  39. * data : new Cesium.Cartesian2(85.0, 4.1)
  40. * }));
  41. */
  42. function TimeIntervalCollectionProperty() {
  43. this._definitionChanged = new Event();
  44. this._intervals = new TimeIntervalCollection();
  45. this._intervals.changedEvent.addEventListener(
  46. TimeIntervalCollectionProperty.prototype._intervalsChanged,
  47. this
  48. );
  49. }
  50. Object.defineProperties(TimeIntervalCollectionProperty.prototype, {
  51. /**
  52. * Gets a value indicating if this property is constant. A property is considered
  53. * constant if getValue always returns the same result for the current definition.
  54. * @memberof TimeIntervalCollectionProperty.prototype
  55. *
  56. * @type {boolean}
  57. * @readonly
  58. */
  59. isConstant: {
  60. get: function () {
  61. return this._intervals.isEmpty;
  62. },
  63. },
  64. /**
  65. * Gets the event that is raised whenever the definition of this property changes.
  66. * The definition is changed whenever setValue is called with data different
  67. * than the current value.
  68. * @memberof TimeIntervalCollectionProperty.prototype
  69. *
  70. * @type {Event}
  71. * @readonly
  72. */
  73. definitionChanged: {
  74. get: function () {
  75. return this._definitionChanged;
  76. },
  77. },
  78. /**
  79. * Gets the interval collection.
  80. * @memberof TimeIntervalCollectionProperty.prototype
  81. *
  82. * @type {TimeIntervalCollection}
  83. * @readonly
  84. */
  85. intervals: {
  86. get: function () {
  87. return this._intervals;
  88. },
  89. },
  90. });
  91. /**
  92. * Gets the value of the property at the provided time.
  93. *
  94. * @param {JulianDate} time The time for which to retrieve the value.
  95. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  96. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  97. */
  98. TimeIntervalCollectionProperty.prototype.getValue = function (time, result) {
  99. //>>includeStart('debug', pragmas.debug);
  100. if (!defined(time)) {
  101. throw new DeveloperError("time is required");
  102. }
  103. //>>includeEnd('debug');
  104. const value = this._intervals.findDataForIntervalContainingDate(time);
  105. if (defined(value) && typeof value.clone === "function") {
  106. return value.clone(result);
  107. }
  108. return value;
  109. };
  110. /**
  111. * Compares this property to the provided property and returns
  112. * <code>true</code> if they are equal, <code>false</code> otherwise.
  113. *
  114. * @param {Property} [other] The other property.
  115. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  116. */
  117. TimeIntervalCollectionProperty.prototype.equals = function (other) {
  118. return (
  119. this === other || //
  120. (other instanceof TimeIntervalCollectionProperty && //
  121. this._intervals.equals(other._intervals, Property.equals))
  122. );
  123. };
  124. /**
  125. * @private
  126. */
  127. TimeIntervalCollectionProperty.prototype._intervalsChanged = function () {
  128. this._definitionChanged.raiseEvent(this);
  129. };
  130. export default TimeIntervalCollectionProperty;