How to get NDVI image overlay only for the box of drawer polygon without background color error

Hello everyone,
I am developing a simple web application that I would be able to extract NDVI values for my area of interest. and i have this background color error
here’s script of my code:

"
//VERSION=3
//This script was converted from v1 to v3 using the converter API

//NDVI EVALSCRIPT
if (dataMask == 0) return [0,0,0,0];

//ndvi
var ndvi = (B08-B04)/(B08+B04);

return colorBlend(ndvi,
[-0.2, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 ],
[[0, 0, 0], // < -.2 = #000000 (black)
[165/255,0,38/255], // → 0 = #a50026
[215/255,48/255,39/255], // → .1 = #d73027
[244/255,109/255,67/255], // → .2 = #f46d43
[253/255,174/255,97/255], // → .3 = #fdae61
[254/255,224/255,139/255], // → .4 = #fee08b
[255/255,255/255,191/255], // → .5 = #ffffbf
[217/255,239/255,139/255], // → .6 = #d9ef8b
[166/255,217/255,106/255], // → .7 = #a6d96a
[102/255,189/255,99/255], // → .8 = #66bd63
[26/255,152/255,80/255], // → .9 = #1a9850
[0,104/255,55/255] // → 1.0 = #006837
]);
"

Hey.

You need to set two things

  • the transparency for the pixels that are outside of the provided geometry, which you already do with setting the fourth element to 0 in line if (dataMask == 0) return [0,0,0,0];
  • set the format to PNG in the request parameters, so that those transparent pixels will be interpreted as transparent and won’t be changed to black in JPEG format (JPEG format doesn’t support transparency)
    • this needs to be set in any case, if you are downloading the image, showing the image with <img> tag or if you use leaflet or other library for interactive maps
    • SH Process API:
    • SH WMS:

Hope this helps.
Cheers

@z.cern thank you very much it really helps me :pray: :pray: :pray: :pray: :pray:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.