Return NDIV value between -1 and 1

Hi I´m trying to get the NDVI value for [-75.6396578867764, -14.197876401443217, -75.64298408105815, -14.20062882414364]

I already checked on the FAQ https://www.sentinel-hub.com/faq/how-do-i-get-actual-ndvi-values and changed my NDVI layer as seen in this picture:

image

This is my code:

from sentinelhub import SHConfig, WmsRequest, WcsRequest, MimeType, CRS, BBox

INSTANCE_ID = 'XXXX'

if INSTANCE_ID:
config = SHConfig()
config.instance_id = INSTANCE_ID
else:
config = None

box = [-75.6396578867764, -14.197876401443217, -75.64298408105815, -14.20062882414364]
box = BBox(box=box, crs=CRS.WGS84)

NDVI_request = WmsRequest(
layer='NDVI',
bbox=box,
time='2019-10-13',
width=100,
config=config
)
wms_true_color_img = NDVI_request.get_data()

output:
[array([[ 92, 92, 95, …, 16, 16, 16],
[ 83, 83, 94, …, 16, 16, 16],
[ 83, 83, 94, …, 16, 16, 16],
…,
[130, 130, 131, …, 126, 126, 123],
[130, 130, 131, …, 126, 126, 123],
[130, 130, 131, …, 126, 126, 123]], dtype=uint8)]

Hi @adrian135torrejon,

A clue to the answer is in your output array: your data is being returned as a numeric type uint8, which is an unsigned integer of length 8 bits.

Since you are looking for NDVI values between -1 and 1 that are decimal, you want to return the data in a float format. You can do that by just adding a single line in your code, as shown in the WmsRequest parameters in the documentation.

If you change your code to the following (you have already imported MimeType), you should retrieve the NDVI values in the format you want:

NDVI_request = WmsRequest(
layer='NDVI',
bbox=box,
time='2019-10-13',
width=100,
image_format = MimeType.TIFF_d32f,
config=config
)

Hope this helps,

Maxim

1 Like

Totally worked, ¡thanks a lot!

Just one question, what’s te difference between the current NDVI calculation I’m using and the NDVI Index you offer as a product:

/VERSION=3 (auto-converted from 1)

let viz = new Identity();

function evaluatePixel(samples) {
    let val = index(samples.B08, samples.B04);
    return viz.process(val);
}

function setup() {
  return {
    input: [{
      bands: [
                  "B04",
          "B08"
      ]
    }],
    output: { bands: 1 }  }
}

Thank again!

Hi @adrian135torrejon,

for you there is no difference.
The index function uses the same formula you are using in your request [index(A,B) = (A-B)/(A+B)]. The other part is “only” needed for the visualization within EO Browser (evaluatePixel function) and for accessing the bands (function setup part) a v3 evalscript is used. You use the basic version of evalscripts, which is also perfectly fine. If you want to read more on evalscripts take a look here. If you want to use v3 scripts make sure they start with “//VERSION=3”.

Best,
Daniel

1 Like