-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
378 lines (314 loc) · 13.3 KB
/
index.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
import RulerControl from 'mapbox-gl-controls/lib/ruler';
import CompassControl from 'mapbox-gl-controls/lib/compass';
import ZoomControl from 'mapbox-gl-controls/lib/zoom';
import AroundControl from 'mapbox-gl-controls/lib/around'
import kriging from './kriging'
var bounds = new mapboxgl.LngLatBounds();
require('dotenv').config()
// mobile nav bar
$(".button-collapse").sideNav();
// page preloader
window.onload = function () {
$('.loader').fadeOut('slow');
document.getElementById("page").style.visibility = 'visible'
// welcome popup
setTimeout(function () { $('.tap-target').tapTarget('open') }, 5000)
setTimeout(function () { $('.tap-target').tapTarget('close') }, 10000)
}
var varOption = document.getElementById("variables")
var performTrainingForm = document.getElementById("trainForm")
var dataInput = document.getElementById("train_data")
var downloadButton = document.getElementById("download_data")
var dataButton = document.getElementById("train_data_button")
var testButtonInput = document.getElementById("test_data")
var testButton = document.getElementById("test_data_button")
// // welcome popup
var info = document.getElementById("info")
// map container //replace with your mapbox access token
mapboxgl.accessToken = process.env.MAPBOX_ACCESS_TOKEN
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/graceamondi/ck1p6wqfs0vj81cqwbikdv87j',
center: [36.74446105957031, -1.2544011203660779],
// zoom: 12
});
// map controls
map.addControl(new ZoomControl(), 'top-left');
map.addControl(new RulerControl(), 'top-left');
map.addControl(new AroundControl(), 'top-left')
map.addControl(new CompassControl(), 'top-left');
// train incoming data
function train(t, x, y, model, sigma2, alpha) {
var variogram = kriging.kriging.train(t, x, y, model, sigma2, alpha);
return variogram
}
// prevent page reload on any form submit
function handleForm(event) { event.preventDefault(); }
// handle uploading files
function uploadBothData() {
// upload datasets
var input = document.getElementById("train_data");
var reader = new FileReader();
reader.onload = function () {
var dataURL = reader.result;
var trainingGeojson = JSON.parse(dataURL);
addTrainToMap(trainingGeojson);
};
reader.readAsText(input.files[0]);
}
// upload both train and test data
dataInput.addEventListener("change", uploadBothData, false)
// add train data to map
function addTrainToMap(trainingGeojson) {
// get variables in train data
var variableOptions = Object.values(trainingGeojson.features[0].properties);
// only use numeric data types
if (typeof (variableOptions[0]) == 'number') {
for (let n = 0; n < variableOptions.length; n++) {
// initialize select field material
varOption.innerHTML += `<option value="${Object.keys(trainingGeojson.features[0].properties)[n]}">${Object.keys(trainingGeojson.features[0].properties)[n]}</option>`;
$('select').material_select();
}
dataButton.classList.add('disabled')
dataButton.addEventListener('mouseenter', function () {
dataButton.style.cursor = 'not-allowed'
})
$('.tap-target').tapTarget('open')
info.innerHTML = `<h5 style="font-family: 'Patrick Hand', cursive;">Woohoo! Go ahead and train your model</h5>`
// add training geojson data to map
map.addSource('Train Layer', {
type: 'geojson',
data: trainingGeojson
});
map.addLayer({
'id': 'Train Layer',
'type': 'circle',
'source': 'Train Layer',
'paint': {
// make circles larger as the user zooms from z12 to z22
'circle-radius': 9.75,
// color circles by ethnicity, using a match expression
// https://docs.mapbox.com/mapbox-gl-js/style-spec/#expressions-match
'circle-color': 'orange',
'circle-stroke-color': 'white',
'circle-stroke-width': 2
}
});
trainingGeojson.features.forEach(function (feature) {
bounds.extend(feature.geometry.coordinates);
});
map.fitBounds(bounds, {
padding: 20,
linear: false
});
var toggleableLayerIds = 'Train Layer'
var link = document.createElement('a');
link.href = '#';
link.className = 'active';
link.innerHTML = `✔`;
link.textContent +=`${toggleableLayerIds}` ;
link.onclick = function (e) {
var clickedLayer = toggleableLayerIds;
e.preventDefault();
e.stopPropagation();
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
var layers = document.getElementById('layergroup');
layers.appendChild(link);
// When a click event occurs on a feature in the Train Layer layer, open a popup at the
// location of the feature, with description HTML from its properties.
var description = []
map.on('click', 'Train Layer', function (e) {
for (var m = 0; m < Object.keys(e.features[0].properties).length; m++) {
var coordinates = e.features[0].geometry.coordinates.slice();
description[m] = `${Object.keys(e.features[0].properties)[m]}:${Object.values(e.features[0].properties)[m]}<br>`;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the Train layer.
map.on('mouseenter', 'Train Layer', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'Train Layer', function () {
map.getCanvas().style.cursor = 'default';
});
}
else {
toastr.options = {
"closeButton": false,
"timeOut": 7000,
"positionClass": "toast-top-right",
"showMethod": 'slideDown',
"hideMethod": 'slideUp',
"closeMethod": 'slideUp',
};
toastr.error(`<p style="font-family: 'Patrick Hand', cursive;">Your Trainind Dataset does not contain any numeric variable.Try Another</p>`);
console.log("no numeric variable");
}
// yep you guessed right! prevent page reload on form submit :-)
performTrainingForm.addEventListener('submit', handleForm);
// perform training when submit button is clicked. I know TMI. lol :-)
performTrainingForm.addEventListener('submit',
function () {
performTraining(trainingGeojson)
$('.tap-target').tapTarget('open')
info.innerHTML = `<h5 style="font-family: 'Patrick Hand', cursive;">Yey!! Model has been trained. Let's Predict</h5>`
}
)
}
// Train variogram model using train data i.e fileData[0]
function performTraining(trainingGeojson) {
var userInput = $("#trainForm").serializeArray();
// generate variogram
var model = userInput[1].value;
var sigma2 = userInput[2].value;
var alpha = userInput[3].value;
var t = [];
var x = [];
var y = [];
var selectedVariable = varOption.options[varOption.selectedIndex].value;
for (var i = 0; i < trainingGeojson.features.length; i++) {
var variable = trainingGeojson.features[i].properties[`${selectedVariable}`];
var X = trainingGeojson.features[i].geometry.coordinates[1];
var Y = trainingGeojson.features[i].geometry.coordinates[0];
t.push(variable);
x.push(X);
y.push(Y);
}
// train data and generate a variogram
var variogram = train(t, x, y, model, sigma2, alpha);
testButtonInput.addEventListener('change', handleForm);
testButtonInput.addEventListener('change', function () {
addTestData(variogram, selectedVariable)
$('.tap-target').tapTarget('open')
info.innerHTML = `<h5 style="font-family: 'Patrick Hand', cursive;">Good Job. Download your predictions.</h5>`
})
};
function addTestData(variogram, selectedVariable) {
var input = document.getElementById("test_data");
var reader = new FileReader()
reader.onload = function () {
var dataUrl = reader.result
var testingGeojson = JSON.parse(dataUrl);
// Predict new data using generated variogram model
performPrediction(testingGeojson, variogram, selectedVariable)
testButton.classList.add('disabled')
testButton.addEventListener('mouseenter', function () {
testButton.style.cursor = 'not-allowed'
})
}
reader.readAsText(input.files[0]);
}
function performPrediction(testingGeojson, variogram, selectedVariable) {
// predict new data
var predctions = []
for (var i = 0; i < testingGeojson.features.length; i++) {
var xnew = testingGeojson.features[i].geometry.coordinates[1];
var ynew = testingGeojson.features[i].geometry.coordinates[0];
var tpredicted = kriging.kriging.predict(xnew, ynew, variogram);
var geom = turf.point([ynew, xnew], { predicted: tpredicted });
predctions.push(geom)
}
var collection = turf.featureCollection(predctions);
// add test data to map
map.addSource(`Predicted Layer`, {
type: 'geojson',
data: collection
});
map.addLayer({
'id': 'Predicted Layer',
'type': 'circle',
'source': `Predicted Layer`,
'paint': {
// make circles larger as the user zooms from z12 to z22
'circle-radius': 9.75,
// color circles by ethnicity, using a match expression
// https://docs.mapbox.com/mapbox-gl-js/style-spec/#expressions-match
'circle-color': 'grey',
'circle-stroke-color': 'white',
'circle-stroke-width': 2
}
});
var toggleableLayerIds = 'Predicted Layer'
var link = document.createElement('a');
link.href = '#';
link.className = 'active';
link.innerHTML = `✔`;
link.textContent +=`${toggleableLayerIds}` ;
link.onclick = function (e) {
var clickedLayer = toggleableLayerIds;
e.preventDefault();
e.stopPropagation();
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
var layers = document.getElementById('layergroup');
layers.appendChild(link);
var bounds = new mapboxgl.LngLatBounds();
testingGeojson.features.forEach(function (feature) {
bounds.extend(feature.geometry.coordinates);
});
map.fitBounds(bounds, {
padding: 20,
linear: false
});
// Change the cursor to a pointer when the mouse is over the Train layer.
map.on('mouseenter', 'Predicted Layer', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'Predicted Layer', function () {
map.getCanvas().style.cursor = 'default';
});
var description = []
map.on('click', 'Predicted Layer', function (e) {
for (var m = 0; m < Object.keys(e.features[0].properties).length; m++) {
var coordinates = e.features[0].geometry.coordinates.slice();
description[m] = `${Object.keys(e.features[0].properties)[m]}:${Object.values(e.features[0].properties)[m]}<br>`;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
downloadButton.addEventListener('click', handleForm)
downloadButton.addEventListener('click', function () {
downloadPredictions(collection, `${selectedVariable}_prediction`)
}, false)
}
// download predicted data
function downloadPredictions(content, filename) {
var file = filename + '.geojson';
saveAs(new File([JSON.stringify(content)], file, {
type: "text/plain;charset=utf-8"
}), file);
}