跳到主要内容

坡向分析示例

这个示例演示如何使用 DMap3D SDK 进行地形坡向分析,以颜色渐变显示地形的朝向。

效果预览

在地形上以不同颜色显示坡面的朝向(东、西、南、北等方向),帮助理解地形走势。

完整代码

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

function AspectAnalysis() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isVisible, setIsVisible] = 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.aspectAnalyse(viewer, {
color: '#ff7300',
})
toolRef.current = tool

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

const handleToggle = () => {
const tool = toolRef.current
if (!tool) return
if (isVisible) {
tool.hide()
} else {
tool.show()
}
setIsVisible(!isVisible)
}

const handleClear = () => {
toolRef.current?.hide()
setIsVisible(false)
}

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

<div className="control-panel">
<h3>坡向分析</h3>
<p>以颜色显示地形坡面朝向</p>

<div className="buttons">
<button onClick={handleToggle}>
{isVisible ? '隐藏坡向' : '显示坡向'}
</button>
<button onClick={handleClear}>清除</button>
</div>

<div className="legend">
<h4>方向说明</h4>
<p className="legend-desc">不同颜色代表坡面朝向不同方向,便于分析地形走势和水流方向。</p>
</div>
</div>
</div>
)
}

export default AspectAnalysis

样式文件

src/components/AspectAnalysis.css
.aspect-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: 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; }
.legend { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.legend h4 { margin: 0 0 8px 0; font-size: 14px; color: #666; }
.legend-desc { font-size: 13px; color: #666; margin: 0; }

关键代码说明

1. 创建坡向分析工具

const tool = new DMap3D.analysis.aspectAnalyse(viewer, {
color: '#ff7300', // 基础颜色
})

2. 显示/隐藏

tool.show()  // 显示坡向分析
tool.hide() // 隐藏坡向分析

3. 与坡度分析的区别

分析类型说明工具
坡向分析坡面朝向(方向)aspectAnalyse
坡度分析坡面倾斜程度slopAnalyse

相关 API