Skip to main content

Buffer Analysis Example

This example demonstrates how to perform buffer analysis using the DMap3D SDK, supporting point, line, and polygon geometry types.

Preview

After the user draws a geometric shape, a buffer zone of the specified distance is automatically generated, with support for dynamic adjustment of buffer distance and geometry type.

Complete Code

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

function BufferAnalysis() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)
const [bufferDistance, setBufferDistance] = useState(50)
const [geometryType, setGeometryType] = useState<string>('polygon')

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,
terrainProvider: new Cesium.EllipsoidTerrainProvider({}),
})
viewerRef.current = viewer

viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 5000),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-45),
roll: 0,
},
})

// Create buffer analysis tool
const tool = new DMap3D.analysis.bufferAnalysis(viewer, {
bufferDistance: 50,
geometryType: 'polygon',
originalColor: '#FFFF00',
bufferColor: '#FF0000',
originalAlpha: 0.6,
bufferAlpha: 0.3,
})
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 = () => {
toolRef.current?.clear()
setIsActive(false)
}

const handleDistanceChange = (value: number) => {
setBufferDistance(value)
toolRef.current?.setBufferDistance(value)
}

const handleTypeChange = (type: string) => {
setGeometryType(type)
toolRef.current?.setGeometryType(type)
}

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

<div className="control-panel">
<h3>Buffer Analysis</h3>
<p>Buffer zone is automatically generated after drawing a shape</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="settings">
<h4>Parameter Settings</h4>

<div className="setting-item">
<label>Geometry Type:</label>
<div className="type-buttons">
{['point', 'polyline', 'polygon'].map((type) => (
<button key={type}
className={geometryType === type ? 'active' : ''}
onClick={() => handleTypeChange(type)}>
{type === 'point' ? 'Point' : type === 'polyline' ? 'Line' : 'Polygon'}
</button>
))}
</div>
</div>

<div className="setting-item">
<label>Buffer Distance: {bufferDistance} m</label>
<input type="range" min="10" max="500" value={bufferDistance}
onChange={(e) => handleDistanceChange(Number(e.target.value))} />
</div>
</div>

<div className="legend">
<div className="legend-item">
<span className="color-box" style={{background: '#FFFF00'}}></span>
<span>Original Shape</span>
</div>
<div className="legend-item">
<span className="color-box" style={{background: '#FF0000', opacity: 0.5}}></span>
<span>Buffer Zone</span>
</div>
</div>
</div>
</div>
)
}

export default BufferAnalysis

Stylesheet

src/components/BufferAnalysis.css
.buffer-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: 260px;
}
.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; }

.settings { border-top: 1px solid #e8e8e8; padding-top: 15px; margin-bottom: 15px; }
.settings h4 { margin: 0 0 12px 0; font-size: 14px; color: #666; }
.setting-item { margin-bottom: 12px; }
.setting-item label { display: block; font-size: 13px; color: #333; margin-bottom: 4px; }
.setting-item input[type="range"] { width: 100%; }

.type-buttons { display: flex; gap: 6px; margin-top: 4px; }
.type-buttons button {
flex: 1; padding: 6px; border: 1px solid #d9d9d9; border-radius: 4px;
background: white; cursor: pointer; font-size: 13px;
}
.type-buttons button.active { background: #1890ff; color: white; border-color: #1890ff; }

.legend-item { display: flex; align-items: center; gap: 8px; margin: 4px 0; font-size: 14px; }
.color-box { width: 20px; height: 12px; border-radius: 2px; }

Key Code Explanation

1. Create Buffer Analysis Tool

const tool = new DMap3D.analysis.bufferAnalysis(viewer, {
bufferDistance: 50, // Buffer distance (meters)
geometryType: 'polygon', // Geometry type
originalColor: '#FFFF00', // Original shape color
bufferColor: '#FF0000', // Buffer zone color
})

2. Supported Geometry Types

TypeDescriptionOperation
pointPoint bufferClick a single point
polylineLine bufferLeft-click to add points, right-click to finish
polygonPolygon bufferLeft-click to add points, right-click to finish

3. Get Analysis Results

const result = tool.getBufferResult()
// result.bufferDistance: Buffer distance
// result.bufferArea: Buffer zone area
// result.originalArea: Original area (for polygon type)

Extended Features

  1. Multi-Level Buffer - Generate multi-level buffer zones at different distances
  2. Merge Buffers - Merge buffer zones from multiple shapes
  3. Area Statistics - Calculate buffer zone area proportion statistics
  4. Impact Range - Analyze features within the buffer zone