Edit

WMS Time (WebGL)

wms14 time4 dimensions4 transition4 nexrad2 webgl19

Smooth tile transitions on a WebGL tile layer when changing the time dimension of a tiled WMS layer.

Demonstrates smooth reloading of layers when changing the time dimension continuously. When the source key changes by calling updateParams() on the source, the renderer keeps the previous frame's tiles visible until the new ones load, so the animation stays smooth instead of flashing. Data shown: IEM generated CONUS composite of NWS NEXRAD WSR-88D level III base reflectivity.

main.js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {getCenter} from 'ol/extent.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import {transformExtent} from 'ol/proj.js';
import StadiaMaps from 'ol/source/StadiaMaps.js';
import TileWMS from 'ol/source/TileWMS.js';

const interval = 3 * 60 * 60 * 1000;
const step = 15 * 60 * 1000;
const frameRate = 0.5; // frames per second
const extent = transformExtent([-126, 24, -66, 50], 'EPSG:4326', 'EPSG:3857');

let wmsTime = new Date();
let animationId = null;

const stadiaLayer = new TileLayer({
  source: new StadiaMaps({
    layer: 'stamen_terrain',
  }),
});

const tileWmsLayer = new TileLayer({
  extent: extent,
  source: new TileWMS({
    attributions: ['Iowa State University'],
    url: 'https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r-t.cgi',
    params: {'LAYERS': 'nexrad-n0r-wmst'},
    crossOrigin: 'anonymous',
  }),
});

const map = new Map({
  layers: [stadiaLayer, tileWmsLayer],
  target: 'map',
  view: new View({
    center: getCenter(extent),
    zoom: 4,
  }),
});

const el = document.getElementById('info');
function updateInfo(time) {
  el.innerHTML = time.toISOString();
}

function threeHoursAgo() {
  return new Date(Math.floor((Date.now() - interval) / step) * step);
}

function setTime() {
  wmsTime.setMinutes(wmsTime.getMinutes() + 15);
  if (wmsTime.getTime() > Date.now()) {
    wmsTime = threeHoursAgo();
  }
  tileWmsLayer.getSource().updateParams({'TIME': wmsTime.toISOString()});
  updateInfo(wmsTime);
}
setTime();

const stop = function () {
  if (animationId !== null) {
    window.clearInterval(animationId);
    animationId = null;
  }
};

const play = function () {
  stop();
  animationId = window.setInterval(setTime, 1000 / frameRate);
};

const startButton = document.getElementById('play');
startButton.addEventListener('click', play, false);

const stopButton = document.getElementById('pause');
stopButton.addEventListener('click', stop, false);
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>WMS Time (WebGL)</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 role="group" aria-label="Animation controls">
      <button id="play" type="button">Play</button>
      <button id="pause" type="button">Pause</button>
      <span id="info"></span>
    </div>

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