Is it possible to modify polygon vertices?

If I create an ellipse (cylinder) like this:

var ellipseEntity = entities.add({
       position: Cesium.Cartesian3.fromDegrees(coordArray[0], coordArray[1]),
       ellipse: {
                 semiMinorAxis: radius,
                 semiMajorAxis: radius,
                 height: 0,
                 extrudedHeight: 10,
                 material: Cesium.Color.BLACK.withAlpha(1.0)
         }
     });

It works fine, and I can see a cylinder on the globe.

If I want to change the position of the cylinder after it's created, I can do this:

ellipseEntity.position = Cesium.Cartesian3.fromDegrees(newLon, newLat);

And the cylinder is repositioned as expected.

However, I'd like to do the same kind of thing with polygon vertices. I've tried this:

var flatPointArray = [ <new vertices> ];

entities.suspendEvents();
var positions = polygon._polygon._hierarchy;
positions.setValue(Cesium.Cartesian3.fromDegreesArray(flatPointArray));
polygon._show = true;
entities.resumeEvents();

polygon.polygon.material = Cesium.Color.YELLOW.withAlpha(1.0);

The original polygon appears just fine, but when I try to set the new vertices, it disappears from the scene.

I know that 'polygon' is valid and refers to the right entity because if I try to change *just* the material (and comment out the vertex update code), it reflects the color change.

But I can't seem to change the vertices.

Any ideas?

Thanks.

I see two probable issues. First, don’t manipulate fields that have a ‘_’ prefix - those are intended to be private, and shouldn’t be used directly. Use the public documented members instead.

Second, I’m not sure where there’s any kind of “positions.setValue()” function. The relevant field structure appears to be Entity.polygon -> PolygonGraphics.hierarchy -> PolygonHierarchy.positions. So, presumably what you really need to do is:

myEntity.polygon.hierarchy.positions = Cesium.Cartesian3.fromDegreesArray(flatPointArray);

``

And that may actually not be correct, because PolygonGraphics.hierarchy is a Property - you might need to use one of the Property classes to wrap your data? I’m not as familiar with manipulating Entity objects directly.

Still, hopefully that gets you on the right track, and someone else can chime in.

Mark,

Thanks for your reply. I tried

myEntity.polygon.hierarchy.positions = Cesium.Cartesian3.fromDegreesArray(flatPointArray);

but unfortunately, the polygon vertices (as rendered in the scene) didn't change. I stepped through the unminified Cesium code from the assignment, and it appears to be changing the polygon vertex values internally, but for some reason, nothing changes in the scene.

There must be a step I'm missing to propagate the vertex changes to the scene. I wonder if there's a "refresh," update," or "redraw" call I'm missing...

Although, I'm a little bewildered because this works:

myEntity.polygon.material = Cesium.Color.YELLOW.withAlpha(1.0);

and I see the color change immediately.

Also, this works when the entity is an ellipse:

myEllipse.position = <a cartesian coordinate>;

The position of the ellipse is updated immediately. It looks like there's something fundamentally different about polygons.

Thanks,
Mike

Did a quick search of the Cesium source for uses of “hierarchy”, and my “this might not work as-is” comment was confirmed. You need to wrap your data in one of Cesium’s Property types. Here’s an example copied from the Cesium GeoJsonDataSource class:

polygon.hierarchy = new ConstantProperty(new PolygonHierarchy(coordinatesArrayToCartesianArray(positions, crsFunction), holes));

``

Not sure if your own code needs to do that “new PolygonHierarchy” part, but yeah, PolygonGraphics defines “hierarchy” as a Property type, so you need to use a Property wrapper for your data.

1 Like

Mark,

That did the trick!

This works:

polygon.hierarchy = new Cesium.ConstantProperty(new Cesium.PolygonHierarchy(
    Cesium.Cartesian3.fromDegreesArray(flatPointArray)));

Thanks so much for your quick reply!

Mike

1 Like