Skip to main content

Skyline Analysis Example

This example demonstrates how to perform skyline analysis using the DMap3D SDK, extracting the skyline contour from the current viewpoint.

Preview

Analyze the skyline from the current viewpoint in the 3D scene, marking the skyline contour of buildings or terrain with colored lines.

Complete Code

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

function SkylineAnalysis() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)
const [color, setColor] = useState('#FF0000')

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 skyline analysis tool
const tool = new DMap3D.analysis.skyline(viewer, {
color: '#FF0000',
})
toolRef.current = tool

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

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

const handleClear = () => {
toolRef.current?.destroy()
// Recreate
const viewer = viewerRef.current
if (viewer) {
toolRef.current = new DMap3D.analysis.skyline(viewer, { color })
}
setIsActive(false)
}

const handleColorChange = (newColor: string) => {
setColor(newColor)
if (isActive) {
toolRef.current?.destroy()
const viewer = viewerRef.current
if (viewer) {
const newTool = new DMap3D.analysis.skyline(viewer, { color: newColor })
newTool.init()
toolRef.current = newTool
}
}
}

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

<div className="control-panel">
<h3>Skyline Analysis</h3>
<p>Analyze the skyline contour from the current viewpoint</p>

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

<div className="settings">
<div className="setting-item">
<label>Skyline Color:</label>
<input type="color" value={color}
onChange={(e) => handleColorChange(e.target.value)} />
</div>
</div>
</div>
</div>
)
}

export default SkylineAnalysis

Stylesheet

src/components/SkylineAnalysis.css
.skyline-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; }
.buttons button:disabled { background: #d9d9d9; cursor: not-allowed; }

.settings { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.setting-item { display: flex; align-items: center; gap: 10px; }
.setting-item label { font-size: 13px; color: #333; }
.setting-item input[type="color"] {
width: 50px; height: 28px; border: 1px solid #d9d9d9; border-radius: 4px; cursor: pointer;
}

Key Code Explanation

1. Create Skyline Analysis Tool

const tool = new DMap3D.analysis.skyline(viewer, {
color: '#FF0000', // Skyline color
})

2. Start Analysis

tool.init() // Start skyline analysis

3. Get 2D Skyline Data

const result = await tool.getSkyline2D()
// result.pixelPoints: Pixel coordinates and colors
// result.groundHeights: Corresponding ground heights

4. Changing Color Requires Rebuilding

tool.destroy()
tool = new DMap3D.analysis.skyline(viewer, { color: '#00FF00' })
tool.init()

Extended Features

  1. Skyline Comparison - Compare skylines from different viewpoints
  2. Occlusion Analysis - Analyze building impact on the skyline
  3. Screenshot Export - Export skyline analysis screenshot
  4. Sunlight Analysis - Calculate sunlight duration combined with the skyline