-
Notifications
You must be signed in to change notification settings - Fork 138
/
leaflet.timedimension.layer.wms.js
492 lines (433 loc) · 16.4 KB
/
leaflet.timedimension.layer.wms.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* L.TimeDimension.Layer.WMS: wms Layer associated to a TimeDimension
*/
L.TimeDimension.Layer.WMS = L.TimeDimension.Layer.extend({
initialize: function(layer, options) {
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
this._timeCacheBackward = this.options.cacheBackward || this.options.cache || 0;
this._timeCacheForward = this.options.cacheForward || this.options.cache || 0;
this._wmsVersion = this.options.wmsVersion || this.options.version || layer.options.version || "1.1.1";
this._getCapabilitiesParams = this.options.getCapabilitiesParams || {};
this._getCapabilitiesAlternateUrl = this.options.getCapabilitiesUrl || null;
this._getCapabilitiesAlternateLayerName = this.options.getCapabilitiesLayerName || null;
this._proxy = this.options.proxy || null;
this._updateTimeDimension = this.options.updateTimeDimension || false;
this._setDefaultTime = this.options.setDefaultTime || false;
this._updateTimeDimensionMode = this.options.updateTimeDimensionMode || 'intersect'; // 'union' or 'replace'
this._period = this.options.period || null;
this._layers = {};
this._defaultTime = 0;
this._availableTimes = [];
this._capabilitiesRequested = false;
if (this._updateTimeDimension || this.options.requestTimeFromCapabilities) {
this._requestTimeDimensionFromCapabilities();
}
this._baseLayer.on('load', (function() {
this._baseLayer.setLoaded(true);
this.fire('timeload', {
time: this._defaultTime
});
}).bind(this));
},
getEvents: function() {
var clearCache = L.bind(this._unvalidateCache, this);
return {
moveend: clearCache,
zoomend: clearCache
}
},
eachLayer: function(method, context) {
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop)) {
method.call(context, this._layers[prop]);
}
}
return L.TimeDimension.Layer.prototype.eachLayer.call(this, method, context);
},
_onNewTimeLoading: function(ev) {
// console.log('Layer._onNewTimeLoading: ' + this._baseLayer.wmsParams.layers + ' with time: ' + new Date(ev.time).toISOString());
var layer = this._getLayerForTime(ev.time);
if (!this._map.hasLayer(layer)) {
this._map.addLayer(layer);
// console.log('Layer._onNewTimeLoading: layer added to map');
}
},
isReady: function(time) {
var layer = this._getLayerForTime(time);
if (this.options.bounds && this._map)
if (!this._map.getBounds().contains(this.options.bounds))
return true;
return layer.isLoaded();
},
onAdd: function(map) {
L.TimeDimension.Layer.prototype.onAdd.call(this, map);
if (this._availableTimes.length == 0) {
this._requestTimeDimensionFromCapabilities();
} else {
this._updateTimeDimensionAvailableTimes();
}
},
_update: function() {
if (!this._map)
return;
var time = this._timeDimension.getCurrentTime();
// It will get the layer for this time (create or get)
// Then, the layer will be loaded if necessary, adding it to the map (and show it after loading).
// If it already on the map (but probably hidden), it will be shown
var layer = this._getLayerForTime(time);
if (this._currentLayer == null) {
this._currentLayer = layer;
}
if (!this._map.hasLayer(layer)) {
this._map.addLayer(layer);
} else {
this._showLayer(layer, time);
}
},
setOpacity: function(opacity) {
L.TimeDimension.Layer.prototype.setOpacity.apply(this, arguments);
// apply to all preloaded caches
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop) && this._layers[prop].setOpacity) {
this._layers[prop].setOpacity(opacity);
}
}
},
setZIndex: function(zIndex){
L.TimeDimension.Layer.prototype.setZIndex.apply(this, arguments);
// apply to all preloaded caches
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop) && this._layers[prop].setZIndex) {
this._layers[prop].setZIndex(zIndex);
}
}
},
setParams: function(params, noRedraw) {
L.extend(this._baseLayer.options, params);
if (this._baseLayer.setParams) {
this._baseLayer.setParams(params, noRedraw);
}
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop) && this._layers[prop].setParams) {
this._layers[prop].setLoaded(false); // mark it as unloaded
this._layers[prop].setParams(params, noRedraw);
}
}
return this;
},
_unvalidateCache: function() {
var time = this._timeDimension.getCurrentTime();
for (var prop in this._layers) {
if (time != prop && this._layers.hasOwnProperty(prop)) {
this._layers[prop].setLoaded(false); // mark it as unloaded
this._layers[prop].redraw();
}
}
},
_evictCachedTimes: function(keepforward, keepbackward) {
// Cache management
var times = this._getLoadedTimes();
var strTime = String(this._currentTime);
var index = times.indexOf(strTime);
var remove = [];
// remove times before current time
if (keepbackward > -1) {
var objectsToRemove = index - keepbackward;
if (objectsToRemove > 0) {
remove = times.splice(0, objectsToRemove);
this._removeLayers(remove);
}
}
if (keepforward > -1) {
index = times.indexOf(strTime);
var objectsToRemove = times.length - index - keepforward - 1;
if (objectsToRemove > 0) {
remove = times.splice(index + keepforward + 1, objectsToRemove);
this._removeLayers(remove);
}
}
},
_showLayer: function(layer, time) {
if (this._currentLayer && this._currentLayer !== layer) {
this._currentLayer.hide();
}
layer.show();
if (this._currentLayer && this._currentLayer === layer) {
return;
}
this._currentLayer = layer;
this._currentTime = time;
console.log('Show layer ' + layer.wmsParams.layers + ' with time: ' + new Date(time).toISOString());
this._evictCachedTimes(this._timeCacheForward, this._timeCacheBackward);
},
_getLayerForTime: function(time) {
if (time == 0 || time == this._defaultTime || time == null) {
return this._baseLayer;
}
if (this._layers.hasOwnProperty(time)) {
return this._layers[time];
}
var nearestTime = this._getNearestTime(time);
if (this._layers.hasOwnProperty(nearestTime)) {
return this._layers[nearestTime];
}
var newLayer = this._createLayerForTime(nearestTime);
this._layers[time] = newLayer;
newLayer.on('load', (function(layer, time) {
layer.setLoaded(true);
// this time entry should exists inside _layers
// but it might be deleted by cache management
if (!this._layers[time]) {
this._layers[time] = layer;
}
if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) {
this._showLayer(layer, time);
}
// console.log('Loaded layer ' + layer.wmsParams.layers + ' with time: ' + new Date(time).toISOString());
this.fire('timeload', {
time: time
});
}).bind(this, newLayer, time));
// Hack to hide the layer when added to the map.
// It will be shown when timeload event is fired from the map (after all layers are loaded)
newLayer.onAdd = (function(map) {
Object.getPrototypeOf(this).onAdd.call(this, map);
this.hide();
}).bind(newLayer);
return newLayer;
},
_createLayerForTime:function(time){
var wmsParams = this._baseLayer.options;
wmsParams.time = new Date(time).toISOString();
return new this._baseLayer.constructor(this._baseLayer.getURL(), wmsParams);
},
_getLoadedTimes: function() {
var result = [];
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop)) {
result.push(prop);
}
}
return result.sort(function(a, b) {
return a - b;
});
},
_removeLayers: function(times) {
for (var i = 0, l = times.length; i < l; i++) {
if (this._map)
this._map.removeLayer(this._layers[times[i]]);
delete this._layers[times[i]];
}
},
setMinimumForwardCache: function(value) {
if (value > this._timeCacheForward) {
this._timeCacheForward = value;
}
},
_requestTimeDimensionFromCapabilities: function() {
if (this._capabilitiesRequested) {
return;
}
this._capabilitiesRequested = true;
var url = this._getCapabilitiesUrl();
if (this._proxy) {
url = this._proxy + '?url=' + encodeURIComponent(url);
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", (function(xhr) {
var data = xhr.currentTarget.responseXML;
if (data !== null){
this._defaultTime = Date.parse(this._getDefaultTimeFromCapabilities(data));
this._setDefaultTime = this._setDefaultTime || (this._timeDimension && this._timeDimension.getAvailableTimes().length == 0);
this.setAvailableTimes(this._parseTimeDimensionFromCapabilities(data));
if (this._setDefaultTime && this._timeDimension) {
this._timeDimension.setCurrentTime(this._defaultTime);
}
}
}).bind(this));
oReq.overrideMimeType('application/xml');
oReq.open("GET", url);
oReq.send();
},
_getCapabilitiesUrl: function() {
var url = this._baseLayer.getURL();
if (this._getCapabilitiesAlternateUrl)
url = this._getCapabilitiesAlternateUrl;
var params = L.extend({}, this._getCapabilitiesParams, {
'request': 'GetCapabilities',
'service': 'WMS',
'version': this._wmsVersion
});
url = url + L.Util.getParamString(params, url, params.uppercase);
return url;
},
_parseTimeDimensionFromCapabilities: function(xml) {
var layers = xml.querySelectorAll('Layer[queryable="1"]');
var layerName = this._baseLayer.wmsParams.layers;
var layer = null;
var times = null;
layers.forEach(function(current) {
if (current.querySelector("Name").innerHTML === layerName) {
layer = current;
}
})
if (layer) {
times = this._getTimesFromLayerCapabilities(layer);
if (!times) {
times = this._getTimesFromLayerCapabilities(layer.parentNode);
}
}
return times;
},
_getTimesFromLayerCapabilities: function(layer) {
var times = null;
var nodes = layer.children;
for (var i=0, l=nodes.length; i<l; i++){
if (nodes[i].nodeName !== 'Extent' && nodes[i].nodeName !== 'Dimension') continue;
if (nodes[i].getAttribute('name') !== 'time') continue;
if (!nodes[i].textContent.length) continue;
times = nodes[i].textContent.trim();
break;
}
return times;
},
_getDefaultTimeFromCapabilities: function(xml) {
var layers = xml.querySelectorAll('Layer[queryable="1"]');
var layerName = this._baseLayer.wmsParams.layers;
var layer = null;
layers.forEach(function(current) {
if (current.querySelector("Name").innerHTML === layerName) {
layer = current;
}
})
var defaultTime = 0;
if (layer) {
defaultTime = this._getDefaultTimeFromLayerCapabilities(layer);
if (defaultTime == 0) {
defaultTime = this._getDefaultTimeFromLayerCapabilities(layer.parentNode);
}
}
return defaultTime;
},
_getDefaultTimeFromLayerCapabilities: function(layer) {
var defaultTime = 0;
var nodes = layer.children;
for (var i=0, l=nodes.length; i<l; i++) {
if (nodes[i].nodeName !== 'Extent' && nodes[i].nodeName !== 'Dimension') continue;
if (nodes[i].getAttribute('name') !== 'time') continue;
if (!nodes[i].attributes.default) continue;
if (!nodes[i].attributes.default.textContent.length) continue;
defaultTime = nodes[i].attributes.default.textContent.trim();
break;
}
return defaultTime;
},
setAvailableTimes: function(times) {
this._availableTimes = L.TimeDimension.Util.parseTimesExpression(times, this._period);
this._updateTimeDimensionAvailableTimes();
},
_updateTimeDimensionAvailableTimes: function() {
if ((this._timeDimension && this._updateTimeDimension) ||
(this._timeDimension && this._timeDimension.getAvailableTimes().length == 0)) {
this._timeDimension.setAvailableTimes(this._availableTimes, this._updateTimeDimensionMode);
if (this._setDefaultTime && this._defaultTime > 0) {
this._timeDimension.setCurrentTime(this._defaultTime);
}
}
},
_getNearestTime: function(time) {
if (this._layers.hasOwnProperty(time)) {
return time;
}
if (this._availableTimes.length == 0) {
return time;
}
var index = 0;
var len = this._availableTimes.length;
for (; index < len; index++) {
if (time < this._availableTimes[index]) {
break;
}
}
// We've found the first index greater than the time. Get the previous
if (index > 0) {
index--;
}
if (time != this._availableTimes[index]) {
console.log('Search layer time: ' + new Date(time).toISOString());
console.log('Return layer time: ' + new Date(this._availableTimes[index]).toISOString());
}
return this._availableTimes[index];
},
});
if (!L.NonTiledLayer) {
L.NonTiledLayer = (L.Layer || L.Class).extend({});
}
L.NonTiledLayer.include({
_visible: true,
_loaded: false,
_originalUpdate: L.NonTiledLayer.prototype._update,
_originalOnRemove: L.NonTiledLayer.prototype.onRemove,
_update: function() {
if (!this._visible && this._loaded) {
return;
}
this._originalUpdate();
},
onRemove: function(map) {
this._loaded = false;
this._originalOnRemove(map);
},
setLoaded: function(loaded) {
this._loaded = loaded;
},
isLoaded: function() {
return this._loaded;
},
hide: function() {
this._visible = false;
this._div.style.display = 'none';
},
show: function() {
this._visible = true;
this._div.style.display = 'block';
},
getURL: function() {
return this._wmsUrl;
}
});
L.TileLayer.include({
_visible: true,
_loaded: false,
_originalUpdate: L.TileLayer.prototype._update,
_update: function() {
if (!this._visible && this._loaded) {
return;
}
this._originalUpdate();
},
setLoaded: function(loaded) {
this._loaded = loaded;
},
isLoaded: function() {
return this._loaded;
},
hide: function() {
this._visible = false;
if (this._container) {
this._container.style.display = 'none';
}
},
show: function() {
this._visible = true;
if (this._container) {
this._container.style.display = 'block';
}
},
getURL: function() {
return this._url;
}
});
L.timeDimension.layer.wms = function(layer, options) {
return new L.TimeDimension.Layer.WMS(layer, options);
};