修改瓦片图层样式
修改瓦片图层样式示例。 COG WebGL 样式
当你想根据应用状态的某些变化来更改 WebGL 瓦片图层的样式时,应该使用 layer.updateStyleVariables() 方法。即使样式变量在每一渲染帧都发生变化,图层仍然可以被高效地渲染。如果你需要完全替换图层的样式,可以调用 layer.setStyle() 方法。此方法不应在响应用户频繁操作(例如鼠标移动、拖动滑块等)时调用。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import GeoTIFF from 'ol/source/GeoTIFF.js';
const max = 3000;
function normalize(value) {
return ['/', value, max];
}
const red = normalize(['band', 1]);
const green = normalize(['band', 2]);
const blue = normalize(['band', 3]);
const nir = normalize(['band', 4]);
const trueColor = {
color: ['array', red, green, blue, 1],
gamma: 1.1,
};
const falseColor = {
color: ['array', nir, red, green, 1],
gamma: 1.1,
};
const ndvi = {
color: [
'interpolate',
['linear'],
['/', ['-', nir, red], ['+', nir, red]],
// NDVI 值的颜色渐变,取值范围为 -1 到 1
-0.2,
[191, 191, 191],
-0.1,
[219, 219, 219],
0,
[255, 255, 224],
0.025,
[255, 250, 204],
0.05,
[237, 232, 181],
0.075,
[222, 217, 156],
0.1,
[204, 199, 130],
0.125,
[189, 184, 107],
0.15,
[176, 194, 97],
0.175,
[163, 204, 89],
0.2,
[145, 191, 82],
0.25,
[128, 179, 71],
0.3,
[112, 163, 64],
0.35,
[97, 150, 54],
0.4,
[79, 138, 46],
0.45,
[64, 125, 36],
0.5,
[48, 110, 28],
0.55,
[33, 97, 18],
0.6,
[15, 84, 10],
0.65,
[0, 69, 0],
],
};
const ndviPalettePlasma = {
color: [
'palette',
[
'interpolate',
['linear'],
['/', ['-', nir, red], ['+', nir, red]],
-0.2,
0,
0.65,
4,
],
['#0d0887', '#7e03a8', '#cb4778', '#f89540', '#f0f921'],
],
};
const ndviPaletteViridis = {
color: [
'palette',
[
'interpolate',
['linear'],
['/', ['-', nir, red], ['+', nir, red]],
-0.2,
0,
0.65,
4,
],
['#440154', '#3b528b', '#21918c', '#5ec962', '#fde725'],
],
};
const layer = new TileLayer({
style: trueColor,
source: new GeoTIFF({
normalize: false,
sources: [
{
url: 'https://cloudlessdownloads.eox.at/api/public/dl/jvu06wnt/exploitation-ready-epsg-4326/exploitation-ready_eoxcloudless-sentinel-2-2024_zoom-1_4bands_16bit.tif',
},
],
}),
});
const map = new Map({
target: 'map',
layers: [layer],
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
zoom: 2,
maxZoom: 6,
}),
});
const styles = {
trueColor,
falseColor,
ndvi,
ndviPalettePlasma,
ndviPaletteViridis,
};
const styleSelector = document.getElementById('style');
function update() {
const style = styles[styleSelector.value];
layer.setStyle(style);
}
styleSelector.addEventListener('change', update);index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改瓦片图层样式</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>
设置图层样式
<select id="style">
<option value="trueColor">真彩色</option>
<option value="falseColor">假彩色</option>
<option value="ndvi">NDVI</option>
<option value="ndviPalettePlasma">NDVI(调色板 1)</option>
<option value="ndviPaletteViridis">NDVI(调色板 2)</option>
</select>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "cog-style",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}