Skip to content

GeoZarr dimensions ​

GeoZarr dimensions示例。 Zarr GeoZarr sentinel-1 time 维度

GeoZarr 数组可以拥有 time 等非空间维度。本示例使用滑块遍历 Sentinel-1 雷达数据立方体的 time 轴:getDimensions() 报告该轴及其大小和单位,updateDimensions() 在原位重新加载所选切片,getValue 读取轴值并用获取日期为其标注。

该数据立方体以双极化合成影像展示 2021 年拉帕尔马岛 Cumbre Vieja 火山喷发:VV 和 VH 波段(垂直发射、垂直/水平接收)分别映射到红色和绿色,二者的比值映射到蓝色。

main.js
js
import Map from 'ol/Map.js';
import {
  getView,
  withExtentCenter,
  withHigherResolutions,
  withLowerResolutions,
  withZoom,
} from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import GeoZarr from 'ol/source/GeoZarr.js';
import OSM from 'ol/source/OSM.js';

// 将 url 直接指向数据立方体的 `descending`(降轨)轨道组,这样
// 它就是一个普通的单组数据源,其唯一的非空间维度是时间。
const url =
  'https://s3.explorer.eopf.copernicus.eu/esa-zarr-sentinel-explorer-fra/tests-output/sentinel-1-grd-rtc-staging/s1-rtc-28RBS.zarr/descending';
const source = new GeoZarr({url, bands: ['vv', 'vh']});

// 由两个地理波段构成的双极化合成影像:VV -> 红色,VH -> 绿色,
// 二者的比值 VV/VH -> 蓝色。雷达后向散射是一个很小的浮点数,因此
// 每个通道都从固定的 [低, 高] 范围重新缩放到 0-255 显示范围。
const style = {
  color: [
    'color',
    ['interpolate', ['linear'], ['band', 1], 0, 0, 0.4, 255], // VV
    ['interpolate', ['linear'], ['band', 2], 0, 0, 0.1, 255], // VH
    ['interpolate', ['linear'], ['/', ['band', 1], ['band', 2]], 1, 0, 15, 255], // VV/VH
  ],
};

const map = new Map({
  target: 'map',
  layers: [new TileLayer({source: new OSM()}), new TileLayer({style, source})],
  view: getView(
    source,
    withLowerResolutions(1),
    withHigherResolutions(2),
    withExtentCenter(),
    withZoom(2),
  ),
});

const slider = document.getElementById('time');
const label = document.getElementById('time-label');

source
  .getDimensions()
  .then(async ({time}) => {
    const {units} = time.attributes;
    // units 类似于 "nanoseconds since 1970-01-01",
    const epoch = Date.parse(units.split(' since ')[1]);
    const toDate = (value) => new Date(epoch + Number(value) / 1e6);

    const slices = await Promise.all(
      [...Array(time.size).keys()].map(async (index) => ({
        index,
        date: toDate(await source.getValue('time', index)),
      })),
    );
    // 数据立方体存储的时间切片不是按时间顺序排列的,因此要先排序。
    slices.sort((a, b) => a.date - b.date);

    function showSlice(position) {
      const {index, date} = slices[position];
      source.updateDimensions({time: index});
      label.textContent = date.toISOString().slice(0, 16).replace('T', ' ');
    }

    slider.max = String(time.size - 1);
    slider.disabled = false;
    slider.addEventListener('input', () => showSlice(Number(slider.value)));
    showSlice(0);
  })
  .catch(() => (label.textContent = 'Failed to load the GeoZarr store.'));
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 g-2 align-items-center">
      <div class="col-auto">
        <label class="col-form-label" for="time">时间</label>
      </div>
      <div class="col-auto">
        <input type="range" class="form-range align-middle" id="time" min="0" max="0" value="0" step="1" style="width: 220px;" disabled>
      </div>
      <div class="col-auto">
        <span id="time-label" class="text-body-secondary">加载中…</span>
      </div>
    </div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "geozarr-dimensions",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}