-
Notifications
You must be signed in to change notification settings - Fork 4
/
erddapToLeafletVelocity.js
198 lines (192 loc) · 10 KB
/
erddapToLeafletVelocity.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
function jsonp(url) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script')
const name = "_jsonp_" + Math.round(100000 * Math.random());
//url formatting
if (url.match(/\?/)) url += "&.jsonp="+name
else url += "?.jsonp="+name
script.src = url;
window[name] = function(data) {
resolve(data);
document.body.removeChild(script);
delete window[name];
}
document.body.appendChild(script);
});
}
L.erddapVelocityLayer = function (options){
return {data: L.fetchErddapVelocity(
options.erddapBaseUrl,
options.datasetID,
options.uParameter,
options.vParameter,
options.minLat,
options.maxLat,
options.minLon,
options.maxLon,
options.refTime,
options.strideLon,
options.strideLat),
minVelocity: options.minVelocity,
maxVelocity: options.maxVelocity,
addTo: function(mapId){
this.data.then(
report => {
try{
var velocityLayer = L.velocityLayer({
displayValues: true,
displayOptions: {
velocityType: "GBR Water",
displayPosition: "bottomleft",
displayEmptyString: "No water data"
},
data: report,
maxVelocity: this.maxVelocity,
minVelocity: this.minVelocity,
velocityScale: 0.1,
opacity: 0.97
}).addTo(mymap);
} catch(err) {
console.log(err.message);
}
}
);
}};
}
L.fetchErddapVelocity = function (
erddapBaseUrl, // `String`: The base URL of the Errdap server to be called - e.g. 'http://erddap.marine.ie'
datasetID, // `String`: The dataset name to be accessed from the Erddap server - 'e.g. IMI_Model_Stats'
uParameter, // `String`: The eastwards velocity parameter name to be accessed from the Erddap dataset - e.g. 'sea_surface_x_velocity'
vParameter, // `String`: The northwards velocity parameter name to be accessed from the Erddap dataset - e.g. 'sea_surface_x_velocity'
minLat, // `Number`: The southernmost latitude to be accessed from the Erddap dataset
maxLat, // `Number`: The northernmost latitude to be accessed from the Erddap dataset
minLon, // `Number`: The westernmost longitude to be accessed from the Erddap dataset
maxLon, // `Number`: The easternmost longitude to be accessed from the Erddap dataset
refTime, // `String`: The dateTime for the data to be returned from Erddap
strideLon, // `Number`: The Erddap stride to use on the grid for the longitude axis as an integer value
strideLat // `Number`: The Erddap stride to use on the grid for the latitude axis as an integer value
){
return jsonp(erddapBaseUrl
+ '/erddap/griddap/'
+ datasetID
+ '.json?'
+ uParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat) + ':(' + String(maxLat) + ')][(' + String(minLon) + '):'+ String(strideLon) +':(' + String(maxLon) + ')],'
+ vParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat)+ ':(' + String(maxLat) + ')][(' + String(minLon) + '):' + String(strideLon) + ':(' + String(maxLon) + ')]')
.then(
data => [{
'header':{
'la1': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'la2': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'lo1': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'lo2': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'dx': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))]))) / ([...new Set(data.table.rows.map(x => x[2]))].length - 1),
'dy': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))]))) / ([...new Set(data.table.rows.map(x => x[1]))].length - 1),
'nx': [...new Set(data.table.rows.map(x => x[2]))].length,
'ny': [...new Set(data.table.rows.map(x => x[1]))].length,
'parameterCategory': 2,
'parameterNumber': 2,
'parameterUnit': 'm.s-1',
'parameterNumberName': data.table.columnNames[3],
'refTime': refTime.replace('T', ' ').replace('Z','')
},
'data': data.table.rows.sort((a,b) => b[1]-a[1]).map(x => x[3])
},{
'header':{
'la1': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'la2': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'lo1': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'lo2': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'dx': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))]))) / ([...new Set(data.table.rows.map(x => x[2]))].length - 1),
'dy': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))]))) / ([...new Set(data.table.rows.map(x => x[1]))].length - 1),
'nx': [...new Set(data.table.rows.map(x => x[2]))].length,
'ny': [...new Set(data.table.rows.map(x => x[1]))].length,
'parameterCategory': 2,
'parameterNumber': 3,
'parameterUnit': 'm.s-1',
'parameterNumberName': data.table.columnNames[4],
'refTime': refTime.replace('T', ' ').replace('Z','')
},
'data': data.table.rows.sort((a,b) => b[1]-a[1]).map(x => x[4])
}]
)
}
erddapToLeafletVelocity = function (
erddapBaseUrl, // `String`: The base URL of the Errdap server to be called - e.g. 'http://erddap.marine.ie'
datasetID, // `String`: The dataset name to be accessed from the Erddap server - 'e.g. IMI_Model_Stats'
uParameter, // `String`: The eastwards velocity parameter name to be accessed from the Erddap dataset - e.g. 'sea_surface_x_velocity'
vParameter, // `String`: The northwards velocity parameter name to be accessed from the Erddap dataset - e.g. 'sea_surface_x_velocity'
minLat, // `Number`: The southernmost latitude to be accessed from the Erddap dataset
maxLat, // `Number`: The northernmost latitude to be accessed from the Erddap dataset
minLon, // `Number`: The westernmost longitude to be accessed from the Erddap dataset
maxLon, // `Number`: The easternmost longitude to be accessed from the Erddap dataset
refTime, // `String`: The dateTime for the data to be returned from Erddap
strideLon, // `Number`: The Erddap stride to use on the grid for the longitude axis as an integer value
strideLat, // `Number`: The Erddap stride to use on the grid for the latitude axis as an integer value
minVelocity, // `Number`: The minimum velocity to use on the Leaflet Velocity display
maxVelocity, // `Number`: The maximum velocity to use on the Leaflet Velocity display
mapID // `String`: The Leaflet map ID to add the velocity layer to
){
jsonp(erddapBaseUrl
+ '/erddap/griddap/'
+ datasetID
+ '.json?'
+ uParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat) + ':(' + String(maxLat) + ')][(' + String(minLon) + '):'+ String(strideLon) +':(' + String(maxLon) + ')],'
+ vParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat)+ ':(' + String(maxLat) + ')][(' + String(minLon) + '):' + String(strideLon) + ':(' + String(maxLon) + ')]')
.then(
data => [{
'header':{
'la1': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'la2': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'lo1': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'lo2': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'dx': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))]))) / ([...new Set(data.table.rows.map(x => x[2]))].length - 1),
'dy': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))]))) / ([...new Set(data.table.rows.map(x => x[1]))].length - 1),
'nx': [...new Set(data.table.rows.map(x => x[2]))].length,
'ny': [...new Set(data.table.rows.map(x => x[1]))].length,
'parameterCategory': 2,
'parameterNumber': 2,
'parameterUnit': 'm.s-1',
'parameterNumberName': data.table.columnNames[3],
'refTime': refTime.replace('T', ' ').replace('Z','')
},
'data': data.table.rows.sort((a,b) => b[1]-a[1]).map(x => x[3])
},{
'header':{
'la1': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'la2': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))])),
'lo1': Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'lo2': Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])),
'dx': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[2]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[2]))]))) / ([...new Set(data.table.rows.map(x => x[2]))].length - 1),
'dy': (Math.max(...Array.from([...new Set(data.table.rows.map(x => x[1]))])) - Math.min(...Array.from([...new Set(data.table.rows.map(x => x[1]))]))) / ([...new Set(data.table.rows.map(x => x[1]))].length - 1),
'nx': [...new Set(data.table.rows.map(x => x[2]))].length,
'ny': [...new Set(data.table.rows.map(x => x[1]))].length,
'parameterCategory': 2,
'parameterNumber': 3,
'parameterUnit': 'm.s-1',
'parameterNumberName': data.table.columnNames[4],
'refTime': refTime.replace('T', ' ').replace('Z','')
},
'data': data.table.rows.sort((a,b) => b[1]-a[1]).map(x => x[4])
}]
).then(
report => {
try{
var velocityLayer = L.velocityLayer({
displayValues: true,
displayOptions: {
velocityType: "GBR Water",
displayPosition: "bottomleft",
displayEmptyString: "No water data"
},
data: report,
maxVelocity: maxVelocity,
minVelocity: minVelocity,
velocityScale: 0.1,
opacity: 0.97
}).addTo(mymap);
} catch(err) {
console.log(err.message);
}
}
)
}