GeoZarr 多组
GeoZarr 多组示例。 Zarr GeoZarr sentinel-2 cloud masking
GeoZarr 数据源支持从同一 Zarr 层级结构中的多个组加载波段。在本示例中,使用 measurements/reflectance 组的 B02(蓝)、B03(绿)和 B04(红)反射率值渲染真彩色合成影像。来自 quality/probability 组的云概率(cld)用于淡出云覆盖区域,露出底下的 OSM 底图。
当波段来自不同的组时,每个波段都被指定为一个包含 name 和 group 路径的对象。group 相对于 url 解析,url 可以是 Zarr 层级结构中的任意公共祖先——不一定是存储根目录。
由于云概率数据只有 20 米分辨率,因此在更高的缩放级别会对其进行重采样,以匹配 10 米分辨率的反射率波段。
main.js
js
import Map from 'ol/Map.js';
import {getView, withExtentCenter, withHigherResolutions} from 'ol/View.js';
import Link from 'ol/interaction/Link.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import GeoZarr from 'ol/source/GeoZarr.js';
import OSM from 'ol/source/OSM.js';
const source = new GeoZarr({
url: 'https://s3.explorer.eopf.copernicus.eu/esa-zarr-sentinel-explorer-fra/tests-output/sentinel-2-l2a/S2B_MSIL2A_20260120T125339_N0511_R138_T27VWL_20260120T131151.zarr',
bands: [
{name: 'b04', group: 'measurements/reflectance'},
{name: 'b03', group: 'measurements/reflectance'},
{name: 'b02', group: 'measurements/reflectance'},
{name: 'cld', group: 'quality/probability'},
],
});
const layer = new TileLayer({
style: {
variables: {threshold: 50},
gamma: 1.5,
color: [
'color',
['interpolate', ['linear'], ['band', 1], 0, 0, 0.5, 255],
['interpolate', ['linear'], ['band', 2], 0, 0, 0.5, 255],
['interpolate', ['linear'], ['band', 3], 0, 0, 0.5, 255],
// 隐藏云概率超过阈值的像素。
['case', ['>', ['band', 4], ['var', 'threshold']], 0, 1],
],
},
source,
});
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
layer,
],
target: 'map',
view: getView(source, withHigherResolutions(2), withExtentCenter()),
});
map.addInteraction(new Link());
const thresholdSlider = document.getElementById('threshold');
const thresholdValue = document.getElementById('threshold-value');
thresholdSlider.addEventListener('input', function () {
thresholdValue.textContent = thresholdSlider.value;
layer.updateStyleVariables({threshold: parseFloat(thresholdSlider.value)});
});index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GeoZarr 多组</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>
<div class="row mt-2">
<div class="col-auto">
<label class="form-label" for="threshold">云阈值: <span id="threshold-value">50</span>%</label>
<input type="range" class="form-range" id="threshold" min="0" max="100" value="50">
</div>
</div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "geozarr-groups",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}