原文链接及内容

运行界面

演示连续更改时间维度时图层平滑地重新加载。数据显示:IEM生成了NWS NEXRAD WSR-88D III级基本反射率的CONUS复合物。

main.js代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Map from 'ol/Map.js';
import Stamen from 'ol/source/Stamen.js';
import TileLayer from 'ol/layer/Tile.js';
import TileWMS from 'ol/source/TileWMS.js';
import View from 'ol/View.js';
import {getCenter} from 'ol/extent.js';
import {transformExtent} from 'ol/proj.js';

function threeHoursAgo() {
return new Date(Math.round(Date.now() / 3600000) * 3600000 - 3600000 * 3);
}

const extent = transformExtent([-126, 24, -66, 50], 'EPSG:4326', 'EPSG:3857');
let startDate = threeHoursAgo();
const frameRate = 0.5; // frames per second
let animationId = null;

const layers = [
new TileLayer({
source: new Stamen({
layer: 'terrain',
}),
}),
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'},
}),
}),
];
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: getCenter(extent),
zoom: 4,
}),
});

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

function setTime() {
startDate.setMinutes(startDate.getMinutes() + 15);
if (startDate > Date.now()) {
startDate = threeHoursAgo();
}
layers[1].getSource().updateParams({'TIME': startDate.toISOString()});
updateInfo();
}
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);

updateInfo();

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WMS Time</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>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>