WebGL 要素过滤
WebGL 要素过滤示例。 WebGL 图标 精灵 过滤 要素
本示例演示如何使用带有字面量样式(literal style)的 ol/layer/WebGLVector 动态过滤大量的 点几何图形。上面的地图基于 NASA 的一个数据集,其中包含 45k 个已记录的陨石 着陆点。每个陨石在地图上用一个圆圈标记(圆圈越大,表示 该物体越重)。还添加了脉冲效果,并根据撞击年份略有偏移。
调整滑块会使日期范围之外的对象从地图中被过滤掉。这是通过 修改提供给 WebGL 图层的 style 对象所使用的变量来实现的。另请注意,最后一段代码 是确保地图每一帧都自行刷新所必需的。
main.js
js
import Feature from 'ol/Feature.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import Point from 'ol/geom/Point.js';
import TileLayer from 'ol/layer/Tile.js';
import WebGLVectorLayer from 'ol/layer/WebGLVector.js';
import {fromLonLat} from 'ol/proj.js';
import StadiaMaps from 'ol/source/StadiaMaps.js';
import Vector from 'ol/source/Vector.js';
const vectorSource = new Vector({
attributions: 'NASA',
});
const oldColor = 'rgba(242,56,22,0.61)';
const newColor = '#ffe52c';
const period = 12; // 动画周期(秒)
const animRatio = [
'^',
[
'/',
[
'%',
[
'+',
['time'],
['interpolate', ['linear'], ['get', 'year'], 1850, 0, 2015, period],
],
period,
],
period,
],
0.5,
];
const style = {
'circle-radius': [
'*',
['interpolate', ['linear'], ['get', 'mass'], 0, 4, 200000, 13],
['-', 1.75, ['*', animRatio, 0.75]],
],
'circle-fill-color': [
'interpolate',
['linear'],
animRatio,
0,
newColor,
1,
oldColor,
],
'circle-opacity': ['-', 1.0, ['*', animRatio, 0.75]],
};
// 处理输入值与事件
const minYearInput = document.getElementById('min-year');
const maxYearInput = document.getElementById('max-year');
function updateStatusText() {
const div = document.getElementById('status');
div.querySelector('span.min-year').textContent = minYearInput.value;
div.querySelector('span.max-year').textContent = maxYearInput.value;
}
const pointsLayer = new WebGLVectorLayer({
variables: {
minYear: parseInt(minYearInput.value),
maxYear: parseInt(maxYearInput.value),
},
style: [
{
style,
filter: [
'between',
['get', 'year'],
['var', 'minYear'],
['var', 'maxYear'],
],
},
],
source: vectorSource,
disableHitDetection: true,
});
minYearInput.addEventListener('input', function () {
pointsLayer.updateStyleVariables({minYear: parseInt(minYearInput.value)});
updateStatusText();
});
maxYearInput.addEventListener('input', function () {
pointsLayer.updateStyleVariables({maxYear: parseInt(maxYearInput.value)});
updateStatusText();
});
updateStatusText();
// 加载数据;
const client = new XMLHttpRequest();
client.open('GET', 'data/csv/meteorite_landings.csv');
client.onload = function () {
const csv = client.responseText;
const features = [];
let prevIndex = csv.indexOf('\n') + 1; // 跳过表头行
let curIndex;
while ((curIndex = csv.indexOf('\n', prevIndex)) != -1) {
const line = csv.substr(prevIndex, curIndex - prevIndex).split(',');
prevIndex = curIndex + 1;
const coords = fromLonLat([parseFloat(line[4]), parseFloat(line[3])]);
if (isNaN(coords[0]) || isNaN(coords[1])) {
// 防御错误数据
continue;
}
features.push(
new Feature({
mass: parseFloat(line[1]) || 0,
year: parseInt(line[2]) || 0,
geometry: new Point(coords),
}),
);
}
vectorSource.addFeatures(features);
};
client.send();
const map = new Map({
layers: [
new TileLayer({
source: new StadiaMaps({
layer: 'stamen_toner',
}),
}),
pointsLayer,
],
target: document.getElementById('map'),
view: new View({
center: [0, 0],
zoom: 2,
}),
});
// 让地图持续播放动画
function animate() {
map.render();
window.requestAnimationFrame(animate);
}
animate();index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用 WebGL 过滤要素</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<form>
<div id="status">显示 <span class="min-year"></span> 与 <span class="max-year"></span> 之间的撞击事件</div>
<label for="min-year">最小年份:</label>
<input id="min-year" type="range" min="1850" max="2015" step="1" value="1850"/>
<label for="max-year">最大年份:</label>
<input id="max-year" type="range" min="1850" max="2015" step="1" value="2015"/>
</form>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "filter-points-webgl",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}