evalscript JS function

 avatar
yasserakbbach
javascript
2 years ago
3.3 kB
6
Indexable
Never
//VERSION=3
function setup() {
    return {
      input: [{
        bands:["B04", "B08", "dataMask"],
      }],
      output: {
        id: "default",
        bands: 4,
      }
    }
  }
  function evaluatePixel(sample) {
      let ndvi = (sample.B08 - sample.B04) / (sample.B08 + sample.B04)
      let step = 0.03125
      
      if (ndvi <= step * 0 && ndvi < step * 1) return [0.46,0,0, sample.dataMask]
      else if (ndvi <= step * 1 && ndvi < step * 2) return [0.56,0,0, sample.dataMask]
      else if (ndvi <= step * 2 && ndvi < step * 3) return [0.65,0,0, sample.dataMask]
      else if (ndvi <= step * 3 && ndvi < step * 4) return [0.76,0,0, sample.dataMask]
      else if (ndvi <= step * 4 && ndvi < step * 5) return [0.89,0.02,0, sample.dataMask]
      else if (ndvi <= step * 5 && ndvi < step * 6) return [0.99,0.14,0, sample.dataMask]
      else if (ndvi <= step * 6 && ndvi < step * 7) return [0.99,0.26,0, sample.dataMask]
      else if (ndvi <= step * 7 && ndvi < step * 8) return [0.99,0.34,0, sample.dataMask]
      else if (ndvi <= step * 8 && ndvi < step * 9) return [0.99,0.42,0.04, sample.dataMask]
      else if (ndvi <= step * 9 && ndvi < step * 10) return [0.99,0.51,0.04,sample.dataMask]
      else if (ndvi <= step * 10 && ndvi < step * 11) return [0.99,0.58,0.04,sample.dataMask]
      else if (ndvi <= step * 11 && ndvi < step * 12) return [0.99,0.65,0.04,sample.dataMask]
      else if (ndvi <= step * 12 && ndvi < step * 13) return [0.99,0.73,0.04,sample.dataMask]
      else if (ndvi <= step * 13 && ndvi < step * 14) return [0.99,0.79,0.04,sample.dataMask]
      else if (ndvi <= step * 14 && ndvi < step * 15) return [0.99,0.85,0.04,sample.dataMask]
      else if (ndvi <= step * 15 && ndvi < step * 16) return [0.99,0.91,0.04,sample.dataMask]
      else if (ndvi <= step * 16 && ndvi < step * 17) return [0.99,0.97,0.05,sample.dataMask]
      else if (ndvi <= step * 17 && ndvi < step * 18) return [0.96,0.98,0.05,sample.dataMask]
      else if (ndvi <= step * 18 && ndvi < step * 19) return [0.90,0.97,0.06,sample.dataMask]
      else if (ndvi <= step * 19 && ndvi < step * 20) return [0.83,0.96,0.07,sample.dataMask]
      else if (ndvi <= step * 20 && ndvi < step * 21) return [0.76,0.94,0.08,sample.dataMask]
      else if (ndvi <= step * 21 && ndvi < step * 22) return [0.69,0.93,0.08,sample.dataMask]
      else if (ndvi <= step * 22 && ndvi < step * 23) return [0.61,0.93,0.08,sample.dataMask]
      else if (ndvi <= step * 23 && ndvi < step * 24) return [0.51,0.91,0.09,sample.dataMask]
      else if (ndvi <= step * 24 && ndvi < step * 25) return [0.43,0.83,0,sample.dataMask]
      else if (ndvi <= step * 25 && ndvi < step * 26) return [0.34,0.76,0,sample.dataMask]
      else if (ndvi <= step * 26 && ndvi < step * 27) return [0.27,0.69,0,sample.dataMask]
      else if (ndvi <= step * 27 && ndvi < step * 28) return [0.18,0.63,0,sample.dataMask]
      else if (ndvi <= step * 28 && ndvi < step * 29) return [0.07,0.56,0,sample.dataMask]
      else if (ndvi <= step * 29 && ndvi < step * 30) return [0,0.49,0,sample.dataMask]
      else if (ndvi <= step * 30 && ndvi < step * 31) return [0,0.43,0,sample.dataMask]
      else return [0,0.35,0,sample.dataMask]
  }