Commit 2a89c5f2 authored by Tomas Pettersson's avatar Tomas Pettersson 🏸

first

parents
/node_modules/
/bower_components/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>L.TimeDimension.layer.weather</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
# L.TimeDimension.layer.weather
>
## Description
TimeDimension layer for displaying L.TileLayers with time dimension. Syncs with leaflet-player-control
## Installation
### bower
`bower install https://git.smhi.se/stw/L.TimeDimension.layer.weather.git --save`
## Demo
## Usage
v;
m;
### options
### Methods
## Copyright and License
## Contact information
Tomas Pettersson tomas.pettersson@smhi.se
## Credits and acknowledgements
## Known bugs
## Troubleshooting
## Changelog
{
"name": "L.TimeDimension.layer.weather",
"version": "0.0.1",
"homepage": "https://git.smhi.se/stw/L.TimeDimension.layer.weather",
"authors": [
"Tomas Pettersson <tomas.pettersson@smhi.se>"
],
"description": "TimeDimension layer for displaying L.TileLayers with time dimension. Syncs with leaflet-player-control",
"main": [
"src/L.TimeDimension.layer.weather.js"
],
"repository": {
"type": "git",
"url": "git@git.smhi.se:stw/L.TimeDimension.layer.weather.git"
},
"dependencies": {
"leaflet": "https://github.com/Leaflet/Leaflet.git#v1.0.0-beta.2"
},
"devDependencies": {},
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components"
],
"moduleType": [
"es6"
]
}
L.TimeDimension.Layer.Weather = L.TimeDimension.Layer.extend({
initialize: function( layer, validtimes, getLayerFn, options) {
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
this._layers = {};
this._defaultTime = 0;
this._timeCacheBackward = this.options.cacheBackward || this.options.cache || 0;
this._timeCacheForward = this.options.cacheForward || this.options.cache || 0;
this._getLayerFn = getLayerFn;
this._validtimes = validtimes;
this._onLoadFn = this.options.onLoadFn;
this._baseLayer.on('load', (function() {
this._baseLayer.setLoaded(true);
this.fire('timeload', {
time: this._defaultTime
});
}).bind(this));
},
getEvents : function(){
var clearCache = L.bind(this.clear, this);
return {
moveend: clearCache,
zoomend: clearCache
}
},
// currently not used. Seems to get stuck when redraw map.
_unvalidateCache : function(){
var time = this._timeDimension.getCurrentTime();
for (var prop in this._layers) {
if (time != prop && this._layers.hasOwnProperty(prop)) {
var cacheLayer = this._layers[prop];
cacheLayer.setLoaded(false); // mark it as unloaded
var onLoadFn =(function(layer, time) {
layer.setLoaded(true);
cacheLayer.off('load', onLoadFn);
}).bind(this, cacheLayer, time);
cacheLayer.on('load', onLoadFn);
cacheLayer.redraw();
}
}
},
clear : function() {
for (var prop in this._layers) {
delete this._layers[prop];
}
},
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) {
var time = ev.time;
time = this._getAvailableTime(time);
var layer = this._getLayerForTime(time);
if (!this._map.hasLayer(layer)) {
this._map.addLayer(layer);
layer.hide();
}
},
// Player specific function: get available times to be used in player
getPlayerTimes: function() {
return this._validtimes;
},
// Player specific function: should this layer available times be forced over other layers available times in player
forcePlayerTimes: function() {
return false;
},
isReady: function(time) {
time = this._getAvailableTime(time);
var layer = this._getLayerForTime(time);
return layer.isLoaded();
},
_update: function() {
if (!this._map)
return;
var time = this._timeDimension.getCurrentTime();
time = this._getAvailableTime(time);
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);
}
},
_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);
}
}
},
setMinimumForwardCache: function(value) {
if (value > this._timeCacheForward) {
this._timeCacheForward = value;
}
},
_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;
this._evictCachedTimes(this._timeCacheForward, this._timeCacheBackward);
},
_getLayerForTime: function(time) {
if (time == 0 || time == this._defaultTime) {
return this._baseLayer;
}
if (this._layers.hasOwnProperty(time)) {
return this._layers[time];
}
var newLayer = this._getLayerFn(moment(time));
this._layers[time] = newLayer;
var onLoadFn =(function(layer, time) {
layer.setLoaded(true);
if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) {
this._showLayer(layer, time);
}
if (this._onLoadFn) this._onLoadFn();
this.fire('timeload', {
time: time
});
newLayer.off('load', onLoadFn);
}).bind(this, newLayer, time);
newLayer.on('load', onLoadFn);
// 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;
},
_getAvailableTime: function(time) {
if (this._validtimes[0] > time || time > this._validtimes[this._validtimes.length-1]) {
return time; // time is out of valid time range
}
var validtime = this._validtimes[0];
for (var i = 0; i < this._validtimes.length; i++) {
if (time >= this._validtimes[i]) {
validtime = this._validtimes[i];
}
}
return validtime;
},
_getLoadedTimes: function() {
var result = [];
for (var prop in this._layers) {
if (this._layers.hasOwnProperty(prop)) {
result.push(prop);
}
}
return result.sort();
},
_removeLayers: function(times) {
for (var i = 0, l = times.length; i < l; i++) {
this._map.removeLayer(this._layers[times[i]]);
delete this._layers[times[i]];
}
},
});
L.timeDimension.layer.weather = function(stores, source, layer, validtimes, options) {
return new L.TimeDimension.Layer.Weather(stores, source, layer, validtimes, options);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment