The MapCalc operation of ILWIS is mathematical expression of pixel values that supports not only implementation of arithmetic, logical and relational operators but also execution of various functions, specially conditional iff, or various combination of operators and ILWIS functions in order to manipulate pixel values of a raster coverage. The ILWIS MapCalc is a powerful user-friendly tools that can be used either as a simple raster calculator or as a tool for implementation of complex mathematical expressions in order to generate analytical or thematic outputs.
A MapCalc formula consists of:
- an output raster map name that will contain the result of the calculation
- a definition symbol =
- an expression
The MapCalc syntax, in its general form, can be illustrated as:
Outputmap = Expression
A simple MapCalc formula reads e.g., as:
OutMap = Map1 + 100 or OutMap = MapA + MapB
A more complex formula reads e.g., as:
OutMap= iff(Map1 < 0 and Map2 < 0, abs(Map1+ Map2), Map1 + Map2)
Note, if you using the ILWIS MapCalc operation form, symbols @1, @2, @n (n=6) will be used to replace the input Map1, Map2, Mapn (the MapCalc operation support the use of maximum 6 raster maps in an expression). For the aforementioned examples, ILWIS expression will looks such as the following:
OutMap = @1 + 100
OutMap = @1 + @2
OutMap = iff(@1 < 0 && @2 < 0, abs(@1+@2), @1+@2)
OutMap = iff(@1 <=5, 'Acidic', iff(@1 >=8, 'Alkaline', 'Neutral'))
In this example the input map contains soil pH values.
Note, that in ILWIS expressions the && and || symbols are used for logical operators of and, or, respectively. Single quotes has to be used for string or class name within an expression. The used arithmetic operators symbols in ILWIS are: +, -, *, / , and the used relational operators symbols are: ==, EQ (equal), <, LT (less than), <=, LE (equal or less than), >, GT (greater than), >=, GE (equal or greater than), != (unequal) .
The MapCalc operation can be applied via ILWIS-Python editor or via ILWIS main window, Toolbar and the Operatins tab. The required Python syntax is:
name of the output raster coverage = ilwis.Engine.do(name of the operation, expression, name of the first input raster coverage, name of the second input raster coverage)
Note, that the number of input raster maps, depends on how many maps are used in the expression. For example, in case of @1 + 100 only one input raster coverage is needed while in case of @1 + @2 - @3 , three input raster maps are required (ILWIS MapCalc operation supports use of maximum 6 raster maps).
In the example bellow, we show use of two raster coverages; where the first one is clay.mpr and the second one called silt.mpr, the expression is @1 + @2 and the name of the output raster coverage is silt_clay.
silt_clay=ilwis.Engine.do("mapcalc", "@1 + @2",“file:///C:/mydir/clay.mpr",
In general, the URL used in the Python syntax can be replaced with variable name created via using Python. For example: you can open a raster coverage as variables and give it a name as the following:
rc1 = ilwis.RasterCoverage("file:///C:/mydir/clay.mpr")
rc2 = ilwis.RasterCoverage("file:///C:/mydir/silt.mpr")
Once the variable is created via ILWIS-Python editor, then it can be used directly in Python syntax without quotation mark and the path as the following:
silt_clay=ilwis.Engine.do("mapcalc", "@1 + @2", rc1, rc2)