Map Algebra
Map algebra is a way to perform raster calculations using mathematical expressions. The expression can be a simple arithmetic operation or a complex combination of multiple operations. The expression can be applied to a single raster band or multiple raster bands. The result of the expression is a new raster. Apache Sedona provides two ways to perform map algebra operations:- Using the
RS_MapAlgebrafunction. - Using
RS_BandAsArrayand array based map algebra functions, such asRS_Add,RS_Multiply, etc.
RS_MapAlgebra function is more flexible and can be used to perform more complex operations. The function takes three to four arguments:
rast: The raster to apply the map algebra expression to.pixelType: The data type of the output raster. This can be one ofD(double),F(float),I(integer),S(short),US(unsigned short) orB(byte). If specifiedNULL, the output raster will have the same data type as the input raster.script: The map algebra script. (For guidance on scripting for map algebra, refer to the Jiffle documentation.)noDataValue: (Optional) The nodata value of the output raster.
RS_MapAlgebra function allows two raster column inputs, with multi-band rasters supported. The function accepts 5 parameters:
rast0: The first raster to apply the map algebra expression to.rast1: The second raster to apply the map algebra expression to.pixelType: The data type of the output raster. This can be one ofD(double),F(float),I(integer),S(short),US(unsigned short) orB(byte). If specifiedNULL, the output raster will have the same data type as the input raster.script: The map algebra script. Refer to the NDVI section for more details on the format.noDataValue: (Not optional) The nodata value of the output raster,nullis allowed.
RS_MapAlgebra:
RS_MapAlgebra function supports returning multi-band rasters:
numBands parameter. The output band can be specified in the same way as with the input rasters in the script, e.g:
RS_MapAlgebra also has good performance, since it is backed by Jiffle and can be compiled to Java bytecode for
execution. We’ll demonstrate both approaches to implementing commonly used map algebra operations.
The
RS_MapAlgebra function can cast the output raster to a different data type specified by pixelType:-
If
pixelTypeis smaller than the input raster data type, narrowing casts will be performed, which may result in loss of data. -
If
pixelTypeis larger, widening casts will retain data accuracy. -
If
pixelTypematches the input raster data type, no casting occurs.
NDVI
The Normalized Difference Vegetation Index (NDVI) is a simple graphical indicator that can be used to analyze remote sensing measurements, typically, but not necessarily, from a space platform, and assess whether the target being observed contains live green vegetation or not. NDVI has become a de facto standard index used to determine whether a given area contains live green vegetation or not. The NDVI is calculated from these individual measurements as follows:RS_MapAlgebra function to do this:
out = (rast[3] - rast[0]) / (rast[3] + rast[0]);. The rast variable is always bound to the input raster, and
the out variable is bound to the output raster. Jiffle iterates over all the pixels in the input raster and executes the script for each pixel. the rast[3] and rast[0]
refers to the current pixel values of the near-infrared and red bands, respectively. The out variable is the current output pixel value.
The result of the RS_MapAlgebra function is a raster with a single band. The band is of type double, since we specified D as the pixelType argument.
We can implement the same NDVI calculation using the array based map algebra functions:
RS_BandAsArray function extracts the specified band of the input raster to an array of double, and the RS_Add, RS_Subtract, and RS_Divide functions perform the arithmetic operations on the arrays. The code using the array based map algebra functions is more verbose. However, there is a RS_NormalizedDifference function that can be used to calculate the NDVI more concisely:
RS_AddBandFromArray to add the array to a raster as a new band.
AWEI
The Automated Water Extraction Index (AWEI) is a spectral index that can be used to extract water bodies from remote sensing imagery. The AWEI is calculated from these individual measurements as follows:RS_MapAlgebra:

