NDVI images with custom interval and color pallete

Is it possible to retrieve NDVI images with 0.05 intervals between index values ​​and with a custom color palette?

Good morning Tetri,

Yes, this is very easy to do using the custom scripts functionality:

Below is an example of a script for use with Sentinel 2 L1C data. The intervals are set at 0.5 and the color palette is set in RGB format using values between 0 and 1. For example 0,0,0 would be black and 1,1,1 would be white.

//VERSION=3
function setup() {
return {
input: [{
bands:[“B04”, “B08”],
}],
output: {
id: “default”,
bands: 3,
}
}
}
function evaluatePixel(sample) {
let ndvi = (sample.B08 - sample.B04) / (sample.B08 + sample.B04)

if (ndvi<-0) return [0.05,0.05,0.05]
else if (ndvi<0.05) return [0.75,0.75,0.75]
else if (ndvi<0.1) return [0.86,0.86,0.86]
else if (ndvi<0.15) return [0.92,0.92,0.92]
else if (ndvi<0.2) return [1,0.98,0.8]
else if (ndvi<0.25) return [0.93,0.91,0.71]
else if (ndvi<0.3) return [0.87,0.85,0.61]
else if (ndvi<0.35) return [0.8,0.78,0.51]
else if (ndvi<0.4) return [0.74,0.72,0.42]
else if (ndvi<0.45) return [0.69,0.76,0.38]
else if (ndvi<0.5) return [0.64,0.8,0.35]
else if (ndvi<0.55) return [0.57,0.75,0.32]
else if (ndvi<0.6) return [0.5,0.7,0.28]
else if (ndvi<0.65) return [0.44,0.64,0.25]
else if (ndvi<0.7) return [0.38,0.59,0.21]
else if (ndvi<0.75) return [0.31,0.54,0.18]
else if (ndvi<0.8) return [0.25,0.49,0.14]
else if (ndvi<0.85) return [0.19,0.43,0.11]
else if (ndvi<0.9) return [0.13,0.38,0.07]
else if (ndvi<0.95) return [0.06,0.33,0.04]
else return [0,0.27,0]

}

This example uses a yellow to green color gradient but if you want to create your own custom palette then using the ColorBrewer website is a good starting point:

Using the tool, you can pick a suitable color palette and specify the number of classes too. To convert the RGB values (0 to 255) to something friendly for the above custom script, just divide the numbers by 255. For example, 255, 51, 0 would convert into 1, 0.2, 0.

Let me know if you need any more clarification on this, happy to help!

Will

1 Like

Perfect. And can I make this request through the API through my C# code?

Hi Tetri,

I’m just trying to find out more from colleagues. Can you clarify what your workflow is / would be please?

Thanks

Will

The idea is to request the NDVI images of polygons formed by the farms that I have registered in my system.

I found examples of requesting images in the link https://docs.sentinel-hub.com/api/latest/data/sentinel-2-l1c/examples/ and implemented the call in my code, but I was not successful in recovering the images.

Hi Tetri,

Do you want to share the curl request you have written and I can try and help you to debug it?

Will

When testing the link requests that I passed above, using Postman, the answer in all of them is the error:

{
    "error": {
        "status": 500,
        "reason": "Internal Server Error",
        "message": "MessageBodyReader not found for media type=application/octet-stream, type=class io.jsonwebtoken.impl.DefaultClaims, genericType=class io.jsonwebtoken.impl.DefaultClaims.",
        "code": "RENDERER_EXCEPTION"
    }
}

Is it something I’m doing wrong?

Hi Tetri,

It looks like you need to configure the Authentication tab in Postman. You will need to create a client ID and client secret on the Sentinel Hub Dashboard and you can use these to obtain a token in Postman.

Once that is done, you can follow the below instructions:

In the Postman request Authorization tab set the Type to OAuth 2.0 and Add the authorization data to Request Headers . Then click the Get New Access Token button. Set the Grant Type to Client Credentials , the access token URL to the token endpoint, then set the Client ID and Client Secret to the values of your OAuth Client. Scope can be blank. Keep Client Authentication as Send As Basic Auth Header . Click Request Token . You should get a new one immediately. To use this token to authorize your request, click Use Token . For more information see the Postman authorization documentation

All the information you need can be found here: https://docs.sentinel-hub.com/api/latest/api/overview/authentication/

Let me know if you run into any problems :slight_smile:

Will

1 Like

I got the access token from your instructions.
But the error 500 in the request continues to be displayed.

Hi Tetri,

Are you able to share the curl request with me? (make sure to redact anything sensitive)

I’m pretty sure it will just be an error in your syntax that is causing the error.

Will