Operations: MapCalc

Jump to: navigation, search
Main Page Arrow.png Operations

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)

Figure 1, MapCalc2 operation form with two input raster fields

To start the MapCalc operation from ILWIS main window:

  • Click the Operation tab on toolbar tabs
  • From the opened operation list select MapCalc

Note, that in the operation list you will find six MapCalc operations numbered from MapCalc 1 to MapCalc 6. The numbers refer to the number of the input maps. For example, if the expression contains only one map, then you need to select MapCac 1 which allows only one raster input, but if the expression includes 4 raster maps, then you need to select MapCalc 4 which allows you to enter 4 input rasters.

Once the MapCalc operation is selected from the list, an operation form will be opened in which users can set parameters required for the operation including input(s), output(s), expression, memory location and data format (Figure 1).

To successfully execute this operation, you need to complete the form with proper inputs data. The inputs data must be dragged from the catalog and dropped in their corresponding fields in the form or directly typed in.

MapCalc 2 operation form

Expression: This field allows you to enter the expression to be applied using pixels values of the input raster maps.

Raster or number: Enter a raster coverage by using drag and drop or type in the name of the map or a number.

Raster or number: Enter a raster coverage by using drag and drop or type in the name of the map or a number.

Output raster coverage: Type a name for the output raster coverage.
Output format: Select a format from the list to save the output raster map. The default format is Memory; if you select it, the output will be temporally saved in the internal catalog and will be removed when you exit the program. If you select other format, the output will be saved in the active catalog.

Execute: Click the execute button to run this operation.