|
| 1 | +//VERSION=3 |
| 2 | + |
| 3 | +// Set Min and Max values for display and number of steps in the palette |
| 4 | +const minValue = 10 |
| 5 | +const maxValue = 45 |
| 6 | +const numSteps = 200 |
| 7 | + |
| 8 | +// Setup palette |
| 9 | +const palette_colours = palette(numSteps, minValue, maxValue) |
| 10 | +const viz = new ColorRampVisualizer(palette_colours); |
| 11 | + |
| 12 | + |
| 13 | +function setup() { |
| 14 | + return { |
| 15 | + input: ["B10", "BQA", "dataMask"], |
| 16 | + output: [ |
| 17 | + { id: "default", bands: 4 }, |
| 18 | + { id: "eobrowserStats", bands: 2 }, |
| 19 | + { id: "dataMask", bands: 1 }, |
| 20 | + ], |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function evaluatePixel(samples) { |
| 25 | + let val = samples.B10 - 273; |
| 26 | + let clouds = isCloud(samples) |
| 27 | + |
| 28 | + return { |
| 29 | + default: [...viz.process(val), (samples.dataMask * (clouds ? 0 : 1))], |
| 30 | + eobrowserStats: [val, clouds ? 0 : 1], |
| 31 | + dataMask: [samples.dataMask], |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function isCloud(sample) { |
| 36 | + const BQA = decodeL8C2Qa(sample.BQA); |
| 37 | + let cloudPresence = false |
| 38 | + if (BQA.cloud == 1 || BQA.cloudShadow == 1 || BQA.cirrus == 1 || BQA.dilatedCloud == 1) { |
| 39 | + cloudPresence = true |
| 40 | + } |
| 41 | + return cloudPresence |
| 42 | +} |
| 43 | + |
| 44 | +function componentToHex(c) { |
| 45 | + var hex = c.toString(16); |
| 46 | + return hex.length == 1 ? "0" + hex : hex; |
| 47 | + } |
| 48 | + |
| 49 | + function rgbToHex(r, g, b) { |
| 50 | + return "0x" + componentToHex(r) + componentToHex(g) + componentToHex(b); |
| 51 | + } |
| 52 | + |
| 53 | +function palette(colour_length, min_value, max_value){ |
| 54 | + let colourPairs = [] |
| 55 | + let values = Array.from({length: colour_length}, (_, i) => min_value + (max_value - min_value) * i / (colour_length - 1)); |
| 56 | + for (var idx = 0; idx < colour_length; idx++){ |
| 57 | + var x = idx * 1.0/colour_length; |
| 58 | + |
| 59 | + // Convert RGB to hex |
| 60 | + let coulours = rgbToHex(Math.round(255*Math.sqrt(x)), |
| 61 | + Math.round(255*Math.pow(x,3)), |
| 62 | + Math.round(255*(Math.sin(2 * Math.PI * x)>=0? |
| 63 | + Math.sin(2 * Math.PI * x) : |
| 64 | + 0 ))) |
| 65 | + |
| 66 | + //Make pairs of colours |
| 67 | + colourPairs.push([values[idx], coulours.toString()]) |
| 68 | + |
| 69 | + } |
| 70 | + return colourPairs |
| 71 | +} |
0 commit comments