Skip to content

GeoZarr datacubes ​

GeoZarr datacubes示例。 Zarr GeoZarr 数据立方体 多尺度

除了每个波段一个数组之外,GeoZarr 数据源还支持将所有波段打包进单个 n 维数据数组的数据立方体(例如 (time, band, y, x)):variable 选项指定数组名称,dimensions 选项通过索引或坐标标签固定其非空间维度,其中一个维度还可以选择多个波段。

上面的数据集是来自不同生产者的真实 Zarr v2 和 v3 存储,具有不同的投影和多尺度布局。范围、分辨率、投影和 y 轴方向从存储元数据(proj:/spatial: 约定、bounds/proj4/spatial_ref 属性)中读取,或从坐标数组推断;当存储两者都不提供时(Delta FG CO2),则由 extent 和 flipY 选项补齐。非正方形像素的数据(CMIP6)和南朝上的行(Delta FG CO2)也会被透明地处理。

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

register(proj4);

// 带有等距颜色停靠点的单波段颜色渐变样式。
function ramp(min, max, colors) {
  const stops = [];
  for (let i = 0; i < colors.length; i++) {
    stops.push(min + (i / (colors.length - 1)) * (max - min), colors[i]);
  }
  return {color: ['interpolate', ['linear'], ['band', 1], ...stops]};
}

// 一种 RGB 合成样式,将每个波段从 [min, max] 拉伸到 0..255。
function rgb(min, max) {
  const stretch = (band) => [
    'interpolate',
    ['linear'],
    ['band', band],
    min,
    0,
    max,
    255,
  ];
  return {color: ['color', stretch(1), stretch(2), stretch(3)]};
}

const FTW_URL =
  'https://data.source.coop/ftw/global-data/predictions/zarr/alpha/global.zarr';

const datasets = {
  'ftw-rgb': {
    source: {
      url: FTW_URL,
      variable: 'variables',
      // 最新年份的三个类别概率以 RGB 呈现
      dimensions: {band: [0, 1, 2], time: 1},
    },
    style: rgb(0, 0.5),
  },
  'ftw-field-2024': {
    source: {
      url: FTW_URL,
      variable: 'variables',
      dimensions: {band: [1], time: 0}, // 波段 1 是 `field`(田块)概率
    },
    style: ramp(0, 1, [
      [255, 255, 178],
      [0, 104, 55],
    ]),
  },
  'usgs-dem': {
    source: {
      url: 'https://carbonplan-share.s3.us-west-2.amazonaws.com/zarr-layer-examples/USGS-CONUS-DEM-10m.zarr',
      variable: 'DEM',
    },
    style: ramp(0, 3500, [
      [26, 152, 80],
      [254, 224, 139],
      [140, 81, 10],
      [255, 255, 255],
    ]),
  },
  'hurricane': {
    source: {
      url: 'https://atlantis-vis-o.s3-ext.jc.rl.ac.uk/hurricanes/era5/florence',
      variable: 'surface_pressure',
      dimensions: {time: 0},
    },
    style: ramp(96000, 103000, [
      [68, 1, 84],
      [33, 145, 140],
      [253, 231, 37],
    ]),
  },
  'antarctic-era5': {
    source: {
      url: 'https://carbonplan-share.s3.us-west-2.amazonaws.com/zarr-layer-examples/antarctic_era5.zarr',
      variable: 'wind_speed',
    },
    style: ramp(0, 12, [
      [247, 251, 255],
      [107, 174, 214],
      [8, 48, 107],
    ]),
  },
  'polar': {
    source: {
      url: 'https://carbonplan-share.s3.us-west-2.amazonaws.com/zarr-layer-examples/polar-subset.zarr',
      variable: 'velocity',
    },
    style: ramp(0, 1, [
      [255, 245, 240],
      [251, 106, 74],
      [103, 0, 13],
    ]),
  },
  'fgco2': {
    source: {
      url: 'https://carbonplan-oae-efficiency.s3.us-west-2.amazonaws.com/fgco2-2021-180x360.zarr',
      variable: 'FG_CO2_2',
      dimensions: {time: 0},
      // 该存储既没有空间元数据也没有坐标数组,因此
      // 必须提供范围和南朝上的方向。
      extent: [-180, -90, 180, 90],
      flipY: true,
    },
    style: ramp(-5, 5, [
      [230, 97, 1],
      [255, 255, 255],
      [5, 113, 176],
    ]),
  },
  'carbonplan-4d': {
    source: {
      url: 'https://carbonplan-maps.s3.us-west-2.amazonaws.com/v2/demo/4d/tavg-prec-month',
      variable: 'climate',
      // 也可以通过坐标标签选择波段
      dimensions: {band: ['tavg'], month: 0},
    },
    style: ramp(-30, 30, [
      [5, 113, 176],
      [255, 255, 255],
      [202, 0, 32],
    ]),
  },
  'cmip6-tasmax': {
    source: {
      url: 'https://carbonplan-benchmarks.s3.us-west-2.amazonaws.com/data/NEX-GDDP-CMIP6/ACCESS-CM2/historical/r1i1p1f1/tasmax/tasmax_day_ACCESS-CM2_historical_r1i1p1f1_gn/pyramids-v2-4326-True-128-1-0-0-f4-0-0-0-gzipL1-100',
      variable: 'tasmax',
      dimensions: {time: 0},
    },
    style: ramp(250, 320, [
      [5, 113, 176],
      [255, 255, 255],
      [253, 174, 97],
      [165, 0, 38],
    ]),
  },
  'tos-con': {
    source: {
      url: 'https://atlantis-vis-o.s3-ext.jc.rl.ac.uk/noc-npd-era5-demo/npd-eorca1-era5v1/gn/T1y/tos_con',
      variable: 'tos_con',
      dimensions: {time: 0},
    },
    style: ramp(-2, 30, [
      [5, 48, 97],
      [247, 247, 247],
      [178, 24, 43],
    ]),
  },
};

const select = document.getElementById('dataset-select');

let map;

function render() {
  const dataset = datasets[select.value];

  const source = new GeoZarr(dataset.source);

  if (map) {
    map.setTarget(null);
  }

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

select.addEventListener('change', render);

render();
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">
        <div class="input-group">
          <label class="input-group-text" for="dataset-select">数据集</label>
          <select class="form-select" id="dataset-select">
            <option value="ftw-rgb" selected>FTW 田块预测 2025(RGB,multiscale v3)</option>
            <option value="ftw-field-2024">FTW 田块概率 2024(单波段,multiscale v3)</option>
            <option value="usgs-dem">USGS CONUS DEM 10 米(multiscale v3)</option>
            <option value="hurricane">飓风"弗洛伦斯" — ERA5 地表气压(v3)</option>
            <option value="antarctic-era5">南极 ERA5 风速(v3,极射赤面投影)</option>
            <option value="polar">思韦茨冰川冰速(v2,EPSG:3031)</option>
            <option value="fgco2">Delta FG CO2(v2,南朝上)</option>
            <option value="carbonplan-4d">月平均气温(v2 金字塔,EPSG:3857)</option>
            <option value="cmip6-tasmax">CMIP6 最高气温(v2 金字塔)</option>
            <option value="tos-con">海表温度(v3 金字塔,EPSG:3857)</option>
          </select>
        </div>
      </div>
    </div>

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