Skip to main content

Area Viewshed Analysis Example

This example demonstrates how to perform area viewshed analysis using the DMap3D SDK, showing visible and non-visible areas within the frustum range of an observation point.

Preview

The user clicks in the scene to set an observation point and analyzes the visibility of the area through a frustum, with support for dynamically adjusting horizontal/vertical field of view angles.

Complete Code

src/components/ViewshedAreaAnalysis.tsx
import { useEffect, useRef, useState } from 'react'
import * as Cesium from 'cesium'
import DMap3D from 'dmap3d'
import './ViewshedAreaAnalysis.css'

function ViewshedAreaAnalysis() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)
const [fovx, setFovx] = useState(90)
const [fovy, setFovy] = useState(60)

useEffect(() => {
if (!containerRef.current) return

Cesium.Ion.defaultAccessToken = 'your-cesium-ion-token'

const viewer = new Cesium.Viewer(containerRef.current, {
animation: false,
fullscreenButton: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
selectionIndicator: false,
timeline: false,
navigationHelpButton: false,
infoBox: false,
baseLayerPicker: false,
terrain: Cesium.Terrain.fromWorldTerrain(),
})
viewerRef.current = viewer

// Load 3DTiles
const loadTileset = async () => {
const tileset = await Cesium.Cesium3DTileset.fromUrl(
'/3DTiles/model/tileset.json'
)
viewer.scene.primitives.add(tileset)
viewer.zoomTo(tileset)
}
loadTileset()

// Create area viewshed analysis tool
const tool = new DMap3D.analysis.viewshedArea(viewer, {
fovx: 90,
fovy: 60,
visibleColor: '#00ff00',
invisibleColor: '#ff0000',
visibleColorAlpha: 0.5,
invisibleColorAlpha: 0.5,
heightOffset: 1.8,
showFrustumRefLines: true,
})
toolRef.current = tool

return () => {
tool.destroy()
viewer.destroy()
}
}, [])

const handleStart = () => {
toolRef.current?.startAnalysis()
setIsActive(true)
}

const handleClear = () => {
const tool = toolRef.current
if (tool) {
tool.destroy()
// Recreate
const viewer = viewerRef.current
if (viewer) {
const newTool = new DMap3D.analysis.viewshedArea(viewer, {
fovx, fovy,
visibleColor: '#00ff00',
invisibleColor: '#ff0000',
visibleColorAlpha: 0.5,
invisibleColorAlpha: 0.5,
heightOffset: 1.8,
showFrustumRefLines: true,
})
toolRef.current = newTool
}
}
setIsActive(false)
}

const handleFovxChange = (value: number) => {
setFovx(value)
if (toolRef.current) {
toolRef.current.fovx = value
}
}

const handleFovyChange = (value: number) => {
setFovy(value)
if (toolRef.current) {
toolRef.current.fovy = value
}
}

return (
<div className="viewshed-area-analysis">
<div ref={containerRef} className="cesium-container" />

<div className="control-panel">
<h3>Area Viewshed Analysis</h3>
<p>Click to set observation point, drag to adjust direction</p>

<div className="buttons">
<button onClick={handleStart} disabled={isActive}>
{isActive ? 'Analyzing...' : 'Start Analysis'}
</button>
<button onClick={handleClear}>Clear</button>
</div>

<div className="settings">
<h4>Parameter Settings</h4>
<div className="setting-item">
<label>Horizontal FOV: {fovx} deg</label>
<input type="range" min="1" max="130" value={fovx}
onChange={(e) => handleFovxChange(Number(e.target.value))} />
</div>
<div className="setting-item">
<label>Vertical FOV: {fovy} deg</label>
<input type="range" min="1" max="179" value={fovy}
onChange={(e) => handleFovyChange(Number(e.target.value))} />
</div>
</div>

<div className="legend">
<div className="legend-item">
<span className="color-box" style={{background: '#00ff00'}}></span>
<span>Visible Area</span>
</div>
<div className="legend-item">
<span className="color-box" style={{background: '#ff0000'}}></span>
<span>Non-visible Area</span>
</div>
</div>
</div>
</div>
)
}

export default ViewshedAreaAnalysis

Stylesheet

src/components/ViewshedAreaAnalysis.css
.viewshed-area-analysis { position: relative; width: 100%; height: 100vh; }
.cesium-container { width: 100%; height: 100%; }

.control-panel {
position: absolute; top: 20px; right: 20px;
background: rgba(255, 255, 255, 0.95); padding: 20px;
border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); min-width: 260px;
}
.control-panel h3 { margin: 0 0 10px 0; font-size: 18px; }
.control-panel p { margin: 0 0 15px 0; color: #666; font-size: 14px; }
.buttons { display: flex; gap: 10px; margin-bottom: 15px; }
.buttons button {
flex: 1; padding: 8px 16px; border: none; border-radius: 4px;
background: #1890ff; color: white; cursor: pointer; font-size: 14px;
}
.buttons button:hover { background: #40a9ff; }
.buttons button:disabled { background: #d9d9d9; cursor: not-allowed; }

.settings { border-top: 1px solid #e8e8e8; padding-top: 15px; margin-bottom: 15px; }
.settings h4 { margin: 0 0 12px 0; font-size: 14px; color: #666; }
.setting-item { margin-bottom: 12px; }
.setting-item label { display: block; font-size: 13px; color: #333; margin-bottom: 4px; }
.setting-item input[type="range"] { width: 100%; }

.legend-item { display: flex; align-items: center; gap: 8px; margin: 4px 0; font-size: 14px; }
.color-box { width: 20px; height: 12px; border-radius: 2px; }

Key Code Explanation

1. Create Area Viewshed Tool

const tool = new DMap3D.analysis.viewshedArea(viewer, {
fovx: 90, // Horizontal field of view (1-130 deg)
fovy: 60, // Vertical field of view (1-179 deg)
visibleColor: '#00ff00', // Visible area color
invisibleColor: '#ff0000', // Non-visible area color
heightOffset: 1.8, // Observation point height offset (meters)
showFrustumRefLines: true, // Show frustum reference lines
})

2. Dynamically Adjust Parameters

// Modify properties for immediate effect
tool.fovx = 120
tool.fovy = 90
tool.visibleColor = '#00ff00'
tool.invisibleColor = '#ff0000'
tool.heightOffset = 2.0

3. Difference from Single-Point Viewshed

FeatureviewshedviewshedArea
Analysis RangeRay detectionFrustum area
Parameter AdjustmentFixedDynamic adjustment supported
Field of ViewNonefovx + fovy
Use CaseQuick checkPrecise analysis

Extended Features

  1. Multiple Observation Points - Analyze multiple observation points simultaneously
  2. Optimal Position - Calculate the best observation position
  3. Viewshed Coverage - Calculate regional visibility coverage rate
  4. Dynamic Analysis - Update analysis in real-time as the camera moves