Geozarr Groups
Runnable OpenLayers example: Geozarr Groups. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:GeoZarr 多组数据 — OpenLayers geozarr-groups
核心逻辑:同一份影像数据挂两个 Raster 图层,一组出彩色合成,一组出云掩膜
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GeoZarr 多组数据</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label><input type="checkbox" id="surface" checked> 地表反射率组</label>
<label><input type="checkbox" id="cloud" checked> 云掩膜组</label>
<span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
/* 生成三通道反射率,云带按亮度混合成高亮区域 */
function makeScene() {
var size = 512;
var c = document.createElement('canvas');
c.width = size;
c.height = size;
var ctx = c.getContext('2d');
var img = ctx.createImageData(size, size);
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
var veg = 0.5 + 0.3 * Math.sin(x / 39) * Math.cos(y / 47);
var i = (y * size + x) * 4;
var r = 70 + 45 * (1 - veg);
var g = 80 + 110 * veg;
var b = 90 + 40 * (1 - veg);
/* 云带:两条斜向高亮条纹 */
var cloud = Math.max(0, Math.sin((x + y * 0.6) / 55) - 0.35) * 1.6;
cloud = Math.min(1, cloud);
img.data[i] = Math.round(r * (1 - cloud) + 246 * cloud);
img.data[i + 1] = Math.round(g * (1 - cloud) + 248 * cloud);
img.data[i + 2] = Math.round(b * (1 - cloud) + 250 * cloud);
img.data[i + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
return c.toDataURL();
}
var extent = ol.proj.transformExtent(
[116.15, 39.78, 116.60, 40.06], 'EPSG:4326', 'EPSG:3857'
);
var scene = new ol.source.ImageStatic({
url: makeScene(),
projection: 'EPSG:3857',
imageExtent: extent,
crossOrigin: 'anonymous'
});
/* 组一:三通道拉伸成真彩 */
var surfaceLayer = new ol.layer.Image({
source: new ol.source.Raster({
sources: [scene],
operation: function (pixels) {
var src = pixels[0];
var out = new Uint8ClampedArray(src.length);
for (var i = 0; i < src.length; i += 4) {
out[i] = Math.min(255, src[i] * 1.5);
out[i + 1] = Math.min(255, src[i + 1] * 1.35);
out[i + 2] = Math.min(255, src[i + 2] * 1.6);
out[i + 3] = 255;
}
return out;
}
}),
opacity: 0.85
});
/* 组二:云概率高于阈值才保留颜色,其余透明 */
var cloudLayer = new ol.layer.Image({
source: new ol.source.Raster({
sources: [scene],
operation: function (pixels) {
var src = pixels[0];
var out = new Uint8ClampedArray(src.length);
for (var i = 0; i < src.length; i += 4) {
/* 用亮度估计云概率:云带被混合成接近白色 */
var lum = (src[i] + src[i + 1] + src[i + 2]) / 3;
var p = Math.max(0, Math.min(1, (lum - 165) / 80));
if (p > 0.35) {
var v = Math.round(235 - p * 35);
out[i] = v;
out[i + 1] = v;
out[i + 2] = v;
out[i + 3] = Math.min(255, Math.round(p * 320));
} else {
out[i + 3] = 0;
}
}
return out;
}
}),
opacity: 0.9
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.5 }),
surfaceLayer,
cloudLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.38, 39.92]),
zoom: 11,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
function update() {
var s = document.getElementById('surface').checked;
var c = document.getElementById('cloud').checked;
document.getElementById('info').textContent =
'可见组:' + [s ? '反射率' : '', c ? '云掩膜' : ''].filter(Boolean).join(' + ') ||
'无';
}
document.getElementById('surface').addEventListener('change', function () {
surfaceLayer.setVisible(this.checked);
update();
});
document.getElementById('cloud').addEventListener('change', function () {
cloudLayer.setVisible(this.checked);
update();
});
update();
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.