forked from sentinel-hub/custom-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhite_water.js
executable file
·95 lines (83 loc) · 2.4 KB
/
white_water.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
/*
Author: Mohor Gartner
*/
// setup values
function setup(ds) {
setInputComponents([ds.B02,ds.B03,ds.B04,ds.B08,ds.B8A,ds.B10,ds.B11,ds.B12]);
setOutputComponentCount(3);
}
// change colour if needed
function colourChange(currC,newC) {
if (newC>currC) {
return newC;
} else {
return currC;
}
}
// evaluate pixel
function evaluatePixel(pix) {
//value to store for every pixel in choosen timeline if it is a white-water. 0->not white-water; 1->white-water; 2-white-water
let WW = 0;
// store bands for white water
let B04ww=0;
let B03ww=0;
let B02ww=0;
//store bands for RGB with no white-water
let B04nat=0;
let B03nat=0;
let B02nat=0;
// loop through timeline
for (var i=0;i<pix.length;i++) {
//simplify band values variables
let B02=pix[i].B02;
let B03=pix[i].B03;
let B04=pix[i].B04;
let B08=pix[i].B08;
let B8A=pix[i].B8A;
let B10=pix[i].B10;
let B11=pix[i].B11;
let B12=pix[i].B12;
//// calculate indices
//ndwi, not needed
//let ndwi=(B03-B08)/(B03+B08);
//moisture index
let moist=(B8A-B11)/(B8A+B11);
//NDSI
let ndsi=(B03-B11)/(B03+B11); //>0.42 should be snowy
//NDVI
let ndvi=(B08-B04)/(B08+B04);
// change maximum white-water value if necessary -> it is needed to store for composite white-water detection
let WWi = (ndsi >= 0.5 && moist >=0.4 && ndvi <=0.2 && B10 < 0.015 && B12 < 0.2 && B11 <= 0.4 && B04 > 0.2)
? (ndsi <= 0.63 && B11 <=0.17 && B04 >=0.35)
? 2
: 1
: 0;
//// take for RGB. Mosaic order should be Least cloud coverage. Therefore non white-water pixels have non cloud coverage.
//there are some errors/empty pixels, where all RGB are 0
//
if (B04!=0 && B03!=0 && B02!=0) {
B04nat=B04;
B03nat=B03;
B02nat=B02;
}
// change WW max if white water detected and higher level than before
if (WWi>=WW) {
WW=WWi;
B04ww=colourChange(B04ww,B04);
B03ww=colourChange(B03ww,B03);
B02ww=colourChange(B02ww,B02);
}
}
//visualization parameters
let gain = 1.5;
let gain1 =2;
let gain2 =2.5;
// set the output colours
let RGB = [B04nat, B03nat, B02nat].map(a => gain * a);
let RGBww1 = [B04ww, B03ww*gain1, 0.25];
let RGBww2 = [B04ww/4,B03ww/3, B02ww].map(a => gain2 * a);
// sets colours according to multi-temporal composite
if (WW==2) return RGBww2;
else if (WW==1) return RGBww1;
else return RGB;
}