Adding multiple datasources?

I have multiple CZML datasource streams, one static objects and one animated.

I can’t seem to get it to load the static stream in it’s own dataSource. If I load it into the same stream as the animated stream(animStream) it renders the static objects but because it’s written static it ends up stacking copies of the static objects as it animates the animated objects.

Below is my client side code. The static stream data is arriving but not rendering in Cesium. I’m getting no errors either.

//animated assets arrive from server send whenever they occur
var animStream = new Cesium.CzmlDataSource();

socket.on(‘threatTruth’, function(data){
animStream.process(JSON.parse(data));
viewer.dataSources.add(animStream);
});

//static assets load on HTML button press
`var staticStream = new Cesium.CzmlDataSource();

`function loadScenario(){
socket.emit(‘loadF’, function(callback){
for (var i=0; i < callback.length; i++){
staticStream.process(JSON.parse(callback[i]));
}
viewer.dataSources.add(staticStream);
});
}

``

The animStream works just fine as the events come from the server. When I hit the button to load the static objects the CZML packets arrive in an array which gets processed but doesn’t get loaded into the scene. I have tried all sorts of ways to get it to load but no luck.

Any ideas?

Are there any errors in the console? Can you try changing the load line to the below, which should add an error pop-up to your code.

staticStream.process(JSON.parse(callback[i])).otherwise(function(e) { alert(e.message ? e.message : e); });

It’s hard to say what the problem could be without any additional context. I assume both of these callbacks are only ever called once? Otherwise you are adding the same data source to you scene multiple times, which is bad.

That helped! Thank you!

Turns out I forgot to include the “document” packet as the first packet for the second datasource. I have no idea why it wasn’t appearing as an error in the console but printing the error like that caught it.

I moved where I was adding my datasources to the viewer outside of the functions as well. You were correct that the animated stream was adding itself multiple times which explains it’s slow behavior.

Everything is running smooth now, thanks again!

The reason you weren’t seeing the error is that data source loading is always asynchronous. When an exception is thrown from a async callback; throwing an exception does not propagate to JavaScript code (because of the nature of how browsers and asynchronous work) The solution is to use the promise that is returned to handle any errors. In 1.7 all data sources return promises. There is also an errorEvent that can be subscribed to on a datasource that will be raised if an error is encountered.

Between this and another question asked recently, I think I might change 1.8 so that all errors are additionally printed to the console, making this easier to debug.