Raster Calculations and Raster Palettes | NDVI Calculator

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2021.2


In this article, we will be calculating the Normalized Difference Vegetation Index (NDVI) . In the process, we will learn how to build conditions in the RasterExpressionEvaluator as well as 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 being 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.

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 Files 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


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

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

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. 

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. 
In the RasterExpressionEvaluator, set the Interpretation to Auto, then set the following for the Expression. 


Which is the NDVI calculation: 
3. Run and Inspect Results
Add an Inspector to the Result output port on the RasterExpressionEvaluator, then run the workspace. It is best to not use Feature Caching when working with rasters as it drastically slows down the workspace. View the results in Visual Preview. 
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 in the following expression:


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. 

An alternative to using RasterExpressionEvaluator here is to use the RasterCellValueReplacer
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 on 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 are defining the interpretation of the palette as RGB24 and are assigning each of the integers from 0 - 3 an RGB color.

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


The colors above are, navy blue, yellow, bright green and dark green but you can set the RGB triads to what you think is appropriate. 
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. 
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. 
This particular image is of the City of Vancouver and the surrounding area. The dark blue is ocean or other bodies of water, the bright yellow patch in the upper left corner are clouds from our previous image, but yellow also indicates no or limited vegetation. The light green is minimal vegetation, particularly in the lower-left quadrant which would be the city itself, which is a rather green city in terms of trees. Then the far right side of the image is farmland and forests which are shown 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?



Please sign in to leave a comment.