SHARE
TWEET

Untitled

cawo4 Sep 22nd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Wildfire and burn scar visualization in Sentinel-2 images V2.0.0
  2. // Twitter: Pierre Markuse (@pierre_markuse)
  3. // CC BY 4.0 International - https://creativecommons.org/licenses/by/4.0/
  4. function a(a, b) {return a + b};
  5. function stretch(val, min, max) {return (val - min) / (max - min);}
  6. function satEnh(rgbArr) {
  7.     var avg = rgbArr.reduce((a, b) => a + b, 0) / rgbArr.length;
  8.     return rgbArr.map(a => avg * (1 - saturation) + a * saturation); }
  9. function highlightBurnscar(val, oLow, oHigh, deSat, darken) {
  10.     if ((B12 + B11 > 0.05) && (val > 0)) {
  11.         if (((B8A - B12) / (B8A + B12)) > oLow) {
  12.             saturation = saturation - deSat;
  13.             stretchMax = stretchMax + darken;
  14.         } else {
  15.             if (((B8A - B12) / (B8A + B12)) <= oHigh) {
  16.                 noFire[0] = noFire[0] + 0.2 * val;
  17.                 noFire[1] = noFire[1] + 0.05 * val;
  18.             } else {
  19.                 noFire[0] = noFire[0] + 0.15 * val;
  20.                 noFire[1] = noFire[1] + 0.15 * val;
  21.             }
  22.         }
  23.     }
  24. }
  25. function indexMap(ind, lVal, mVal, hVal, cont, dir, pal) {
  26.   var col1=GREEN;var col2=YELLOW;var col3=RED;  
  27.   if (pal == 1) {col1=CBL;col2=CBM;col3=CBH;}
  28.   if (pal == 2) {col1=OWNL;col2=OWNM;col3=OWNH;}        
  29.     var lValCol = col1;
  30.     var mValCol = col2;var hValCol = col3;
  31.     if (dir == 1){
  32.         lValCol = col3;hValCol = col1;
  33.     }
  34.     if (cont == 0) {
  35.         if (ind <= lVal) return lValCol; if ((ind > lVal) && (ind < hVal)) return mValCol; if (ind >= hVal) return hValCol;
  36.       } else {
  37.         return colorBlend(ind, [lVal, mVal,hVal], [lValCol,mValCol,hValCol]);
  38.     }
  39. }
  40. function blend(bArr1, bArr2, opa1, opa2) {
  41.     return bArr1.map(function(num, index) {
  42.         return (num / 100 * opa1 + bArr2[index] / 100 * opa2);
  43.     });
  44. }
  45. function applyEnh(bArr) {
  46.     highlightBurnscar(burnscarHighlight, burnscarThresholdLow, burnscarThresholdHigh, burnscarDesaturateBackdrop, burnscarDarkenBackdrop);
  47.     return satEnh([stretch(bArr[0], stretchMin, stretchMax), stretch(bArr[1], stretchMin, stretchMax), stretch(bArr[2], stretchMin, stretchMax)]);
  48. }
  49. var BLACK = [0.0, 0.0, 0.0];
  50. var RED = [0.9, 0.1, 0.1];
  51. var YELLOW = [0.9, 0.9, 0.1];
  52. var GREEN = [0.0, 0.6, 0.0];
  53. var CBL  = [0/255, 80/255, 0/255];
  54. var CBM  = [120/255, 120/255, 230/255];
  55. var CBH  = [70/255, 195/255, 255/255];
  56. var OWNL = [0.0, 0.0, 0.0];
  57. var OWNM = [0.0, 0.0, 0.0];
  58. var OWNH = [0.0, 0.0, 0.0];
  59. // Visualization style of the different fire zones
  60. var Fire1OVL = [stretch((2.1 * B04 + 0.5 * B12), 0.01, 0.99) + 1.1, stretch((2.2 * B03 + 0.5 * B08), 0.01, 0.99), stretch(2.1 * B02, 0.01, 0.99)];
  61. var Fire2OVL = [stretch((2.1 * B04 + 0.5 * B12), 0.01, 0.99) + 1.1, stretch((2.2 * B03 + 0.5 * B08), 0.01, 0.99) + 0.25, stretch(2.1 * B02, 0.01, 0.99)];
  62. var Fire3OVL = [stretch((2.1 * B04 + 0.5 * B12), 0.01, 0.99) + 1.1, stretch((2.2 * B03 + 0.5 * B08), 0.01, 0.99) + 0.5, stretch(2.1 * B02, 0.01, 0.99)];
  63. // Band combinations (To get quicker processing you should comment out all those you are not using in the Settings further down)
  64. var NaturalColors = [2.9 * B04, 3.1 * B03, 3.0 * B02];
  65. // var EnhancedNaturalColors = [2.8 * B04 + 0.1 * B05, 2.8 * B03 + 0.15 * B08, 2.8 * B02];
  66. // var NaturalNIRSWIRMix = [2.1 * B04 + 0.5 * B12, 2.2 * B03 + 0.5 * B08, 3.0 * B02];
  67. // var NIRSWIRColors1 = [2.6 * B12, 1.9 * B08, 2.7 * B02];
  68. var NIRSWIRColors2 = [2.4 * B12, 1.7 * B8A, 2.2 * B05];
  69. // var NIRSWIRColors3 = [0.5 * (B12 + B11) / 4 / B07, 0.8 * B8A, 1 * B07];
  70. // var NIRSWIRColors4 = [2.0 * B12, 1.1 * B11, 1.6 * B08];
  71. // var FalseColor = [B08 * 2, B04 * 2, B03 * 2];
  72. // var NatFalseColor = [B12 * 2.6, B11 * 2, B04 * 2.7];
  73. // var Vegetation = [B11 * 2.4, B8A * 2, B04 * 2.9];
  74. // var PanBand = [B08, B08, B08];
  75. // var NBR8A12 = indexMap((B8A - B12) / (B8A + B12), -0.8, -0.4, 0.0, 1, 1, 1);
  76. // var NDVI = indexMap((B08 - B04) / (B08 + B04), -0.4, -0.2, 0.0, 1, 1, 1);
  77. // Settings
  78. // Fire (hot spot) visualization
  79. var fire1 = Fire1OVL;
  80. var fire2 = Fire2OVL;
  81. var fire3 = Fire3OVL;
  82. // Used band combinations and mixing
  83. var layer1 = NIRSWIRColors2;
  84. var layer2 = NaturalColors;
  85. var layer1Amount = 0;
  86. var layer2Amount = 100;
  87. // Influence contrast and saturation
  88. var stretchMin = 0.00;
  89. var stretchMax = 1.00;
  90. var saturation = 1.00;
  91. // Fire sensitivity (Default = 1.00), higher values increase fire (hot spot) detection and false positives
  92. var fireSensitivity = 1.00;
  93. // Burn scar visualization
  94. var burnscarHighlight = 0.00;
  95. var burnscarThresholdLow = -0.25;
  96. var burnscarThresholdHigh = -0.38;
  97. var burnscarDesaturateBackdrop = 0.25;
  98. var burnscarDarkenBackdrop = 0.25;
  99. // Manually influence RGB output
  100. var manualCorrection = [0.00, 0.00, 0.00];
  101. // Image generation and output
  102. noFire = blend(layer1, layer2, layer1Amount, layer2Amount);
  103. finalRGB = applyEnh(noFire).map(function(num, index) {
  104.     return num + manualCorrection[index];});
  105. return (a(B12, B11) > (1.0 / fireSensitivity)) ?
  106.     (a(B12, B11) > (2.0 / fireSensitivity)) ? fire3 :
  107.     (a(B12, B11) > (1.5 / fireSensitivity)) ? fire2 : fire1 :
  108.    finalRGB;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top