NDVI with discrete colors in EO Browser

A few days ago somebody asked me whether I could help him with a script that would create NDVI images from Landsat 8 data in the Sinergise EO Browser.
The EO Browser already allows to automatically display the NDVI, but to get an overview of a specific situation and to present the data, a view with three discrete colors would be easier to understand.
I came up with this little script, maybe it is useful to somebody else as well. Feel free to use and change it as you please. You can include other indices and define the thresholds, when to use which color.
Adaptation for Sentinel-2 should be easy, just change the bands used in the script.
The Script
// *** // Create an index with three discrete color classifications (HIGH-MEDIUM-LOW view) // Landsat 8 version, you need to change bands for use with Sentinel-2 // For use in Sinergise EO Browser (http://apps.sentinel-hub.com/eo-browser) // Pierre Markuse (@pierre_markuse) // *** // Functions function stretch(val, min, max) { return (val - min) / (max - min); } // Some basic colors var BLACK = [0.0, 0.0, 0.0]; var WHITE = [1.0, 1.0, 1.0]; var RED = [0.9, 0.1, 0.1]; var YELLOW = [0.9, 0.9, 0.1]; var GREEN = [0.1, 0.9, 0.1]; var GRAY50 = [0.5, 0.5, 0.5]; var GRAY20 = [0.2, 0.2, 0.2]; var CBFhigh = [0.11, 0.62, 0.46]; var CBFmedium = [0.85, 0.37, 0.01]; var CBFlow = [0.46, 0.44, 0.70]; // Index // You can easily add whatever you need // Made for Landsat 8, change bands for Sentinel 2 var NDVI = (B05-B04)/(B05+B04); var GNDVI = (B05-B03)/(B05+B03); var RBNDVI = (B05-(B04+B02))/(B05+(B04+B02)); // Sentinel-2 Example // var NDVI = (B08-B04)/(B08+B04); // Used Index (Pick from above list) var USEDINDEXvalue = NDVI; // Band combinations // Made for Landsat 8, you might need to change things for Sentinel-2, depending on what you are going to use var NaturalColors = [stretch(4 * B04, 0.1,1), stretch(4 * B03, 0.1,1), stretch(3.2 * B02, 0.1,1)]; var EnhancedNaturalColors = [stretch((3.3 * B04 + 0.1 * B07) , 0.01,1), stretch((3 * B03), 0.01,1), stretch(2.8 * B02, 0.01,1)]; var NIRSWIRColors = [stretch(4.6 * B07, 0.05, 0.9), stretch(1.6 * B05, 0.05, 0.9), stretch(3.7 * B04, 0.05, 0.9)]; var NaturalNIRSWIRMix =[stretch(3.6 * B07 + 2*B04, 0.05, 0.9), stretch(0.6 * B05 + 2 * B03, 0.05, 0.9), stretch(2.2 * B04 + 2 * B02, 0.05, 0.9)]; var INDEXmono = [stretch(USEDINDEXvalue, 0.5, 0.99), stretch(USEDINDEXvalue, 0.5, 0.99), stretch(USEDINDEXvalue, 0.5, 0.99)]; var INDEXhigh= [(stretch(USEDINDEXvalue, 0.1, 0.99) * 0.4), (stretch(USEDINDEXvalue, 0.1, 0.99) * 1), (stretch(USEDINDEXvalue, 0.1, 0.99) * 0.4)]; var INDEXlow= [stretch(USEDINDEXvalue, 0.1, 0.99) * 2, stretch(USEDINDEXvalue, 0.1, 0.99)*0.4, stretch(USEDINDEXvalue, 0.1, 0.99) * 0.4]; var INDEXmedium = [stretch(USEDINDEXvalue, 0.1, 0.99) * 1.3, stretch(USEDINDEXvalue, 0.1, 0.99) * 1.3, stretch(USEDINDEXvalue, 0.1, 0.99) * 0.3]; // Styles // GREEN, YELLOW, and RED will yield a crisp result // Try INDEXhigh, INDEXmedium, and INDEXlow for a softer result // Using CBFhigh, CBFmedium, and CBFlow will yield a color-blind friendly result var HIGH = GREEN; var MEDIUM = YELLOW; var LOW = RED; // Thresholds for HIGH/MEDIUM/LOW classification // Change to whatever suits your needs var THRESHOLDmedium = 0.52; // Values above this will be rendered using MEDIUM style, below LOW style is used var THRESHOLDhigh = 0.62; // Values above this will be rendered using HIGH style // Classification of values into HIGH, MEDIUM, and LOW return (USEDINDEXvalue > THRESHOLDmedium) ? (USEDINDEXvalue > THRESHOLDhigh) ? HIGH : MEDIUM : LOW;
The script as it is produces images like the one you can see at the beginning of this post, showing the NDVI in red, yellow, and green, in ranges that can be specified in the script. Apart from this view the script can generate the following views:
More examples
The same classification, using color-blind friendly colors.
A NIR/SWIR view, set all styles to NIRSWIRcolors.
An enhanced natural color view, set all styles to EnhancedNaturalColors.
And finally, a shaded NDVI view, making it look a little softer than just three discrete colors.
Landsat 8 data courtesy of U.S. Geological Survey.