11//VERSION=3
2-
3- function setup ( ) {
4- return {
5- input : [ "B03" , "B08" , "dataMask" ] ,
6- output : { bands : 4 }
7- } ;
8- }
9-
10-
2+ //ndwi with kndvi
113const colorRamp1 = [
12- [ 0 , 0xFFFFFF ] ,
13- [ 1 , 0x008000 ]
4+ [ 0 , 0xFFFFFF ] , //Black
5+ [ 0.7 , 0x008000 ] //Green (lower if you want a greener map)
146] ;
157const colorRamp2 = [
16- [ 0 , 0xFFFFFF ] ,
17- [ 1 , 0x0000CC ]
8+ [ 0 , 0xFFFFFF ] , //Black
9+ [ 1 , 0x0000CC ] //Medium Blue
1810] ;
1911
20- const viz1 = new ColorRampVisualizer ( colorRamp1 ) ;
21- const viz2 = new ColorRampVisualizer ( colorRamp2 ) ;
12+ let viz1 = new ColorRampVisualizer ( colorRamp1 ) ;
13+ let viz2 = new ColorRampVisualizer ( colorRamp2 ) ;
14+
15+ function setup ( ) {
16+ return {
17+ input : [ "B03" , "B04" , "B08" , "dataMask" ] ,
18+ output : [
19+ { id :"default" , bands : 4 } ,
20+ { id : "index" , bands : 1 , sampleType : "FLOAT32" } ,
21+ { id : "eobrowserStats" , bands : 1 , sampleType : 'FLOAT32' } ,
22+ { id : "dataMask" , bands : 1 }
23+ ]
24+ } ;
25+ }
2226
2327function evaluatePixel ( samples ) {
24- let val = index ( samples . B03 , samples . B08 ) ;
25- if ( val < 0 ) {
26- imgVals = viz1 . process ( - val )
27- } else {
28- imgVals = viz2 . process ( Math . sqrt ( Math . sqrt ( val ) ) )
29- }
30- return imgVals . concat ( samples . dataMask ) ;
28+ let factor = 1 / 2000 ;
29+ let Green = factor * samples . B03 ;
30+ let Red = factor * samples . B04 ;
31+ let NIR = factor * samples . B08 ;
32+ let val = index ( Green , NIR ) ;
33+ let kndvi = Math . tanh ( Math . pow ( ( ( NIR - Red ) / ( NIR + Red ) ) , 2 ) ) ; //https://doi.org/10.1126/sciadv.abc7447
34+ let imgVals = null ;
35+ // The library for tiffs works well only if there is only one channel returned.
36+ // So we encode the "no data" as NaN here and ignore NaNs on frontend.
37+ const indexVal = samples . dataMask === 1 ? val : NaN ;
38+
39+ if ( val < - 0 ) {
40+ imgVals = [ ...viz1 . process ( kndvi ) , samples . dataMask ] ;
41+ } else {
42+ imgVals = [ ...viz2 . process ( Math . sqrt ( Math . sqrt ( val ) ) ) , samples . dataMask ] ;
3143}
44+ return {
45+ default : imgVals ,
46+ index : [ indexVal ] ,
47+ eobrowserStats :[ val ] ,
48+ dataMask : [ samples . dataMask ]
49+ } ;
50+ }
0 commit comments