Raster Calculations and Raster Palettes | NDVI Calculator

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2021.2

Introduction

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

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, 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. 
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 to not 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 in 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 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.

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 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. 
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 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?

Comments

0 comments

Please sign in to leave a comment.