.TH r.mapcalc 1grass "" "GRASS 6.4.4" "Grass User's Manual" .SH NAME \fI\fBr.mapcalc\fR\fR .SH DESCRIPTION \fIr.mapcalc\fR performs arithmetic on raster map layers. New raster map layers can be created which are arithmetic expressions involving existing raster map layers, integer or floating point constants, and functions. .SS PROGRAM USE If used without command line arguments, \fIr.mapcalc\fR will read its input, one line at a time, from standard input (which is the keyboard, unless redirected from a file or across a pipe). Otherwise, the expression on the command line is evaluated. \fIr.mapcalc\fR expects its input to have the form: .PP \fBresult=\fR\fIexpression\fR .PP where \fIresult\fR is the name of a raster map layer to contain the result of the calculation and \fIexpression\fR is any legal arithmetic expression involving existing raster map layers, integer or floating point constants, and functions known to the calculator. Parentheses are allowed in the expression and may be nested to any depth. \fIresult\fR will be created in the user's current mapset. .PP The formula entered to \fIr.mapcalc\fR by the user is recorded both in the \fIresult\fR map title (which appears in the category file for \fIresult\fR) and in the history file for \fIresult\fR. .PP Some characters have special meaning to the command shell. If the user is entering input to \fIr.mapcalc\fR on the command line, expressions should be enclosed within single quotes. See NOTES, below. .PP .SS OPERATORS AND ORDER OF PRECEDENCE The following operators are supported: \fC .DS .br Operator Meaning Type Precedence .br -------------------------------------------------------------- .br - negation Arithmetic 12 .br ~ one's complement Bitwise 12 .br ! not Logical 12 .br ^ exponentiation Arithmetic 11 .br % modulus Arithmetic 10 .br / division Arithmetic 10 .br * multiplication Arithmetic 10 .br + addition Arithmetic 9 .br - subtraction Arithmetic 9 .br << left shift Bitwise 8 .br >> right shift Bitwise 8 .br >>> right shift (unsigned) Bitwise 8 .br > greater than Logical 7 .br >= greater than or equal Logical 7 .br < less than Logical 7 .br <= less than or equal Logical 7 .br == equal Logical 6 .br != not equal Logical 6 .br & bitwise and Bitwise 5 .br | bitwise or Bitwise 4 .br && logical and Logical 3 .br &&& logical and[1] Logical 3 .br || logical or Logical 2 .br ||| logical or[1] Logical 2 .br ?: conditional Logical 1 .br .DE \fR (modulus is the remainder upon division) .PP [1] The &&& and ||| operators handle null values differently to other operators. See the section entitled \fBNULL support\fR below for more details. .PP The operators are applied from left to right, with those of higher precedence applied before those with lower precedence. Division by 0 and modulus by 0 are acceptable and give a NULL result. The logical operators give a 1 result if the comparison is true, 0 otherwise. .PP .PP .SS RASTER MAP LAYER NAMES Anything in the expression which is not a number, operator, or function name is taken to be a raster map layer name. Examples: .PP \fC .DS .br elevation .br x3 .br 3d.his .br .DE \fR .PP Most GRASS raster map layers meet this naming convention. However, if a raster map layer has a name which conflicts with the above rule, it should be quoted. For example, the expression .PP \fC .DS .br x = a-b .br .DE \fR .PP would be interpreted as: x equals a minus b, whereas .PP \fC .DS .br x = "a-b" .br .DE \fR .PP would be interpreted as: x equals the raster map layer named \fIa-b\fR .PP Also .PP \fC .DS .br x = 3107 .br .DE \fR .PP would create \fIx\fR filled with the number 3107, while .PP \fC .DS .br x = "3107" .br .DE \fR .PP would copy the raster map layer \fI3107\fR to the raster map layer \fIx\fR. .PP Quotes are not required unless the raster map layer names look like numbers or contain operators, OR unless the program is run non-interactively. Examples given here assume the program is run interactively. See NOTES, below. .PP \fIr.mapcalc\fR will look for the raster map layers according to the user's current mapset search path. It is possible to override the search path and specify the mapset from which to select the raster map layer. This is done by specifying the raster map layer name in the form: .PP \fC .DS .br name@mapset .br .DE \fR .PP For example, the following is a legal expression: .PP \fC .DS .br result = x@PERMANENT / y@SOILS .br .DE \fR .PP The mapset specified does not have to be in the mapset search path. (This method of overriding the mapset search path is common to all GRASS commands, not just \fIr.mapcalc\fR.) .PP .SS THE NEIGHBORHOOD MODIFIER Maps and images are data base files stored in raster format, i.e., two-dimensional matrices of integer values. In \fIr.mapcalc\fR, maps may be followed by a \fIneighborhood\fR modifier that specifies a relative offset from the current cell being evaluated. The format is \fImap[r,c]\fR, where \fIr\fR is the row offset and \fIc\fR is the column offset. For example, \fImap[1,2]\fR refers to the cell one row below and two columns to the right of the current cell, \fImap[-2,-1]\fR refers to the cell two rows above and one column to the left of the current cell, and \fImap[0,1]\fR refers to the cell one column to the right of the current cell. This syntax permits the development of neighborhood-type filters within a single map or across multiple maps. .PP .SS RASTER MAP LAYER VALUES FROM THE CATEGORY FILE Sometimes it is desirable to use a value associated with a category's \fIlabel\fR instead of the category value itself. If a raster map layer name is preceded by the \fB@\fR operator, then the labels in the category file for the raster map layer are used in the expression instead of the category value. .PP For example, suppose that the raster map layer \fIsoil.ph\fR (representing soil pH values) has a category file with labels as follows: .PP \fC .DS .br cat label .br ------------------ .br 0 no data .br 1 1.4 .br 2 2.4 .br 3 3.5 .br 4 5.8 .br 5 7.2 .br 6 8.8 .br 7 9.4 .br .DE \fR .PP Then the expression: .PP \fC .DS .br result = @soils.ph .br .DE \fR .PP would produce a result with category values 0, 1.4, 2.4, 3.5, 5.8, 7.2, 8.8 and 9.4. .PP Note that this operator may only be applied to raster map layers and produces a floating point value in the expression. Therefore, the category label must start with a valid number. If the category label is integer, it will be represented by a floating point number. I the category label does not start with a number or is missing, it will be represented by NULL (no data) in the resulting raster map. .SS GREY SCALE EQUIVALENTS AND COLOR SEPARATES It is often helpful to manipulate the colors assigned to map categories. This is particularly useful when the spectral properties of cells have meaning (as with imagery data), or when the map category values represent real quantities (as when category values reflect true elevation values). Map color manipulation can also aid visual recognition, and map printing. .PP The # operator can be used to either convert map category values to their grey scale equivalents or to extract the red, green, or blue components of a raster map layer into separate raster map layers. .PP \fC .DS .br result = #map .br .DE \fR .PP converts each category value in \fImap\fR to a value in the range 0-255 which represents the grey scale level implied by the color for the category. If the map has a grey scale color table, then the grey level is what #map evaluates to. Otherwise, it is computed as: .PP \fC .DS .br 0.10 * red + 0.81 * green + 0.01 * blue .br .DE \fR .PP Alternatively, you can use: .PP \fC .DS .br result = y#map .br .DE \fR .PP to use the NTSC weightings: .PP \fC .DS .br 0.30 * red + 0.59 * green + 0.11 * blue .br .DE \fR .PP Or, you can use: .PP \fC .DS .br result = i#map .br .DE \fR .PP to use equal weightings: .PP \fC .DS .br 0.33 * red + 0.33 * green + 0.33 * blue .br .DE \fR .PP The # operator has three other forms: r#map, g#map, b#map. These extract the red, green, or blue components in the named raster map, respectively. The GRASS shell script \fIr.blend\fR extracts each of these components from two raster map layers, and combines them by a user-specified percentage. These forms allow color separates to be made. For example, to extract the red component from \fImap\fR and store it in the new 0-255 map layer \fIred\fR, the user could type: .PP \fC .DS .br red = r#map .br .DE \fR .PP To assign this map grey colors type: .PP \fC .DS .br r.colors map=red color=rules .br black .br white .br .DE \fR .PP To assign this map red colors type: .PP \fC .DS .br r.colors map=red color=rules .br black .br red .br .DE \fR .PP .SS FUNCTIONS The functions currently supported are listed in the table below. The type of the result is indicated in the last column. \fIF\fR means that the functions always results in a floating point value, \fII\fR means that the function gives an integer result, and \fI*\fR indicates that the result is float if any of the arguments to the function are floating point values and integer if all arguments are integer. .PP \fC .DS .br function description type .br --------------------------------------------------------------------------- .br abs(x) return absolute value of x * .br acos(x) inverse cosine of x (result is in degrees) F .br asin(x) inverse sine of x (result is in degrees) F .br atan(x) inverse tangent of x (result is in degrees) F .br atan(x,y) inverse tangent of y/x (result is in degrees) F .br cos(x) cosine of x (x is in degrees) F .br double(x) convert x to double-precision floating point F .br eval([x,y,...,]z) evaluate values of listed expr, pass results to z .br exp(x) exponential function of x F .br exp(x,y) x to the power y F .br float(x) convert x to single-precision floating point F .br graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F .br if decision options: * .br if(x) 1 if x not zero, 0 otherwise .br if(x,a) a if x not zero, 0 otherwise .br if(x,a,b) a if x not zero, b otherwise .br if(x,a,b,c) a if x > 0, b if x is zero, c if x < 0 .br int(x) convert x to integer [ truncates ] I .br isnull(x) check if x = NULL .br log(x) natural log of x F .br log(x,b) log of x base b F .br max(x,y[,z...]) largest value of those listed * .br median(x,y[,z...]) median value of those listed * .br min(x,y[,z...]) smallest value of those listed * .br mode(x,y[,z...]) mode value of those listed * .br not(x) 1 if x is zero, 0 otherwise .br pow(x,y) x to the power y * .br rand(a,b) random value x : a <= x < b .br round(x) round x to nearest integer I .br sin(x) sine of x (x is in degrees) F .br sqrt(x) square root of x F .br tan(x) tangent of x (x is in degrees) F .br xor(x,y) exclusive-or (XOR) of x and y I .br .DE \fR \fC .DS .br Internal variables: .br row() current row of moving window .br col() current col of moving window .br x() current x-coordinate of moving window .br y() current y-coordinate of moving window .br ewres() current east-west resolution .br nsres() current north-south resolution .br null() NULL value .br .DE \fR Note, that the row() and col() indexing starts with 1. .SS FLOATING POINT VALUES IN THE EXPRESSION Floating point numbers are allowed in the expression. A floating point number is a number which contains a decimal point: \fC .DS .br 2.3 12.0 12. .81 .br .DE \fR Floating point values in the expression are handled in a special way. With arithmetic and logical operators, if either operand is float, the other is converted to float and the result of the operation is float. This means, in particular that division of integers results in a (truncated) integer, while division of floats results in an accurate floating point value. With functions of type * (see table above), the result is float if any argument is float, integer otherwise. .PP Note: If you calculate with integer numbers, the resulting map will be integer. If you want to get a float result, add the decimal point to integer number(s). .PP If you want floating point division, at least one of the arguments has to be a floating point value. Multiplying one of them by 1.0 will produce a floating-point result, as will using float(): \fC .DS .br r.mapcalc "ndvi=float(lsat.4 - lsat.3) / (lsat.4 + lsat.3)" .br .DE \fR .SS NULL support .RS .IP Division by zero should result in NULL. .IP Modulus by zero should result in NULL. .IP NULL-values in any arithmetic or logical operation should result in NULL. (however, &&& and ||| are treated specially, as described below). .IP The &&& and ||| operators observe the following axioms even when x is NULL: \fC .DS .br x &&& false == false .br false &&& x == false .br x ||| true == true .br true ||| x == true .br .DE \fR .IP NULL-values in function arguments should result in NULL (however, if(), eval() and isnull() are treated specially, as described below). .IP The eval() function always returns its last argument .IP The situation for if() is: \fC .DS .br if(x) .br NULL if x is NULL; 0 if x is zero; 1 otherwise .br if(x,a) .br NULL if x is NULL; a if x is non-zero; 0 otherwise .br if(x,a,b) .br NULL if x is NULL; a if x is non-zero; b otherwise .br if(x,n,z,p) .br NULL if x is NULL; n if x is negative; .br z if x is zero; p if x is positive .br .DE \fR .IP The (new) function isnull(x) returns: 1 if x is NULL; 0 otherwise. The (new) function null() (which has no arguments) returns an integer NULL. .IP Non-NULL, but invalid, arguments to functions should result in NULL. \fC .DS .br Examples: .br log(-2) .br sqrt(-2) .br pow(a,b) where a is negative and b is not an integer .br .DE \fR .RE .PP NULL support: Please note that any math performed with NULL cells always results in a NULL value for these cells. If you want to replace a NULL cell on-the-fly, use the isnull() test function in a if-statement. .PP Example: The users wants the NULL-valued cells to be treated like zeros. To add maps A and B (where B contains NULLs) to get a map C the user can use a construction like: .PP \fC .DS .br C=A + if(isnull(B),0,B) .br .DE \fR .PP \fBNULL and conditions:\fR .PP For the one argument form: \fC .DS .br if(x) = NULL if x is NULL .br if(x) = 0 if x = 0 .br if(x) = 1 otherwise (i.e. x is neither NULL nor 0). .br .DE \fR .PP For the two argument form: \fC .DS .br if(x,a) = NULL if x is NULL .br if(x,a) = 0 if x = 0 .br if(x,a) = a otherwise (i.e. x is neither NULL nor 0). .br .DE \fR .PP For the three argument form: \fC .DS .br if(x,a,b) = NULL if x is NULL .br if(x,a,b) = b if x = 0 .br if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0). .br .DE \fR .PP For the four argument form: \fC .DS .br if(x,a,b,c) = NULL if x is NULL .br if(x,a,b,c) = a if x > 0 .br if(x,a,b,c) = b if x = 0 .br if(x,a,b,c) = c if x < 0 .br .DE \fR More generally, all operators and most functions return NULL if *any* of their arguments are NULL. .br The functions if(), isnull() and eval() are exceptions. .br The function isnull() returns 1 if its argument is NULL and 0 otherwise. If the user wants the opposite, the ! operator, e.g. "!isnull(x)" must be used. .PP All forms of if() return NULL if the first argument is NULL. The 2, 3 and 4 argument forms of if() return NULL if the "selected" argument is NULL, e.g.: \fC .DS .br if(0,a,b) = b regardless of whether a is NULL .br if(1,a,b) = a regardless of whether b is NULL .br .DE \fR eval() always returns its last argument, so it only returns NULL if the last argument is NULL. .PP \fBNote\fR: The user cannot test for NULL using the == operator, as that returns NULL if either or both arguments are NULL, i.e. if x and y are both NULL, then "x == y" and "x != y" are both NULL rather than 1 and 0 respectively. .br The behaviour makes sense if the user considers NULL as representing an unknown quantity. E.g. if x and y are both unknown, then the values of \(dqx == y" and "x != y" are also unknown; if they both have unknown values, the user doesn't know whether or not they both have the same value. .PP .SH NOTES Extra care must be taken if the expression is given on the command line. Some characters have special meaning to the UNIX shell. These include, among others: .PP * ( ) > & | .PP It is advisable to put single quotes around the expression; e.g.: \fC .DS .br result = 'elevation * 2' .br .DE \fR Without the quotes, the *, which has special meaning to the UNIX shell, would be altered and \fIr.mapcalc\fR would see something other than the *. .PP In general, it's preferable to do as much as possible in each r.mapcalc command. E.g. rather than: \fC .DS .br r.mapcalc "$GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND" .br r.mapcalc "$GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND" .br r.mapcalc "$GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND" .br .DE \fR use: \fC .DS .br r.mapcalc <