GeoJson and sampleTerrain issues + general terrain oddities

I'm currently having a few issues with loading GeoJson point data and then updating those points with the current TerrainProvider using sampleTerrain. I'm hoping I'm just not using things optimally and there is another suggested method for updating the GeoJson items.

basic code for what i'm doing (some steps trimmed for brevity):

var dataSource = new Cesium.GeoJsonDataSource();
dataSource.loadUrl(geoJson).then(function() {
  var entities = dataSource.entities.entities;
  updateCollectionHeightForTerrain(entities);
}

function updateCollectionHeightForTerrain(collection){
  var positions = ;
  for(index = 0; index < collection.length; ++index){
    var item = collection[index];
    var position = item.position.getValue(Cesium.JulianDate.fromDate(new Date()));
    position = _viewer.scene.globe.ellipsoid.cartesianToCartographic(position);
    positions.push(position);
  }
  var promise = Cesium.sampleTerrain(terrainProvider, 11, positions);
  Cesium.when(promise, function(updatedPositions) {
    for(var i = 0; i < updatedPositions.length; i++){
  var newPos = _viewer.scene.globe.ellipsoid.cartographicToCartesian(updatedPositions[i]);
  collection[i].position = new Cesium.ConstantPositionProperty(newPos);
    }
  });

First problem: If my array of positions is larger than 1 or 2 points, the terrain sampling appears to time out and when function never gets hit. Is there a way to adjust the sampleTerrain timeout? Or is there some other workaround here?

Second problem: If I instead do this point by point, since it is an asynchronous call, when it returns I have no idea what object/item the updatedPosition is actually for. Any suggestions?

Third problem: even after adjusting the height, I still see some z-fighting and the symbol being partially covered by the terrain at far zoom levels despite the symbol being above the terrain and depthTestAgainstTerrain being set to false. Any idea what's going on here? (i also see this problem with models and polylines that are well above the terrain.. like airplane cruise altitudes)

Problem 1: I suspect you’re not seeing a timeout, but rather, your callback is crashing because the height of the sampled position can come back as undefined when the terrain provider fails to load a particular tile, which fails when trying to convert back to cartesian coordinates.

When working with promises, I highly recommend always installing an error callback so you can see if you are getting errors. The easiest way to do this with when.js (the bundled promise library in Cesium) is to add a call to otherwise after your success callback. That will catch both promise rejections and errors thrown by your success callback itself.

For example:

Cesium.when(promise, function(updatedPositions) {

// etc.

}).otherwise(function(err) {

console.error(err);

});

Problem 2:

If you want to call sampleTerrain individually for each position, you can use a JavaScript closure to keep a reference to the entity that corresponds with each position.

For example:

dataSource.load(geojson).then(function() {

var entities = dataSource.entities.entities;



var promises = entities.map(function(entity) { 

    var cartesian = entity.position.getValue(Cesium.JulianDate.now());

    var cartographic = globe.ellipsoid.cartesianToCartographic(cartesian);

    var promise = Cesium.sampleTerrain(scene.terrainProvider, 11, [cartographic]);

    return Cesium.when(promise, function(updatedCartographic) { 

        updatedCartographic = updatedCartographic[0];

        if (updatedCartographic.height === undefined) {

            return;

        }

        var updatedCartesian = globe.ellipsoid.cartographicToCartesian(updatedCartographic);

        entity.position = new Cesium.ConstantPositionProperty(updatedCartesian);           

    }).otherwise(function(err) {

        console.error(err);

    });

});

}).otherwise(function(err) {

console.error(err);

});

I created a full Sandcastle example with the rest of the code here: https://gist.github.com/shunter/4c21b8cb05e9646c6773

Problem 3:

I believe this is a bug in Cesium that is causing clipping of billboards at multifrustum boundaries. I opened https://github.com/AnalyticalGraphicsInc/cesium/issues/2139 to track the issue.