sentinel-hub/custom-scripts

help implementing an Idea: custom code for NDVI Change Ratio to Previous Month

mkganyago opened this issue · 4 comments

Hi

I am looking for help implementing a custom for NDVI Change Ratio to Previous Month.
Change_ratio

Thanks.

Dear Mahlatse,

please find the attached NDVI Change Ratio scripts and EO Browser links below. I'm sending you the script in 3 different visualizations, tested on Nile river agriculture, as you will see below.

I took a multitemporal NDVI script, made by Harel Dan, and modified it for your needs. The script first gets the month of the most recent acquisition available and then loops through all the acquisitions and checks if each of them was acquired in the same month. Then you have an else statement, where all the other acquisitions fall. To make this work, the filterScenes function at the end is set to 2 months. This means the script should take the first and second month from the Until date you choose (either in EO Browser timespan or in your request body).
Finally, the NDVI Change Ratio is calculated from the average NDVI of the first month (avg1) and average NDVI of the second month (avg2) as: NDVI Change Ratio = (avg1-avg2) / avg2.

Grayscale visualization

The visualization in this script is grayscale, which makes it possible to see how the index looks like without visualization manipulation. The lighter the pixel, the higher the value of the NDVI Change ratio.

Please find the script in EO Browser and in the NDVI Change Ratio, Grayscale.txt file.

2020-10-17 14_50_26-Sentinel Hub EO Browser

Visualization from your Table 1. Legend

I implemented the visualization with the same borders and colors as in Table 1. Legend from your question. However, the result does not look good. I think the values in the table are not ok, as it seems that most values of the index are between 0 and 5, which falls into one single category in the legend (No change), hence, the result is mostly yellow. This might be ok, if the vegetation really would not change at all, but I checked the results in Ljubljana as well, and I'm confident there should be quite a lot of vegetation changes between July and September, and the script returned all yellow there as well. So I believe this is probably not a good way to visualize this index.

Please find the script in EO Browser and in the NDVI Change Ratio, suggested visualization.txt file.

2020-10-17 14_52_22-Sentinel Hub EO Browser

New visualization between 0 and 5

My suggestion would be to instead make a visualization with more differentiation between 0 and 5. I prepared an example visualization with the borders 0,0.5,1,3 and 5, and colors going from black to blue, then green and finally red for the highest values.

Please find the script in EO Browser and in the NDVI Change Ratio, new visualization 0-5.txt file.

2020-10-17 14_53_26-Sentinel Hub EO Browser


I wish you all the best,
Monja

Hi Monja,

Thank you very much for your timely response and implementation of this idea. I highly appreciate it.

I noted that when switching between dates (months) in EO browser, the following error occurs. I am not sure what is the cause.
//----------------------------------------------------------------------------------
An error has occurred while fetching images:

Failed to evaluate script!
evalscript.js:19: TypeError: Cannot read property 'date' of undefined
var endMonth = scenes[0].date.getMonth(); //get month of the most recent scene
^
TypeError: Cannot read property 'date' of undefined
at evaluatePixel (evalscript.js:19:27)
at executeForMultipleScenes (:988:14)
//---------------------------------------------------------------------------------
I think the blank yellow image on the second code, i.e., suggested visualisation.txt, required multiplication by 100 to work. Otherwise, I managed to make the suggested visualisation to work by adapting the third code, i.e., new visualization 0-5.txt, as follows:
//---------------------------------------------------------------------------------
let NDVI_change_ratio = ((avg1-avg2)/(avg2))*100

return colorBlend (NDVI_change_ratio, [-25,-15,-5,0,5,15,25], [
[204,41,0], //values <= -25, red,Significant decrease
[179,89,0], //values <= -15, brown, Moderate decrease
[255,153,0], //values <= -5, orange, Low decrease
[255,255,0], //values = 0, yellow, No change
[153,255,153], //values >=5, lime green, Low increase
[0,204,0], //values >=15, light green, Moderate increase
[0,128,0]//values >=25, Green, Significant increase
]);
}
//---------------------------------------------------------------------------------
Regards,
Mahlatse

Dear Mahlatse,

I missed your answer, sorry for a late response. Technically this should work in EO Browser with a single date, as you have it all specified in filterScenes, but for some reason it doesn't work, when you change the date. It's important to have a timespan turned on and to select before-to months so they cover at least 2 months. If you change the Until date, you should also change the From date. I changed it to February 6 to April 6 and it worked. So set a timespan, and it should work (I hope).

2020-11-03 09_46_03-Sentinel Hub EO Browser

Oh yes, it does work better if I multiply it by 100. Cool.

Best,
Monja

Hi Monja,

Thanks a lot.