Spatial analysis: distance, profile and visibility
Quick answer
This tutorial builds measurement, terrain profile and line-of-sight tools from CesiumJS primitives rather than a ready-made toolbox. It separates EllipsoidGeodesic surface distance from Cartesian3.distance and explains when each is correct. It samples a line with sampleTerrainMostDetailed, interpolates sight height to detect blocking terrain, and draws results into a CustomDataSource. Readers can produce credible distance, profile and visibility outputs and clear temporary analysis layers.
Key points
- Use EllipsoidGeodesic.surfaceDistance for ground distance and Cartesian3.distance for sight lines.
- Await sampleTerrainMostDetailed on an array of Cartographic points before reading height.
- Compare interpolated sight height against sampled terrain, allowing a small epsilon.
Example
var start = Cesium.Cartographic.fromDegrees(116.30, 39.90);
var end = Cesium.Cartographic.fromDegrees(116.46, 39.92);
var geodesic = new Cesium.EllipsoidGeodesic(start, end);
console.log('地表距离', geodesic.surfaceDistance.toFixed(1), '米');
console.log('方位角', Cesium.Math.toDegrees(geodesic.startHeading).toFixed(1), '度');
var a = Cesium.Cartesian3.fromDegrees(116.30, 39.90, 40);
var b = Cesium.Cartesian3.fromDegrees(116.46, 39.92, 120);
console.log('直线距离', Cesium.Cartesian3.distance(a, b).toFixed(1), '米');function sampleLine(start, end, count) {
var out = [];
for (var i = 0; i <= count; i++) {
var t = i / count;
out.push(new Cesium.Cartographic(
start.longitude + (end.longitude - start.longitude) * t,
start.latitude + (end.latitude - start.latitude) * t
));
}
return out;
}
async function buildProfile(start, end, count) {
var samples = sampleLine(start, end, count || 120);
await Cesium.sampleTerrainMostDetailed(viewer.terrainProvider, samples);
return samples.map(function (c) {
return {
lng: Cesium.Math.toDegrees(c.longitude),
lat: Cesium.Math.toDegrees(c.latitude),
height: c.height
};
});
}
var profile = await buildProfile(
Cesium.Cartographic.fromDegrees(116.30, 39.90),
Cesium.Cartographic.fromDegrees(116.46, 39.92),
120
);
var maxHeight = Math.max.apply(null, profile.map(function (p) { return p.height; }));
Notes
- With EllipsoidTerrainProvider every sampled height is 0, producing a flat profile.
- Sampling is asynchronous and expensive, so show a loading state and never reuse stale results.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Spatial analysis: distance, profile and visibility. https://www.jingweizhi.com/en/cesium/tutorials/spatial-analysis.html (accessed 2026-11-03).