Search code examples
javascripttypescriptcesiumjs

How can I define a polygon entity using availability intervals in Cesium?


I am defining a realy huge polygon that changes its position and shape using inertial reference frame.

First, I tryed to define a set of CZML polygons, each one with an availability and its coordinates like the "california" object in this sandcastle example, but this exceeds the maximum memory limit and blocks the browser and eventually the computer. Then, I had it working using only one polygon in CZML providing a list with an interval and the polygon shape coordinates, like the "dynamicPolygon" in the same sandcastle example.

Now I am trying to change the CZML implementation to use entities but the entity documentation allows to use a PolygonGraphics that allows to use a Property or a PolygonHierarchy to configure the shape, and I can not figure out how to do the same "dynamicPolygon" using entities.


Solution

  • You can use a CallbackProperty (that is a type of Property) in the hierarchy constructor option. There should not be issues with memory due to there is only one object and the positions are being calculated dynamically. You also can use a collection like TimeIntervalCollectionProperty and then ask for the value for an specific time.

    Try to add the next code fragment in the Hello World Cesium sandcastle:

    viewer.entities.add({
        id: "dynamicPolygon",
        name: "dynamicPolygon",
        polygon: new Cesium.PolygonGraphics({
          hierarchy: new Cesium.CallbackProperty(function (time, result) {
            var n = time.secondsOfDay%10;
            result = new Cesium.PolygonHierarchy([
                Cesium.Cartesian3.fromDegrees(2*n, n),
                Cesium.Cartesian3.fromDegrees(-2*n, n),
                Cesium.Cartesian3.fromDegrees(-2*n, -n),
                Cesium.Cartesian3.fromDegrees(2*n, -n)
            ]);
            return result;
          }, false),
          material: Cesium.Color.WHITE
      })
    });