剖面分析示例
这个示例演示如何使用 DMap3D SDK 进行地形剖面分析,沿路径生成高程剖面图。
效果预览
用户在地图上绘制路径,自动沿路径采样地形高程并生成剖面图表。
完整代码
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
// 飞到有地形起伏的区域
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,
},
})
// 创建剖面分析工具
const tool = new DMap3D.analysis.profile(viewer, {
perStep: 50,
perExpansion: 20,
showChart: true,
chartPosition: {
bottom: '20px',
width: '600px',
height: '240px',
},
})
toolRef.current = tool
// 监听分析完成
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>剖面分析</h3>
<p>在地形上绘制路径,生成高程剖面图</p>
<div className="buttons">
<button onClick={handleStart} disabled={isActive}>
{isActive ? '绘制中...' : '开始分析'}
</button>
<button onClick={handleStop} disabled={!isActive}>停止</button>
<button onClick={handleClear}>清除</button>
</div>
<div className="tip">
<h4>操作说明</h4>
<ol>
<li>左键点击添加路径点</li>
<li>右键完成路径绘制</li>
<li>自动在底部显示剖面图</li>
</ol>
</div>
</div>
</div>
)
}
export default ProfileAnalysis
样式文件
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; }
关键代码说明
1. 创建剖面分析工具
const tool = new DMap3D.analysis.profile(viewer, {
perStep: 50, // 每段采样点数
perExpansion: 20, // 每段扩充点数
showChart: true, // 显示剖面图表
chartPosition: { // 图表位置
bottom: '20px',
width: '600px',
height: '240px',
},
})
2. 控制图表显隐
tool.showChart() // 显示图表
tool.hideChart() // 隐藏图表
3. 获取分析结果
const result = tool.getAnalysisResult()
// result: Array<{ distance: number, lnglat: [lng, lat, height] }>
扩展功能
- 导出图表 - 将剖面图导出为图片
- 坡度标注 - 在剖面图上标注坡度变化
- 对比分析 - 多条路径剖面对比
- 通视分析 - 基于剖面进行通视判断