This sample demonstrates how to create ViewshedAnalysis and how to place Viewsheds in a 3D Scene.
For a higher-level overview of the analysis lifecycle, see Introduction to analyses. For a step-by-step walkthrough of the same workflow, see the Create and manage a viewshed analysis tutorial.
// Initialize viewshed analysis and add it to the view. const viewshedAnalysis = new ViewshedAnalysis(); viewElement.analyses.add(viewshedAnalysis);
// Create the viewshed shape programmatically and add it to the analysis. const programmaticViewshed = new Viewshed({ observer: { spatialReference: SpatialReference.WebMercator, x: -9754426, y: 5143111, z: 330, }, farDistance: 900, // In meters tilt: 84, // Tilt of 0 looks down, tilt of 90 looks parallel to the ground, tilt of 180 looks up to the sky heading: 63, // Counted clockwise from North horizontalFieldOfView: 85, verticalFieldOfView: 60, }); viewshedAnalysis.viewsheds.add(programmaticViewshed);Place additional viewsheds using a custom UI element by calling the place() method on the analysis view.
// Access the viewshed's analysis view. const analysisView = await viewElement.whenAnalysisView(viewshedAnalysis); // Controller for aborting the viewshed place operation. let abortController = null;
placeButton.addEventListener("click", () => startPlacing()); async function startPlacing() {
// Create a new abort controller for the new operation. abortController = new AbortController(); const { signal } = abortController;
// Pass the controller as an argument to the interactive place method. try { await analysisView.place({ signal: abortController.signal }); } catch (error) { // This is used to handle the case when the viewshed placement is aborted. if (!promiseUtils.isAbortError(error)) { throw error; } } finally { if (abortController && abortController.signal === signal) { abortController = null; } updateUI(); } }Cancel the ongoing placement operation with the abortController.
cancelButton.addEventListener("click", () => stopPlacing()); function stopPlacing() { abortController?.abort(); abortController = null; updateUI(); }One can also limit the viewshed’s field of view. In the sample, this is done using a switch element in the UI.
if (viewshed.horizontalFieldOfView > horizontalLimit) { viewshed.horizontalFieldOfView = horizontalLimit; }
if (viewshed.verticalFieldOfView > verticalLimit) { viewshed.verticalFieldOfView = verticalLimit; }Additionally, the sample demonstrates how to set up a second camera that shows the view from a selected viewshed’s observer point.
function getCameraFromViewshed(viewshed) { return new Camera({ position: viewshed.observer, heading: viewshed.heading, tilt: viewshed.tilt, // Calculate camera's diagonal field of view angle. fov: getDiagonal(viewshed.verticalFieldOfView, viewshed.horizontalFieldOfView), }); } function getDiagonal(a, b) { return Math.sqrt(a ** 2 + b ** 2); }Snapping and tooltips
Snapping is enabled by default in 3D and helps position the viewshed observer on vertices and edges of existing features. Hold Ctrl to temporarily toggle snapping on or off. To disable snapping through the Scene component’s view options (beta), set:
viewElement.options.snapping.enabled = false;This sample enables tooltips through the same view options to enter precise values while placing or editing a viewshed:
viewElement.options.tooltips.enabled = true;Press Tab to activate the tooltip’s input mode. Numeric inputs accept simple math expressions with two values and one operator: +, -, *, or /. For example, enter 100 + 2 in an elevation input to use 102 in the input’s current unit. Press Enter to apply the value and close input mode.
Elevation modes
By default, the observer is placed at an absolute elevation relative to sea level. While placing or editing a viewshed’s observer, press Tab and use the elevation mode icon next to the tooltip’s Elevation field to select its elevation mode. This changes the elevation reference for the observer, not the viewshed’s viewing direction or field of view. The default elevation settings can be customized through the elevationInfo option when calling ViewshedAnalysisView3D.place().
The mode determines what the elevation value is measured relative to, not its units:
- Absolute: Height above sea level, independent of terrain or buildings.
- On the ground: Positions the observer on the terrain surface; its elevation value is ignored.
- Relative to ground: Height above the terrain at the observer’s location.
- Relative to scene: Height above a scene surface, such as a building roof, with the ground used where no supported scene feature is present.
An elevation of 2 meters in Relative to ground means 2 meters above the local terrain, rather than above sea level. The same numeric elevation can therefore produce different observer heights in different modes, which can change the visible and occluded areas of the viewshed.