-
Notifications
You must be signed in to change notification settings - Fork 1
/
forecast-layer.js
127 lines (112 loc) · 3.79 KB
/
forecast-layer.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
import L from 'leaflet'
import { getNearestForecastTime } from 'weacast-core/common'
let ForecastLayer = L.Layer.extend({
initialize (api, layer, options) {
this.api = api
this.baseLayer = layer
this.onForecastTimeChanged = this.fetchData.bind(this)
L.setOptions(this, options || {})
this.forecastElements = this.options.elements
},
onAdd (map) {
map.addLayer(this.baseLayer)
this.api.on('forecast-time-changed', this.onForecastTimeChanged)
this.fetchData()
},
onRemove (map) {
map.removeLayer(this.baseLayer)
this.api.removeListener('forecast-time-changed', this.onForecastTimeChanged)
},
getQuery () {
// Default implementation
return {
query: {
time: this.currentForecastTime.format(),
$select: ['forecastTime', 'data', 'minValue', 'maxValue'],
$paginate: false,
// Resample according to input parameters
oLon: this.forecastModel.origin[0],
oLat: this.forecastModel.origin[1],
sLon: this.forecastModel.size[0],
sLat: this.forecastModel.size[1],
dLon: this.forecastModel.resolution[0],
dLat: this.forecastModel.resolution[1]
}
}
},
getColorMap (nbColors) {
if (!this.colorMap) return
// Convert from chromajs object to value/color ramp
const dm = this.colorMap.domain()[0]
const dd = this.colorMap.domain()[1] - dm
let colorMap = []
for (let i = 0; i < nbColors; i++) {
const value = dm + ((i / (nbColors - 1)) * dd)
colorMap.push({ value, color: this.colorMap(value) })
}
return colorMap
},
setData (data) {
// To be overriden, call ancestor to set flag and send event
this.hasData = true
this.fire('data', data)
},
fetchData () {
// Not yet ready
if (!this.forecastModel || !this.api.getForecastTime()) return
// Find nearest available data
this.currentForecastTime = getNearestForecastTime(this.api.getForecastTime(), this.forecastModel.interval)
// Already up-to-date ?
if (this.downloadedForecastTime && this.downloadedForecastTime.isSame(this.currentForecastTime)) return
this.downloadedForecastTime = this.currentForecastTime.clone()
// Query data for current time
let query = this.getQuery()
let queries = []
for (let element of this.forecastElements) {
const serviceName = this.forecastModel.name + '/' + element
queries.push(this.api.getService(serviceName).find(query))
}
return Promise.all(queries)
.then(results => {
// To be reactive directly set data after download, flatten because find returns an array even if a single element is selected
this.setData([].concat(...results))
})
},
setForecastModel (model) {
this.forecastModel = model
this.downloadedForecastTime = null
// This will launch a refresh
this.fetchData()
},
setForecastElements (elements) {
this.forecastElements = elements
this.downloadedForecastTime = null
// This will launch a refresh
this.fetchData()
},
getBounds () {
let bounds = new L.LatLngBounds()
if (this.baseLayer && this.baseLayer.getBounds) {
bounds.extend(this.baseLayer.getBounds())
} else if (this.forecastModel) {
let minLon = this.forecastModel.bounds[0]
let minLat = this.forecastModel.bounds[1]
let maxLon = this.forecastModel.bounds[2]
let maxLat = this.forecastModel.bounds[3]
// Take care that leaflet uses [-180, 180]
if (maxLon > 180) {
minLon -= 180
maxLon -= 180
}
bounds.extend([minLat, minLon])
bounds.extend([maxLat, maxLon])
}
return bounds
}
})
L.weacast = {}
L.weacast.ForecastLayer = ForecastLayer
L.weacast.forecastLayer = function (api, layer, options) {
return new L.weacast.ForecastLayer(api, layer, options)
}
export { ForecastLayer }