Slope Measurement Example
This example demonstrates how to perform two-point slope measurement using the DMap3D SDK, displaying both degrees and percentage.
Preview
The user clicks two points, and the system automatically calculates the slope between them, displayed in both degrees and percentage.
Complete Code
src/components/SlopeMeasurement.tsx
import { useEffect, useRef, useState } from 'react'
import * as Cesium from 'cesium'
import DMap3D from 'dmap3d'
import './SlopeMeasurement.css'
function SlopeMeasurement() {
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, 15000),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-45),
roll: 0,
},
})
// Create slope measurement tool
const tool = new DMap3D.measurement.slope(viewer)
tool.updateConfig({
pointColor: '#ff0000',
lineColor: '#00ff00',
fontColor: '#ffffff',
fontSize: 16,
})
toolRef.current = tool
return () => {
tool.destroy()
viewer.destroy()
}
}, [])
const handleStart = () => {
toolRef.current?.start()
setIsActive(true)
}
const handleStop = () => {
toolRef.current?.end()
setIsActive(false)
}
const handleClear = () => {
if (isActive) toolRef.current?.end()
toolRef.current?.clear()
setIsActive(false)
}
return (
<div className="slope-measurement">
<div ref={containerRef} className="cesium-container" />
<div className="control-panel">
<h3>Slope Measurement</h3>
<p>Click two points at different elevations to measure slope</p>
<div className="buttons">
<button onClick={handleStart} disabled={isActive}>
{isActive ? 'Measuring...' : 'Start Measurement'}
</button>
<button onClick={handleStop} disabled={!isActive}>Stop</button>
<button onClick={handleClear}>Clear</button>
</div>
<div className="info">
<h4>Slope Representations</h4>
<table>
<thead>
<tr><th>Representation</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>Degrees (deg)</td><td>Angle with the horizontal plane</td></tr>
<tr><td>Percentage (%)</td><td>Vertical distance / Horizontal distance x 100</td></tr>
</tbody>
</table>
</div>
</div>
</div>
)
}
export default SlopeMeasurement
Stylesheet
src/components/SlopeMeasurement.css
.slope-measurement {
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; }
.info { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.info h4 { margin: 0 0 8px 0; font-size: 14px; color: #666; }
.info table { width: 100%; border-collapse: collapse; font-size: 13px; }
.info th, .info td { padding: 6px 8px; border: 1px solid #e8e8e8; text-align: left; }
.info th { background: #fafafa; color: #666; }
Key Code Explanation
1. Create Slope Measurement Tool
const tool = new DMap3D.measurement.slope(viewer)
Slope measurement is calculated based on the ENU (East-North-Up) coordinate system, with results displayed in both degrees and percentage.
2. Slope Calculation Principle
Measuring the slope between two points:
- Degrees:
arctan(height difference / horizontal distance) - Percentage:
(height difference / horizontal distance) x 100%
For example, 45 deg corresponds to 100%, and 30 deg corresponds to approximately 57.7%.
3. Applicable Scenarios
Recommended for use in areas with terrain relief. Load global terrain:
const viewer = new Cesium.Viewer(container, {
terrain: Cesium.Terrain.fromWorldTerrain(),
})
Extended Features
- Slope Classification - Color-code display by slope grade
- Path Slope - Calculate continuous slope changes along a path
- Maximum Slope - Search for the point of maximum slope within an area
- Slope Statistics - Statistical distribution of slopes within an area