RasterExpressionEvaluator
Evaluates expressions on each cell in a raster or pair of rasters, including algebraic operations and conditional statements.
Typical Uses
- Performing calculations on rasters or pairs of rasters
- Manipulating images
- Combining rasters
- Classifying rasters
How does it work?
The RasterExpressionEvaluator receives raster features and evaluates defined expressions using the cell values, then outputs new raster features with the calculated values.
Input Mode - One or Two Rasters
The transformer has two modes - One Raster and Two Rasters.
One Raster mode provides a single input port -the A port. In this mode, expressions will be evaluated wholly within a single raster.
Two Rasters mode provides two input ports - A and B. In this mode, expressions can use cell values from two rasters of the same dimensions (same number of rows and columns). Position information for georeferenced rasters is not taken into consideration. The number of input raster features may be:
- If One Raster (A) - one or more A raster, no B rasters.
- If Two Rasters (A and B) - one A raster, one or more B rasters. The single A input will be paired with each B input.
All bands used in the same expression must have the same nodata value, or have no nodata value at all. No band may have a palette.
In Two Raster mode, if an expression references both the A and B input raster features, the output raster will have all the attributes from both feature A and B. If the same attribute exists on both input features, then the attribute value from feature B will be preferred.
When only a single input is being operated on (for example, in One Raster mode or if the expression only references input A), the feature attributes and raster properties will remain unchanged.
Expressions
Expressions are defined in the Band Expression(s) table. Each line in the table corresponds to one band in the output raster, in sequential order - that is, the first line produces Band 0, second line produces Band 1, and so forth. The Interpretation Type for each output band is also specified here.
Expressions may be as simple a constant value or simple arithmetic operation. The Arithmetic Editor - Raster Expression is available via the Expression ellipsis (...) button to assist in the construction of expressions.
Expressions are case-sensitive.
Note that when converting between different data types, a Bounded Cast is used. As a result, when a calculated value does not fit in the specified destination interpretation, the corresponding destination value will either be set to the minimum or maximum value possible in the destination data type. For example, a value of 300, if converted to an unsigned 8-bit integer band, would become 255 (the maximum available value for that interpretation type).
Nodata and Zero (0) Values: Expression evaluation often involves changing interpretation types of values, and can sometimes result in unpredictable behavior particularly in the case of zero (0). When defining your expression, best practice is to specify zero in decimal form as 0.0 to avoid it being misinterpreted as nodata.
Expression Syntax
|
Means... |
---|---|
|| | Or |
&& | And |
== | Equal |
!= | Not equal |
< | Less than |
> | Greater than |
A[x] | Band x of raster feature A |
B[x] | Band x of raster feature B |
Bands start counting at zero (so a three-band raster is A[0], A[1], and A[2]).
A statement can be as simple as these:
255 | Set the value of the current cell to be 255. |
A[0]+1 | Set the value of the current cell to be Raster A, band zero, plus 1. |
"if" statements allow a value to be set depending on conditions, in the form if (conditions, pass value, fail value):
if (A[0] < 255, 200, 255) | If Raster A, band zero is less than 255, then set the current cell to 200, else leave it as 255. |
if (A[0]<50 || A[1]<50 || A[2]<50, 0, 255) | If Raster A, band zero is less than 50 OR band one is less than 50 OR band two is less than 50, then set the current cell to 0, else set it to 255. |
Bands in two raster features can be referenced in the statement:
if (A[0]!=B[0] && A[0]<255, 0, A[0]) | If Raster A band zero is not equal to Raster B band zero, AND Raster A band zero is less than 255, then set the current cell to 0, else set it to the value of Raster A band zero. |
Standard maths operators are permitted:
if (A[0] - B[0] == 255, 0, A[0]/5) | If Raster A band zero minus Raster B band zero equals 255, then set the current cell to 0, else set it to the value of Raster A band zero divided by five. |
Attributes can be referenced using @Value and either A: or B: to refer to the attribute name:
A[0]*@Value(A:myattr) | Multiply Raster A band zero by the attribute myattr on raster A. |
Examples
Expression |
Result |
---|---|
255-A[0] | Inverts an 8-bit band. For a 24-bit RGB image, repeat for A[1] and A[2] to invert the full image. Repeat for A[3] too if it is a 32-bit RGBA image. |
if ((A[0]+A[1]+A[2])==0, 255, A[0]) | Convert black values into white in an RGB image. Repeat for A[1] and A[2] to invert black/white for the full image. |
A[0]*1.5 | Increases the brightness of the red band in an RGB image. Repeat for A[1] and A[2] to brighten the full image |
@sqrt((A[0]*A[0]) + (B[0]*B[0])) | Combines two raster cells together. An alternative would be to average the values: (A[0]+B[0])/2) |
if(A[0]==0,0,A[0]/@Value(A:_height+1)) | Classify raster DEM cells into groups where group size is defined by the _height attribute |
In this example, we will convert a color raster to grayscale. Note that the original raster has three bands - Red, Green and Blue. Values from all bands will be used to calculate a single output value.
The values for each band on each cell will be used for calculations.
The raster is routed into a RasterExpressionEvaluator.
In the parameters dialog, we define a single expression to be calculated and stored in one single Gray band. The Interpretation is set to Gray8, and the Expression references cell values from all three incoming bands. (This formula uses commonly recommended values for color to gray scale conversion.)
Band numbering starts at zero (0), and so A[0] refers to the first band (Red), A[1] refers to the second band (Green), and A[2] refers to the third band (Blue).
The output raster feature contains only one band, the Gray8 band specified in the parameters.
The results of the expression evaluation are output as cell values.
In this example, we will use a RasterExpressionEvaluator to isolate land of a specific zoning type that falls with 150 meters of major arteries.
We start with two numeric rasters. The first represents zoning, with integer values ranging from 1 to 10, where each value arbitrarily represents a zone type. This will be our “A” raster.
We are interested in zone type 3.
The second raster represents 150 meter buffers along arterial roads. Cells have a value of either 0 or 1, where 1 represents pixels inside the buffer zones, and 0 outside the buffer zones. This is our “B” raster.
The pair of rasters is routed into a RasterExpressionEvaluator - note that Mode: Two Rasters must be selected in the parameters dialog prior to connecting the second raster, to enable (and expose) the B input port.
In the parameters dialog, we create a single expression. This will result in a single-band numeric raster being output. An Interpretation type must be specified - Int32 (32-bit integer) is chosen, as an appropriate interpretation type for these values.
The expression references cells from both the A and B rasters, and is using this form:
if (conditions, pass value, fail value)
If the cell value from raster A (zones) is equal to 3, AND the cell value from raster B (artery buffers) is equal to 1, then output the cell value 3. Otherwise, set the cell value to 0.
Note that zero is specified in decimal form as 0.0 (avoiding misinterpretation as nodata).
Any cells that fall within both zone 3 and an artery buffer are output with a value of 3, and all others cells are 0.
Usage Notes
- To perform basic calculations between two rasters (Cell 1 <operator> Cell 2), consider using the RasterCellValueCalculator.
- To perform basic calculations between one raster cell and a numeric value, consider using the RasterSingularCellValueCalculator.
- To combine rasters, blending the edges of partial overlaps, consider using the RasterMosaicker.
- Nodata values can be set with the RasterBandNodataSetter or removed with the RasterBandNodataRemover.
- Palettes may be resolved using the RasterPaletteResolver or removed using the RasterPaletteRemover.
Choosing a Raster Transformer
FME has an extensive selection of transformers for working with raster data. They can be generally categorized as working with whole rasters, bands, cells or palettes, and those designed for workflow control or combing raster with vector data.
For information on raster geometry and properties, see Rasters (IFMERaster).
Working with Rasters
RasterCellOriginSetter | Sets the cell origin point within cells in a raster. |
RasterConvolver |
Applies a convolution filter (sometimes called a kernel or lens) to raster features and outputs the results. |
RasterExpressionEvaluator | Evaluates expressions on each cell in a raster or pair of rasters, including algebraic operations and conditional statements. |
RasterExtentsCoercer | Replaces the geometry of input raster features with a polygon covering either the extents of a raster or the extent of data within a raster. |
RasterGCPExtractor | Extracts Ground Control Point (GCP) coordinate system and point values from a raster feature and exposes them as attributes. |
RasterGCPSetter | Sets Ground Control Points (GCPs) on a raster, pairing cell positions with known coordinates. |
RasterGeoreferencer | Georeferences a raster by either known corner coordinates or origin, cell size, and rotation. |
RasterHillshader | Generates a grayscale shaded relief representation of terrain, based on elevation values. |
RasterInterpretationCoercer |
Alters the interpretation type of rasters, including all bands, and converts cell values if necessary. |
RasterMosaicker | Merges multiple raster features into a single raster feature. |
RasterPropertyExtractor | Extracts the geometry properties of a raster feature and exposes them as attributes. |
RasterPyramider | Resamples rasters to multiple resolutions, based on either number of levels or dimensions of the smallest output raster. |
RasterResampler | Resamples rasters, based on specified output dimensions, cell size in ground units, or percentage of original, and interpolates new cell values. |
RasterRotationApplier |
Rotates a raster feature according to its rotation angle property, interpolating new cell values, updating all other affected raster properties, and producing an output raster feature with a rotation angle of zero. |
RasterSubsetter | Clips raster features using pixel bounds instead of ground coordinates, and optionally adds cells around the perimeter. |
RasterTiler | Splits each input raster into a series of tiles by specifying either a tile size in cells/pixels or the number of tiles. |
RasterToPolygonCoercer | Creates polygons from input raster features. One polygon is output for each contiguous area of pixels with the same value in the input raster. |
WebMapTiler | Creates a series of image tiles that can be utilized by web mapping applications such as Bing™ Maps, Google Maps™, or Web Map Tile Service. This is done by resampling rasters to various different resolutions and then splitting them into tiles. |
Working with Bands
RasterBandAdder | Adds a new band to a raster feature. |
RasterBandCombiner | Merges coincidental raster features into a single output raster feature, preserving and appending all bands. |
RasterBandInterpretationCoercer |
Alters the interpretation type of individual raster bands, converting cell values if necessary. |
RasterBandKeeper |
Removes all unselected bands from a raster feature. |
RasterBandMinMaxExtractor | Extracts the minimum and maximum band values, palette keys, and palette values from a raster feature, and adds them to a list attribute. |
RasterBandNameSetter | Sets the band name of selected bands on a raster, making raster contents simpler to understand compared to band numbers. |
RasterBandNodataRemover | Removes the existing nodata identifier from selected bands of a raster feature. Any values previously equal to the nodata value are considered valid data. |
RasterBandNodataSetter | Sets a new nodata value on selected bands of a raster feature. |
RasterBandOrderer | Specifies the required order of bands in a raster. Bands are reordered according to the input band indices. |
RasterBandPropertyExtractor | Extracts the band and palette properties of a raster feature and exposes them as attributes. |
RasterBandRemover | Removes any selected bands from a raster feature. |
RasterBandSeparator | Separates bands or unique band and palette combinations, and outputs either individual raster features or a single new raster feature containing all combinations. |
RasterStatisticsCalculator | Calculates statistics on raster bands and adds the results as attributes. |
Working with Cells
RasterAspectCalculator |
Calculates the aspect (direction of slope) for each cell of a raster. Aspect is measured in degrees from 0 to 360, clockwise from north. |
RasterCellCoercer | Creates individual points or polygons for each cell in a raster, optionally extracting band values as z coordinates or attributes. |
RasterCellValueCalculator | Evaluates basic arithmetic , minimum, maximum or average operations on the cell values of a pair of rasters. |
RasterCellValueReplacer | Replaces a range of band values in a raster with a new single value. |
RasterCellValueRounder | Rounds off raster cell values. |
RasterSingularCellValueCalculator | Performs basic arithmetic operations on the cell values of a raster against a numeric value. |
RasterSlopeCalculator | Calculates the slope (maximum rate of change in z) for each cell of a raster. |
Working with Palettes
RasterPaletteAdder |
Creates a palette from an attribute, and adds this palette to all selected bands on a raster. |
RasterPaletteExtractor | Creates a string representation of an existing palette on a raster and saves it to an attribute. |
RasterPaletteGenerator | Generates a palette out of the selected band(s) of a raster. The output raster will have the selected band(s) replaced by a new band with a palette. |
RasterPaletteInterpretationCoercer |
Alters the interpretation type of raster palettes. |
RasterPaletteNodataSetter |
Identifies the palette key that matches a raster band’s nodata value, and sets a value on it. |
RasterPaletteRemover | Removes selected palette(s) from raster features. |
RasterPaletteResolver | Resolves the palette(s) on a raster by replacing cell values with their corresponding palette values. Palette values with multiple components, such as RGB, are broken down and the individual values assigned to multiple, newly-added bands. |
Workflow Control
RasterCheckpointer | Forces accumulated raster operations to be processed, saving the state to disk and releasing resources to tune performance or assist with memory limitations. |
RasterConsumer | Reads raster features for testing purposes, including any accumulated raster operations. No additional operations are performed, and nothing is done with the features. |
RasterExtractor | Serializes the geometry of a raster feature into a Blob attribute, encoding the contents according to a choice of common binary raster formats. |
RasterNumericCreator | Creates a numeric raster of specified size and resolution, with default cell values. |
RasterReplacer | Decodes a binary attribute containing encoded rasters stored as Blobs, replacing the feature’s geometry with the decoded raster. |
RasterRGBCreator | Creates a color raster feature of specified size, resolution, and interpretation type, with default cell values. |
RasterSelector |
Selects specific bands and palettes of a raster for subsequent transformer operations. |
Vectors and Rasters
ImageRasterizer | Creates a raster representation of vector or point cloud input features, using the fme_color attribute over a solid background fill for vector features. Point clouds may be rendered using their color or intensity components. |
NumericRasterizer | Creates a numeric raster representation of vector or point cloud input features, where cell values are taken from the z coordinates of the input features and overlaid on a uniform background. |
MapnikRasterizer | Generates a raster from input vector and raster features, with fine control over symbolization and labeling, using the Mapnik toolkit. |
PointOnRasterValueExtractor | Extracts the band and palette values from a raster at the location of one or more input points and sets them as attributes on the feature. |
RasterDEMGenerator | Produces a raster digital elevation model (DEM) by uniformly sampling a Delaunay triangulation generated from input points and breaklines. |
VectorOnRasterOverlayer | Rasterizes vector or point cloud features onto an existing raster. For vector features the fme_color attribute sets pixel color, and point clouds may be rendered using their color or intensity components. |
Configuration
Input Ports
Raster features to evaluate expressions against. Palettes are not supported.
The B input port is only exposed when Mode: Two Rasters is currently selected.
Output Ports
Raster features where bands are defined and values calculated according to parameter selections. One raster is output for each input raster in One Raster mode, and for each A/B pair in Two Raster mode.
Non-raster features will be routed to the <Rejected> port, as well as invalid rasters.
Rejected features will have an fme_rejection_code attribute with one of the following values:
INVALID_GEOMETRY_TYPE
INVALID_A_RASTER_NO_BANDS
INVALID_B_RASTER_NO_BANDS
INVALID_A_RASTER_MISSING_BAND
INVALID_B_RASTER_MISSING_BAND
MISSING_A_RASTER_MISSING_ATTRIBUTES
MISSING_B_RASTER_MISSING_ATTRIBUTES
EXTRA_A_RASTER_HAS_PALETTE
EXTRA_B_RASTER_HAS_PALETTE
Rejected Feature Handling: can be set to either terminate the translation or continue running when it encounters a rejected feature. This setting is available both as a default FME option and as a workspace parameter.
Parameters
Mode |
This parameter specifies how many types of input features are allowed. One Raster: Only A input features are permitted. Two Rasters: A and B input features are permitted. |
Group By | If any Group By attributes are given, then each group will be treated independently. This allows a single transformer to operate on multiple pairs of As and Bs. Note that this parameter is not applicable when the Mode is One Raster; in that case, each raster is considered individually and there are no groupings. |
This table is used to specify how to calculate and interpret one or more bands in the output raster.
Each row of the table represents a different band in the output raster, in sequential order.
Interpretation |
Defines the Interpretation Type of each output band. Preserve: If all input bands share the same interpretation, the output band will preserve that interpretation. If the input bands do not share the same interpretation, Preserve is the same as Auto. Auto: The output interpretation will be automatically determined based on the data types used to perform the calculations. Alternatively, the Interpretation may be specified as any of the following:
|
Expression |
Define an expression to be evaluated. The Arithmetic Editor - Raster Expression may be accessed via the ellipsis (...) button to assist in creating expressions. The result of this expression will be stored in the corresponding band on the output raster. |
Editing Transformer Parameters
Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.
Defining Values
There are several ways to define a value for use in a Transformer. The simplest is to simply type in a value or string, which can include functions of various types such as attribute references, math and string functions, and workspace parameters. There are a number of tools and shortcuts that can assist in constructing values, generally available from the drop-down context menu adjacent to the value field.
Using the Text Editor
The Text Editor provides a convenient way to construct text strings (including regular expressions) from various data sources, such as attributes, parameters, and constants, where the result is used directly inside a parameter.
Using the Arithmetic Editor
The Arithmetic Editor provides a convenient way to construct math expressions from various data sources, such as attributes, parameters, and feature functions, where the result is used directly inside a parameter.
Conditional Values
Set values depending on one or more test conditions that either pass or fail.
Parameter Condition Definition Dialog
Content
Expressions and strings can include a number of functions, characters, parameters, and more - whether entered directly in a parameter or constructed using one of the editors.
These functions manipulate and format strings. | |
A set of control characters is available in the Text Editor. | |
Math functions are available in both editors. | |
These operators are available in the Arithmetic Editor. | |
These return primarily feature-specific values. | |
FME and workspace-specific parameters may be used. | |
Working with User Parameters | Create your own editable parameters. |
Reference
Processing Behavior |
|
Feature Holding |
Yes |
Dependencies | None |
FME Licensing Level | FME Professional Edition and above |
Aliases | |
History | |
Categories |
FME Community
The FME Community is the place for demos, how-tos, articles, FAQs, and more. Get answers to your questions, learn from other users, and suggest, vote, and comment on new features.
Search for all results about the RasterExpressionEvaluator on the FME Community.
Examples may contain information licensed under the Open Government Licence – Vancouver