Sea Level (与 WebGL)
Sea Level (与 WebGL)示例。 WebGL math flood
WebGL 瓦片图层的 style 属性接受一个 color 表达式, 可用于在渲染前修改像素值。这里加载并表示高程数据的 RGB 瓦片 会被渲染为:小于或等于海平面的值显示为蓝色,高于海平面的值则为透明。 color 表达式作用于范围从 0 到 1 的归一化像素值。 band 操作符用于从单个波段中选取归一化后的值。
在将归一化的 RGB 值转换为高程之后,使用 case 表达式 来选取在给定高程处应用的颜色。与在 colors 数组中使用常量数值作为分段点不同, var 操作符允许你使用一个可以被应用程序修改的值。 当用户拖动海平面滑块时,会调用 layer.updateStyleVariables() 函数, 用滑块的值更新 level 样式变量。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import {fromLonLat} from 'ol/proj.js';
import ImageTile from 'ol/source/ImageTile.js';
const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const attributions =
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
// 波段运算作用于 0-1 的归一化值
// 因此我们乘以 255,以对齐高程公式
// 来自 https://cloud.maptiler.com/tiles/terrain-rgb-v2/
const elevation = [
'+',
-10000,
['*', 0.1 * 255 * 256 * 256, ['band', 1]],
['*', 0.1 * 255 * 256, ['band', 2]],
['*', 0.1 * 255, ['band', 3]],
];
const layer = new TileLayer({
opacity: 0.6,
source: new ImageTile({
url:
'https://api.maptiler.com/tiles/terrain-rgb-v2/{z}/{x}/{y}.webp?key=' +
key,
tileSize: 512,
maxZoom: 14,
}),
style: {
variables: {
level: 0,
},
color: [
'case',
// 使用 `level` 样式变量来确定颜色
['<=', elevation, ['var', 'level']],
[139, 212, 255, 1],
[139, 212, 255, 0],
],
},
});
const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new ImageTile({
url:
'https://api.maptiler.com/maps/streets-v2/{z}/{x}/{y}.png?key=' + key,
attributions: attributions,
tileSize: 512,
maxZoom: 22,
}),
}),
layer,
],
view: new View({
center: fromLonLat([-122.3267, 37.8377]),
zoom: 11,
}),
});
const control = document.getElementById('level');
const output = document.getElementById('output');
control.addEventListener('input', function () {
output.innerText = control.value;
layer.updateStyleVariables({level: parseFloat(control.value)});
});
output.innerText = control.value;
const locations = document.getElementsByClassName('location');
for (let i = 0, ii = locations.length; i < ii; ++i) {
locations[i].addEventListener('click', relocate);
}
function relocate(event) {
const data = event.target.dataset;
const view = map.getView();
view.setCenter(fromLonLat(data.center.split(',').map(Number)));
view.setZoom(Number(data.zoom));
}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;
}
#level {
display: inline-block;
width: 150px;
vertical-align: text-bottom;
}
a.location {
cursor: pointer;
}
#map {
background: #8bd4ff;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<label>
海平面
<input id="level" type="range" min="0" max="100" value="1"/>
+<span id="output"></span> 米
</label>
<br>
跳转到
<a class="location" data-center="-122.3267,37.8377" data-zoom="11">旧金山</a>、
<a class="location" data-center="-73.9338,40.6861" data-zoom="11">纽约</a>、
<a class="location" data-center="72.9481,18.9929" data-zoom="11">孟买</a> 或
<a class="location" data-center="120.831,31.160" data-zoom="9">上海</a>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "webgl-sea-level",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}