Raster Calculations and Raster Palettes | NDVI Calculator

Liz Sanderson
Liz Sanderson
  • Updated

Introduction

In this article, we will calculate the Normalized Difference Vegetation Index (NDVI). In the process, we will learn how to build conditions in the RasterExpressionEvaluator and how to create rasters with palettes. NDVI is based on the ability of plants to consume visible light radiation for photosynthesis (mostly in blue and red parts of the spectrum). This is why we see our plants mostly green; this color is mostly reflected. Near-infrared light, on the other hand, cannot cause the proper reaction in cells, and hence, is reflected (otherwise it will overheat the plants). The more infrared light is reflected, and the more red light is consumed, the healthier the plant is.

Downloads

Click on the links below to download the dataset and workspace from Amazon S3. 

Step-by-step Instructions

1. Add a JPEG 2000 Reader

Open FME Workbench and start a blank workspace. Add a JPEG 2000 reader to the canvas and browse to the Sentinel-2 data that we used in the previous tutorial. You can also download the data from the Downloads section of this article. Add the Red and NIR Bands (B04 and B08). Then open the Parameters. 

  • T10UEV_20210828T190909_B04.jp2
  • T10UEV_20210828T190909_B08.jp2

JPEGReader.png

In the parameters, change the Feature Type Name(s) to From File Name(s), then click OK twice. 

JpegParams.png

In the Select Feature Type dialog, confirm that both B04 and B08 are selected, and then click OK to complete the reader addition. 

2. Create NDVI Expression

Add a RasterExpressionEvaluator to the canvas, then, before connecting it, open the parameters and change the Mode to Two Rasters. This will change the number of input ports available. 

RasterMode.png

Now that we have two input ports, we can connect the reader feature types. Connect the Red Band (B04) to the A input port on the RasterExpressionEvaluator, then connect the NIR Band (B08) to the B input port. 

NDVIConnections.png

In the RasterExpressionEvaluator, set the Interpretation to Auto, then set the following for the Expression. 

(B[0]-A[0])/(B[0]+A[0])

Which is the NDVI calculation: 

NDVIEquation.png

NDVIParams.png

3. Run and Inspect Results

Add an Inspector to the Result output port on the RasterExpressionEvaluator, then run the workspace. It is best not to use Feature Caching when working with rasters, as it drastically slows down the workspace. View the results in Visual Preview. 

NDVIVP.png

Notice that the band ranges of the resulting pixels fall between -1 and 1, and our interpretation has changed to REAL64. With these values, we can now recolor the raster to easily interpret the information. 

4. Create Raster IF Statement

Add another RasterExpressionEvaluator to the canvas and connect it to the Result output port on the previous one. In the parameters, set the Interpretation to UINT8 and then copy and paste the following expression:

if(A[0]<=0,0,if(A[0]<=0.3,1,if(A[0]<=0.6,2,3)))

This expression is setting the value of our pixels. So if the pixel value is:

  • Equal to or less than 0, set the value to 0
  • Greater than 0 and is less than or equal to 0.3, set the value to 1
  • Greater than 0.3, and if the value is less than or equal to 0.6, set the value to 2
  • Greater than 0.6, set the value to 3

SetValueExpression.png

An alternative to using RasterExpressionEvaluator here is to use the RasterCellValueReplacer

dialog.png

5. Color Values

Now that we only have four-pixel values (0 - 3), we can easily color them. Add an AttributeCreator to the canvas, then connect it to the second RasterExpressionEvaluator Result output port. In the parameters, create a new attribute called _palette. Then, for Value, open the Text Editor by clicking the ellipsis. In the Text Editor, paste in the following text: 6).

Add an AttributeCreator and use the text editor to create a _palette attribute as is shown below. Here, we define the interpretation of the palette as RGB24 and assign each of the integers from 0 to 3 an RGB color.

RGB24
0 0,0,64
1 255,255,0
2 0,255,0
3 0,127,0

AttributeCreator.png

The colors above are navy blue, yellow, bright green, and dark green, but you can adjust the RGB triads to what you think is most suitable. 

6. Add Palette

With the palette values defined, we need to add them to the raster. Add a RasterPaletteAdder to the canvas and connect it to the AttributeCreator. In the parameters, set the Raster Palette to the _palette attribute we just created. 

RasterPaletteAdder.png

7. Run Workspace and Inspect Result

Move the Inspector to the output port of the RasterPaletteAdder, then run the workspace. View the output in Visual Preview. 

NDVI.png

This particular image is from the City of Vancouver and its surrounding area. The dark blue represents the ocean or other bodies of water. The bright yellow patch in the upper left corner is clouds from our previous image, but yellow also indicates no or limited vegetation. The light green represents minimal vegetation, particularly in the lower-left quadrant, which corresponds to the city itself, a rather green city in terms of its tree cover. Then, the far right side of the image is dominated by farmland and forests, which are depicted in dark green. 

To learn more about interpreting an NDVI image, see the Wikipedia page or the GIS Geography page for more information. 

Was this article helpful?

We're sorry to hear that.

Please tell us why.

As of January 14th, 2026, comments on knowledge base articles have been closed. To make sure questions don’t get missed and to enable more community support, we’ve moved discussions to the FME Community. If you have a question or a comment about this article, please create a new post or create a support ticket.