FME Version
Files
Introduction
The RasterExpressionEvaluator is a transformer that evaluates expressions on each cell in a raster, such as algebraic operations or conditional statements. This tutorial focuses on how to construct conditional statements for this transformer, which use the format:
if (conditions, pass value, fail value)
In other words, “if the cell value fulfills the specified conditions, set it to the pass value, otherwise set it to the fail value”. In this tutorial, the rasters we are working with are .png files in the RGB color space, so we will specify the pass value as 200 and the fail value as 255:
if (conditions, 200, 255)
This means cells that meet our specified conditions will be set to grey (200), and cells that fail will be set to white (255).
To learn more about raster expression syntax in FME, refer to the RasterExpressionEvaluator documentation.
Step-by-Step Instructions
In this example, we will read a PNG source file and write out another PNG that shows the cells we are interested in. In Part 1, we want to create a condition that identifies all green cells, i.e. the ones representing parks, and in Part 2, we want to identify roads. The input raster is a map that looks like this:
Part 1: Extract Parks using Color Classification
In this first part, we are interested in the cells representing parks. We will use the RasterExpressionEvaluator to classify the input raster according to a condition (cells that are green). Follow the below steps to create the workspace from scratch, or open the attached GreenFilter.fmwt template to view the completed workspace.
1. Generate a workspace to read and write the raster
Click File > Generate Workspace and create a workspace with the following parameters:
Reader Format: PNG (Portable Network Graphics)
Reader Dataset: C:\<Tutorial download>\map.png
Writer Format: PNG (Portable Network Graphics)
Writer Dataset: C:\<Tutorial download>\output
Workflow Options: Static Schema
This will generate a workspace that reads map.png and writes a new raster in a folder called “output”. You can optionally double-click the writer feature type and give it a custom name, like “GreenFiltered”.
2. Add a RasterExpressionEvaluator
Click anywhere on the canvas and begin typing “RasterExpressionEvaluator”. Connect this transformer between the reader and writer feature types.
Double-click the transformer to open the parameters. Create the following Band Expression:
Interpretation: Red8
Expression: if (A[1]-A[0]>5 && A[1]-A[2]>5, 200, 255)
This expression sets cell values to 200 (grey) when they meet the condition, and the rest are assigned a value of 255 (white).
The condition “A[1]-A[0]>5 && A[1]-A[2]>5” tests if the cell is more green than red (A[1]-A[0] is greater than 5), and more green than blue (A[1]-A[2] is greater than 5).
Note if we simply compared green with red and blue as "if (A[1]>A[0] && A[1]>A[2], 200, 255)", this would also select unwanted grey cells. In this case, the green values must be significantly more than the red and blue in order for the cell to be truly green.
For more syntax examples, refer to the transformer’s documentation.
3. Run the workspace
Run the workspace and view the output. You will see the following raster:
Part 2: Extract Roads
In this second part, we are interested in the cells representing roads, which is a little more complex because roads can be white or yellow, and they have black edges and labels. Again, we will use the RasterExpressionEvaluator to classify the input raster according to a condition and output a new raster that shows the result in grey and white.
Follow the below steps to modify the existing workspace built in Part 1, or open the attached RoadsFilter.fmwt template to view the completed workspace.
1. Modify the RasterExpressionEvaluator parameters
In the existing workspace you built in Part 1, double-click the RasterExpressionEvaluator to open the parameters. Modify the Band Expression to the following:
Interpretation: Red8
Expression: if ((abs(A[0]-A[1])<=3 && abs(A[0]-A[2])<=3) || (abs(A[0]-A[1])<=4 && abs(A[0]-A[2])>10) , 200, 255)
This expression sets cell values to 200 (grey) when they meet the condition, and the rest are assigned a value of 255 (white).
The condition tests if all bands have similar values (white/grey/black), which is used for roads and their edges and labels. It also tests for yellow roads.
2. Run the workspace
Run the workspace and view the output. You will see the following raster:
Comments
0 comments
Please sign in to leave a comment.