Profile Analysis Example
This example demonstrates how to perform terrain profile analysis using the DMap3D SDK, generating an elevation profile chart along a path.
Preview
The user draws a path on the map, and the system automatically samples terrain elevation along the path and generates a profile chart.
Complete Code
src/components/ProfileAnalysis.tsx
import { useEffect, useRef, useState } from 'react'
import * as Cesium from 'cesium'
import DMap3D from 'dmap3d'
import './ProfileAnalysis.css'
function ProfileAnalysis() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)
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
// Fly to an area with terrain relief
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(86.925, 27.988, 30000),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-60),
roll: 0,
},
})
// Create profile analysis tool
const tool = new DMap3D.analysis.profile(viewer, {
perStep: 50,
perExpansion: 20,
showChart: true,
chartPosition: {
bottom: '20px',
width: '600px',
height: '240px',
},
})
toolRef.current = tool
// Listen for analysis completion
tool.onAnalysisEnd(() => {
setIsActive(false)
})
return () => {
tool.destroy()
viewer.destroy()
}
}, [])
const handleStart = () => {
toolRef.current?.activate()
setIsActive(true)
}
const handleStop = () => {
toolRef.current?.deactivate()
setIsActive(false)
}
const handleClear = () => {
const tool = toolRef.current
if (!tool) return
tool.clear()
tool.hideChart()
setIsActive(false)
}
return (
<div className="profile-analysis">
<div ref={containerRef} className="cesium-container" />
<div className="control-panel">
<h3>Profile Analysis</h3>
<p>Draw a path on terrain to generate an elevation profile</p>
<div className="buttons">
<button onClick={handleStart} disabled={isActive}>
{isActive ? 'Drawing...' : 'Start Analysis'}
</button>
<button onClick={handleStop} disabled={!isActive}>Stop</button>
<button onClick={handleClear}>Clear</button>
</div>
<div className="tip">
<h4>Instructions</h4>
<ol>
<li>Left-click to add path points</li>
<li>Right-click to finish path drawing</li>
<li>Profile chart appears automatically at the bottom</li>
</ol>
</div>
</div>
</div>
)
}
export default ProfileAnalysis
Stylesheet
src/components/ProfileAnalysis.css
.profile-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: 250px;
}
.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: 8px; margin-bottom: 15px; }
.buttons button {
flex: 1; padding: 8px 12px; 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; }
.tip { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.tip h4 { margin: 0 0 8px 0; font-size: 14px; color: #666; }
.tip ol { padding-left: 20px; margin: 0; }
.tip li { font-size: 13px; margin: 4px 0; color: #666; }
Key Code Explanation
1. Create Profile Analysis Tool
const tool = new DMap3D.analysis.profile(viewer, {
perStep: 50, // Sample points per segment
perExpansion: 20, // Expansion points per segment
showChart: true, // Show profile chart
chartPosition: { // Chart position
bottom: '20px',
width: '600px',
height: '240px',
},
})
2. Control Chart Visibility
tool.showChart() // Show chart
tool.hideChart() // Hide chart
3. Get Analysis Results
const result = tool.getAnalysisResult()
// result: Array<{ distance: number, lnglat: [lng, lat, height] }>
Extended Features
- Export Chart - Export the profile chart as an image
- Slope Annotation - Annotate slope changes on the profile chart
- Comparative Analysis - Compare profiles of multiple paths
- Line-of-Sight Analysis - Perform line-of-sight analysis based on the profile