-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
343 lines (286 loc) · 8.76 KB
/
main.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
let imageURL;
let zarrUrls = [];
let zarrUrl;
let currentIndex = 0;
let canvas;
/*
This function will be called at startup
*/
async function startup(){
// initialize Zarr URL
zarrUrl = "https://storage.sbg.cloud.ovh.net/v1/AUTH_d40770b0914c46bfb19434ae3e97ae19/hdsa-public/s2_v1_multiscales_4326/T33UWT/20210727";
//set default values to input fields
setInputValue('zarrUrl',zarrUrl);
setBandFieldsWithPredefinedValues();
// call loadZarr(...) function
canvas = document.createElement('canvas');
imageURL = await loadZarr(zarrUrl, canvas);
// create a map to display the zarr image
createMap();
registerResolutionChangeCallback();
//Set current zoom button
setCurrentZoomButton();
//Display the subsetting form.
generateSubsettingForm();
//setDateInfo();
//toggleNavButtons();
//TODO: remove: this should be done in map.js
changeZoomLevel(defaultZoomLevel);
}
async function navigate(index){
var newIndex = currentIndex + index;
if(newIndex < 0){
newIndex = 0;
}
if(newIndex > (zarrUrls.length -1)){
newIndex = zarrUrls.length -1;
}
currentIndex = newIndex;
await applyChange();
setDateInfo();
toggleNavButtons();
}
/**
This function will be called when user click on "Zoom" buttons
*/
async function changeZoomLevel(targetZoomLevel){
setDesiredZoomLevel(targetZoomLevel);
applyChange().then(() =>{
//Update map Zoom
updateMapZoom(targetZoomLevel);
//Disable the current zoom button
disableCurrentZoomButton(targetZoomLevel);
});
}
async function applyChange(){
const loadingBar = document.getElementById('loadingBar');
loadingBar.style.display = 'table';
//retrieve values from the form
zarrUrl = getInputValue('zarrUrl');
redBand = getInputValue('redBand');
greenBand = getInputValue('greenBand');
blueBand = getInputValue('blueBand');
setRequestedExtent(getInputValue("extent"))
let subset = getSubsetValues();
selectedProjection = getSelectedProjectionCode();
setProjectionCode(selectedProjection);
// call loadZarr(...) function
// assign the canvas data URL to the global variable imageURL
imageURL = await loadZarr(zarrUrl,canvas,subset);
//refresh Image view displayed on the Map.
refreshMapView(imageURL,zoomLevel);
registerResolutionChangeCallback();
loadingBar.style.display = 'none';
setCurrentZoomButton();
if(document.getElementById("subsetting-form").innerHTML === ""){
generateSubsettingForm();
}
}
function registerResolutionChangeCallback(){
callback = (event) => {
var zoom = Math.round(getMapZoom());
if(zoom != getDesiredZoomLevel()){
console.log("Update Map Zoom from "+getDesiredZoomLevel()+ " to "+zoom)
changeZoomLevel(zoom);
}
};
onMapResolutionChange(callback);
}
function setCurrentZoomButton(){
var btn = document.getElementById('zoomBtn' + getDesiredZoomLevel());
if(btn){
btn.disabled = true;
btn.style.backgroundColor = "#003366";
}
}
function disableCurrentZoomButton(targetZoomLevel){
for(var i = 1; i <= getMaxZoom(); i++){
var btn = document.getElementById('zoomBtn' + i);
if(btn){
if(i != targetZoomLevel){
btn.disabled = false;
btn.style.backgroundColor = "#17a2b8";
}
}
}
}
function setDateInfo(){
var dateSpan = document.getElementById('dateInfo');
if(dateSpan){
dateSpan.textContent = zarrUrls[currentIndex][1];
}
}
function toggleNavButtons(){
var prevBtn = document.getElementById('prevBtn');
var nextBtn = document.getElementById('nextBtn');
if(currentIndex === 0){
// disable the prev button
prevBtn.disabled = true;
prevBtn.style.backgroundColor = "#cccccc";
prevBtn.style.color = "#666666";
prevBtn.style.cursor = "default";
}else{
prevBtn.disabled = false;
prevBtn.style.backgroundColor = "white";
prevBtn.style.color = "black";
prevBtn.style.cursor = "pointer";
}
if(currentIndex === (zarrUrls.length -1)){
// disable the next button
nextBtn.disabled = true;
nextBtn.style.backgroundColor = "#cccccc";
nextBtn.style.color = "#666666";
nextBtn.style.cursor = "default";
}else{
nextBtn.disabled = false;
nextBtn.style.backgroundColor = "white";
nextBtn.style.color = "black";
nextBtn.style.cursor = "pointer";
}
}
/*
* Set value to an input field
*/
function setInputValue(id,value){
var inputField = document.getElementById(id);
inputField.value = value;
}
/**
* Get input value from input field
* @param {*} id
* @returns
*/
function getInputValue(id){
var value = "";
var inputField = document.getElementById(id);
if(inputField){
value = inputField.value;
if(value){
value = value.trim();
}
}
return value;
}
function getZarrUrl(){
var value = "";
var selectionField = document.getElementById(id);
value = selectionField.options[selectionField.selectedIndex].value;
if(value){
value = value.trim();
}
return value;
}
function generateSubsettingForm(){
console.log('Generating subsetting form...');
subsettingForm = document.getElementById("subsetting-form");
let dimensions = getDimensions();
let dimensionsValues = getDimensionsValues();
let title = document.createElement("h2");
title.innerText = "Subsetting";
subsettingForm.appendChild(title);
dimensions.forEach( dim =>{
console.log('creating input for dimension: '+dim);
values = dimensionsValues[dim];
if(values !== undefined){
let min = values.data[0];
let max = values.data[values.data.length -1];
let label = document.createElement("label");
label.setAttribute("value",dim);
label.setAttribute("for", dim);
label.innerText = dim+" from : ";
subsettingForm.appendChild(label);
let inputStart = document.createElement("input");
inputStart.setAttribute("type", "text");
inputStart.setAttribute("name", dim);
inputStart.setAttribute("id", dim+"_start");
inputStart.setAttribute("value",min);
subsettingForm.appendChild(inputStart);
let middleLabel = document.createElement("label");
middleLabel.setAttribute("value",dim);
middleLabel.setAttribute("for", dim);
middleLabel.innerText = " to ";
subsettingForm.appendChild(middleLabel);
let inputEnd = document.createElement("input");
inputEnd.setAttribute("type", "text");
inputEnd.setAttribute("name", dim);
inputEnd.setAttribute("id", dim+"_end");
inputEnd.setAttribute("value",max);
subsettingForm.appendChild(inputEnd);
let lineReturn = document.createElement("br");
subsettingForm.appendChild(lineReturn);
let secondLineReturn = document.createElement("br");
subsettingForm.appendChild(secondLineReturn);
}
});
}
function clearSubsettingForm(){
console.log("Clearing subsetting form...")
subsettingForm = document.getElementById("subsetting-form");
subsettingForm.innerHTML="";
}
function getSubsetValues(){
subsettingForm = document.getElementById("subsetting-form");
let dimensions = getDimensions();
let subset = [];
dimensions.forEach( dim =>{
console.log('reading input for dimension: '+dim);
let inputStart = document.getElementById(dim+"_start");
let inputEnd = document.getElementById(dim+"_end");
if(inputStart !== null && inputEnd !== null){
subset[dim] = { "start": inputStart.value,"end": inputEnd.value};
}
});
return subset;
}
/**
* Update band fields with valuesmatching the product (predefined values).
*/
function setBandFieldsWithPredefinedValues(){
let selectionField = document.getElementById("zarrUrl");
let productName = selectionField.options[selectionField.selectedIndex].label;
console.log("Product selected: "+productName);
switch(productName){
case "Prisma":
scaleFactor=100;
defaultZoomLevel=10;
setInputValue('redBand',"/reflectance[690]");
setInputValue('greenBand',"/reflectance[591]");
setInputValue('blueBand',"/reflectance[510]");
break;
case "Sentinel-2":
scaleFactor=10;
defaultZoomLevel=9;
setInputValue('redBand',"B04/band_data[0]");
setInputValue('greenBand',"B03/band_data[0]");
setInputValue('blueBand',"B02/band_data[0]");
break;
}
}
/**
* Update selected projection code with the one matching the product (predefined values).
*/
function setProjectionForCurrentProduct(){
let selectionField = document.getElementById("zarrUrl");
let productName = selectionField.options[selectionField.selectedIndex].label;
console.log("Product selected: "+productName);
//projectionField = document.getElementById('projectionCode');
//projectionField.options[projectionField.selectedIndex].value;
switch(productName){
case "Prisma":
setInputValue('projectionCode',"EPSG:32630");
break;
case "Sentinel-2":
setInputValue('projectionCode',"EPSG:4326");
break;
}
}
function productSelectionChanged(){
clearSubsettingForm();
setBandFieldsWithPredefinedValues();
setProjectionForCurrentProduct();
changeZoomLevel(defaultZoomLevel);
}
function getSelectedProjectionCode(){
projectionField = document.getElementById('projectionCode');
return projectionField.options[projectionField.selectedIndex].value;
}