Geozarr
Runnable OpenLayers example: Geozarr. 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
核心逻辑:页面内生成三通道反射率影像,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:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar label{display:flex;gap:6px;align-items:center}
.legend{display:flex;align-items:center;gap:6px}
.ramp{width:120px;height:10px;border-radius:2px;
background:linear-gradient(90deg,#8d6e3f,#c7c05a,#4c9a3f,#14532d)}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span class="legend">指数 <i class="ramp"></i> 低 → 高</span>
<label>不透明度 <input type="range" id="op" min="20" max="100" value="80"></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;
}
/* 页面内构造“哨兵 2 号”风格的三波段场景:R=红光,G=近红外,B=短波红外 */
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.34 * Math.sin(x / 43) * Math.cos(y / 57) +
0.16 * Math.sin((x + y) / 19);
veg = Math.max(0, Math.min(1, veg));
var red = 55 + 45 * (1 - veg) + 12 * Math.sin(x / 7);
var nir = 80 + 150 * veg;
var swir = 60 + 70 * (1 - veg) + 15 * Math.cos(y / 11);
var i = (y * size + x) * 4;
img.data[i] = Math.round(red);
img.data[i + 1] = Math.round(nir);
img.data[i + 2] = Math.round(swir);
img.data[i + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
return { url: c.toDataURL(), data: img.data };
}
var sceneData = makeScene();
/* 统计在渲染前一次算完,避免在 operation 里改图层属性触发重复渲染 */
function meanIndex(data) {
var sum = 0, count = 0;
for (var i = 0; i < data.length; i += 4) {
var idx = (data[i + 1] - data[i]) / Math.max(1, data[i + 1] + data[i]);
sum += Math.max(0, Math.min(1, (idx + 0.2) / 1.2));
count++;
}
return sum / count;
}
var extent = ol.proj.transformExtent(
[116.15, 39.78, 116.60, 40.06], 'EPSG:4326', 'EPSG:3857'
);
var scene = new ol.source.ImageStatic({
url: sceneData.url,
projection: 'EPSG:3857',
imageExtent: extent,
crossOrigin: 'anonymous'
});
/* 指数色带:低值棕黄,高值深绿 */
var STOPS = [
[0.00, 141, 110, 63],
[0.35, 199, 192, 90],
[0.60, 76, 154, 63],
[1.00, 20, 83, 45]
];
function ramp(t) {
for (var i = 1; i < STOPS.length; i++) {
if (t <= STOPS[i][0]) {
var a = STOPS[i - 1], b = STOPS[i];
var k = (t - a[0]) / (b[0] - a[0]);
return [
Math.round(a[1] + (b[1] - a[1]) * k),
Math.round(a[2] + (b[2] - a[2]) * k),
Math.round(a[3] + (b[3] - a[3]) * k)
];
}
}
return [20, 83, 45];
}
var layer = 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 red = src[i];
var nir = src[i + 1];
var idx = (nir - red) / Math.max(1, nir + red);
idx = Math.max(0, Math.min(1, (idx + 0.2) / 1.2));
var rgb = ramp(idx);
out[i] = rgb[0];
out[i + 1] = rgb[1];
out[i + 2] = rgb[2];
out[i + 3] = 255;
}
return out;
}
}),
opacity: 0.8
});
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.55 }),
layer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.38, 39.92]),
zoom: 11,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
document.getElementById('info').textContent =
'平均指数 ' + meanIndex(sceneData.data).toFixed(3) + ' · 512 × 512 合成波段';
document.getElementById('op').addEventListener('input', function () {
layer.setOpacity(this.value / 100);
});
})();
</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.