Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a polyline collection in Cesium?

I must be doing something simply wrong because I am not sure after reading the documents how to show a polyline collection in Cesium. I am not explicitly seeing any method or tutorial in the documentation here about displaying the polyline collection. Nor are there any tutorials in the sand box that I can find that seem more on point that this one, which only displays singular polylines with

viewer.entites.add(Polyline)

I have tried using the example code for PolylineCollection's add (and suggestion for iteration then toggling) in this sandbox, but nothing is displayed, and no errors are shown:

// Create a polyline collection with two polylines
var polylines = new Cesium.PolylineCollection();
polylines.add({
  positions : Cesium.Cartesian3.fromDegreesArray([
    -75.10, 39.57,
    -77.02, 38.53,
    -80.50, 35.14,
    -80.12, 25.46]),
  width : 2
});

polylines.add({
  positions : Cesium.Cartesian3.fromDegreesArray([
    -73.10, 37.57,
    -75.02, 36.53,
    -78.50, 33.14,
    -78.12, 23.46]),
  width : 4
});
// Toggle the show property of every polyline in the collection
var len = polylines.length;
for (var i = 0; i < len; ++i) {
  var p = polylines.get(i);
  p.show = true;
}

I'm not sure what other means the documentation would point me towards to render these. Any help is appreciated.

like image 851
ThePartyTurtle Avatar asked Jan 10 '17 22:01

ThePartyTurtle


People also ask

What happens when a polyline is removed from the collection?

Removing a polyline shifts all polylines after it to the left, changing their indices. This function is commonly used with PolylineCollection#length to iterate over all the polylines in the collection. If polylines were removed from the collection and PolylineCollection#update was not called, an implicit O (n) operation is performed.

What are the optional components of a polyline?

optional The width of the polyline in pixels. optional Whether a line segment will be added between the last and first line positions to make this line a loop. Material. ColorType optional The material. optional The positions. optional The user-defined object to be returned when this polyline is picked.

How do I organize my collection of polylines?

Organize collections so that polylines with the same update frequency are in the same collection, i.e., polylines that do not change should be in one collection; polylines that change every frame should be in another collection; and so on. optional The 4x4 transformation matrix that transforms each polyline from model to world coordinates.

What is a primitive layer in cesium?

You're mixing Cesium API layers here. Cesium has 2 different layers of public API, an "Entity" layer and a "Primitive" layer. The Primitive layer is for graphics primitives: A whole collection of polylines is effectively a single graphics primitive (internally, a single "draw call"), a collection of billboards is another single primitive, etc.


Video Answer


1 Answers

You're mixing Cesium API layers here. Cesium has 2 different layers of public API, an "Entity" layer and a "Primitive" layer. The Primitive layer is for graphics primitives: A whole collection of polylines is effectively a single graphics primitive (internally, a single "draw call"), a collection of billboards is another single primitive, etc. An "Entity" is for a more high-level concept of an object or vehicle, for example a single truck entity may have a billboard, a label, and a polyline that are all showing where the truck is and where it's been. A collection of separate entities will share one collection of billboards, plus one collection of polylines, etc., for graphics performance reasons.

Typically it's recommended to use the Entity layer where possible or practical, as that lets you think in terms of real-world objects instead of collections of graphics primitives. But sometimes, you have such a large collection of static primitives that it's more performant to simply submit that collection directly.

In the demo you linked, the code there is creating a number of entities, and attaching a polyline to each one. But, in the code you've posted, you're manually creating PolylineCollection, and trying to display it. So to fix your code, remove this line:

viewer.entites.add(Polyline)

and add this line:

viewer.scene.primitives.add(polylines);

Note that polylines is declared in your code, but Polyline is just a class. Also note that we're adding the polylineCollection as a scene.primitive, not an entity.

Depending on what you're really using this for, it may or may not be better to scrap your code here and re-copy the entity demo code you linked to, and use that form instead.

like image 196
emackey Avatar answered Sep 29 '22 03:09

emackey