Polylines between entities that are clamped to ground

Hi there,

The new 1.23 release of Cesium is fantastic! We've been looking forward to clamp to ground for entities for a long time!

I do have a question though, is there an easy way to draw a line between two entities that are clamped to ground? How do you get the calculated height?


Unfortunately we don’t currently have a way to clamp polylines to the terrain. It’s something we are looking into and plan on adding soon, but we have to do a bit more research first to figure out the best way to implement it.

In the meantime, you can query the terrain to get the heights and draw your polyline. Here is an example:

var viewer = new Cesium.Viewer(‘cesiumContainer’);
viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
url : ‘https://assets.agi.com/stk-terrain/world

var ellipsoid = viewer.scene.globe.ellipsoid;
var positions = Cesium.Cartesian3.fromDegreesArray([
-115, 35,
-114, 36

var flatPositions = Cesium.PolylinePipeline.generateArc({
positions: positions,
granularity: 0.000001

var cartographicArray = ;
for (var i = 0; i < flatPositions.length; i+=3) {
var cartesian = Cesium.Cartesian3.unpack(flatPositions, i);

Cesium.sampleTerrain(viewer.terrainProvider, 15, cartographicArray)
.then(function(raisedPositionsCartograhpic) {
var raisedPositions = ellipsoid.cartographicArrayToCartesianArray(raisedPositionsCartograhpic);
polyline : {
positions : raisedPositions,
width : 5,
material : Cesium.Color.RED


This will be slow if you have a lot of polylines, or polylines that are really long. Instead of adjusting the height at runtime every time, you can print the raised positions out and use that to replace your input positions.



Thanks for your response Hannah.

Unfortunately our lines are between entities that are temporally relative (use SampledPositionProperty as their position). We used a callback property to set either end of the line to the position of a different entity so they join them as they move. This works great until clamp to ground is enabled, and using sampleTerrain every frame for each line isn't feasible.

We don't need the lines to be on the terrain themselves, we just need to be able to set the position on both ends to take into account the entity's clamped to ground height. I imagine this may not be a variable on the entity though, and is instead stored in GPU memory?

Quick question. Is there any kind of variable available on the entity or entity's BillboardGraphics object that has the calculated on-terrain height available? The position itself doesn't appear to take into account the terrain.

Or is the only option what you said in regards to running Cesium.sampleTerrain every frame at both ends for each polyline?

Cesium newbie here. I see this thread is almost two years old now; I was wondering if there had been any improvements to clamping polylines? I'm trying to find the right solution for showing a hiking trail on 3D terrain. The GPS data I have does have has elevation data, but sometimes they are a bit off from the terrain provider and as such they don't appear clamped to the ground when you zoom in (they might sorta float) or use the camera to look at them at an angle. On a related polyline note, when a hiking trail wraps around a mountain and that mountain is between the camera and trail, I'd expect the terrain to block my view of the trail. Unfortunately, that doesn't seem to happen; is there a property perhaps to control that?

Hi there,

We’re working on Polylines on terrain right now. Expect to see it in the next month or two.



Thank you for the response Gabby. What I am ultimately hoping to do is to create a progressive web app (PWA) for hikers on a long back-country trail. On a users IPhone or Android device (phone or tablet) I’m hoping that in addition to taking 2D maps offline (which I’m able to do today) that I can take a limited amount of 3D imagery and terrain offline as well. Obviously storage space and the client-side rendering of 3D terrain and the polyline representing the trail is the real key here.
To limit storage requirements, what I would hope to provide is a mechanism for users to download the necessary imagery and terrain to their device. The resolution of the imagery and terrain would improve as you zoomed in on the trail. Can you tell me if what I’m dreaming about will be possible with Cesium in the future?
Perhaps I should add that this app idea would be non-commercial in nature and for a non-profit / 501C - I believe I read that there are some limitations with taking imagery offline for commercial purposes.


Hi River,

For the imagery, it should be straightforward. Just make sure you have the rights to that particular imagery and host the files locally from the same server as you are running your local Cesium app.

For terrain, you need a special server configuration in order to handle terrain requests. We do this when hosting terrain out of Cesium ion, but that is not a local solution. You can contact tim@cesium.com to inquire about an on-premise solution, or look into the STK Terrain Server which is a product from AGI.