123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- <h2>DESCRIPTION</h2>
- <em>r3.mapcalc</em> 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.
- <h2>PROGRAM USE</h2>
- <em>r3.mapcalc</em> expects its input to have the form:
- <p><b>result =</b><em> expression</em>
- <p>where <em>result</em> is the name of a raster map layer
- to contain the result of the calculation and
- <em>expression</em> 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.
- <em>result</em> will be created in the user's current mapset.
- <p>
- As <em>expression=</em> is the first option, it is the default. This
- means that passing an expression on the command line is possible
- as long as the expression is quoted and a space is included before the
- first <em>=</em> sign.
- Example ('foo' is the resulting map):
- <div class="code"><pre>
- r3.mapcalc "foo = 1"
- </pre></div>
- or:
- <div class="code"><pre>
- r3.mapcalc 'foo = 1'
- </pre></div>
- An unquoted expression (i.e. split over multiple arguments) won't
- work, nor will omitting the space before the = sign:
- <div class="code"><pre>
- r3.mapcalc 'foo=1'
- Sorry, <foo> is not a valid parameter
- </pre></div>
- If no options are given, it manufactures "input=-" (which reads from
- stdin), so you can continue to use e.g.:
- <div class="code"><pre>
- r3.mapcalc < file
- </pre></div>
- or:
- <div class="code"><pre>
- r3.mapcalc <<EOF
- foo = 1
- EOF
- </pre></div>
- But unless you need compatibility with previous versions, use input=
- explicitly, e.g.:
- <div class="code"><pre>
- r3.mapcalc input=file
- </pre></div>
- or:
- <div class="code"><pre>
- r3.mapcalc input=- <<EOF
- foo = 1
- EOF
- </pre></div>
- <p>The formula entered to <em>r3.mapcalc</em> by the user is recorded both in the
- <em>result</em> map title (which appears in the category file for <em>result</em>)
- and in the history file for <em>result</em>.
- <p>Some characters have special meaning to the command shell. If the user
- is entering input to <em>r3.mapcalc</em> on the command line, expressions
- should be enclosed within single quotes. See NOTES, below.
- <p>
- <h2>OPERATORS AND ORDER OF PRECEDENCE</h2>
- The following operators are supported:
- <div class="code"><pre>
- Operator Meaning Type Precedence
- --------------------------------------------------------------
- - negation Arithmetic 12
- ~ one's complement Bitwise 12
- ! not Logical 12
- ^ exponentiation Arithmetic 11
- % modulus Arithmetic 10
- / division Arithmetic 10
- * multiplication Arithmetic 10
- + addition Arithmetic 9
- - subtraction Arithmetic 9
- << left shift Bitwise 8
- >> right shift Bitwise 8
- >>> right shift (unsigned) Bitwise 8
- > greater than Logical 7
- >= greater than or equal Logical 7
- < less than Logical 7
- <= less than or equal Logical 7
- == equal Logical 6
- != not equal Logical 6
- & bitwise and Bitwise 5
- | bitwise or Bitwise 4
- && logical and Logical 3
- &&& logical and[1] Logical 3
- || logical or Logical 2
- ||| logical or[1] Logical 2
- ?: conditional Logical 1
- </pre></div>
- (modulus is the remainder upon division)
- <p>[1] The &&& and ||| operators handle null values differently to other
- operators. See the section entitled <b>NULL support</b> below for more
- details.
- <p>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.
- <p><p>
- <h2>3D GRID NAMES</h2>
- Anything in the expression which is not a number, operator, or function
- name is taken to be a 3D grid name. Examples:
- <p><div class="code"><pre>
- volume
- x3
- 3d.his
- </pre></div>
- <p>Most GRASS raster map layers and 3D grids meet this naming convention.
- However, if a 3D grid has a name which conflicts with the above rule, it
- should be quoted. For example, the expression
- <p><div class="code"><pre>
- x = a-b
- </pre></div>
- <p>would be interpreted as: x equals a minus b, whereas
- <p><div class="code"><pre>
- x = "a-b"
- </pre></div>
- <p>would be interpreted as: x equals the 3D grid named <em>a-b</em>
- <p>Also
- <p><div class="code"><pre>
- x = 3107
- </pre></div>
- <p>would create <em>x</em> filled with the number 3107, while
- <p><div class="code"><pre>
- x = "3107"
- </pre></div>
- <p>would copy the 3D grid <em>3107</em> to the 3D grid <em>x</em>.
- <p>Quotes are not required unless the 3D grid 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.
- <p><em>r3.mapcalc</em> will look for the 3D grids 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 3D grid. This is done by
- specifying the 3D grid name in the form:
- <p><div class="code"><pre>
- name@mapset
- </pre></div>
- <p>For example, the following is a legal expression:
- <p><div class="code"><pre>
- result = x@PERMANENT / y@SOILS
- </pre></div>
- <p>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 <em>r3.mapcalc</em>.)
- <p>
- <h2>THE NEIGHBORHOOD MODIFIER</h2>
- 3D grids are data base files stored in voxel format, i.e., three-dimensional
- matrices of float/double values. In <em>r3.mapcalc</em>, 3D grids may be
- followed by a <em>neighborhood</em> modifier that specifies a relative offset
- from the current cell being evaluated. The format is
- <em>map[r,c,d]</em>,
- where <em>r</em> is the row offset, <em>c</em> is the column offset and <em>d</em>
- is the depth offset. For example, <em>map[1,2,3]</em> refers to the cell
- one row below, two columns to the right and 3 levels below of the current
- cell, <em>map[-3,-2,-1]</em> refers to the cell three rows above, two columns
- to the left and one level below of the current cell, and <em>map[0,1,0]</em>
- 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 3D
- grid or across multiple 3D grids.
- <p>
- <h2>FUNCTIONS</h2>
- The functions currently supported are listed in the table below.
- The type of the result is indicated in the last column.
- <em>F</em>
- means that the functions always results in a floating point value,
- <em>I</em>
- means that the function gives an integer result, and
- <em>*</em>
- 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.
- <p><div class="code"><pre>
- function description type
- ---------------------------------------------------------------------------
- abs(x) return absolute value of x *
- acos(x) inverse cosine of x (result is in degrees) F
- asin(x) inverse sine of x (result is in degrees) F
- atan(x) inverse tangent of x (result is in degrees) F
- atan(x,y) inverse tangent of y/x (result is in degrees) F
- cos(x) cosine of x (x is in degrees) F
- double(x) convert x to double-precision floating point F
- eval([x,y,...,]z) evaluate values of listed expr, pass results to z
- exp(x) exponential function of x F
- exp(x,y) x to the power y F
- float(x) convert x to single-precision floating point F
- graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
- if decision options: *
- if(x) 1 if x not zero, 0 otherwise
- if(x,a) a if x not zero, 0 otherwise
- if(x,a,b) a if x not zero, b otherwise
- if(x,a,b,c) a if x > 0, b if x is zero, c if x < 0
- int(x) convert x to integer [ truncates ] I
- isnull(x) check if x = NULL
- log(x) natural log of x F
- log(x,b) log of x base b F
- max(x,y[,z...]) largest value of those listed *
- median(x,y[,z...]) median value of those listed *
- min(x,y[,z...]) smallest value of those listed *
- mode(x,y[,z...]) mode value of those listed *
- nmax(x,y[,z...]) largest value of those listed, excluding NULLs *
- nmedian(x,y[,z...]) median value of those listed, excluding NULLs *
- nmin(x,y[,z...]) smallest value of those listed, excluding NULLs *
- nmode(x,y[,z...]) mode value of those listed, excluding NULLs *
- not(x) 1 if x is zero, 0 otherwise
- pow(x,y) x to the power y *
- rand(a,b) random value x : a <= x < b
- round(x) round x to nearest integer I
- sin(x) sine of x (x is in degrees) F
- sqrt(x) square root of x F
- tan(x) tangent of x (x is in degrees) F
- xor(x,y) exclusive-or (XOR) of x and y I
- </pre></div>
- <div class="code"><pre>
- Internal variables:
- row() current row of moving window
- col() current col of moving window
- depth() return current depth
- x() current x-coordinate of moving window
- y() current y-coordinate of moving window
- z() return current z value
- ewres() current east-west resolution
- nsres() current north-south resolution
- tbres() current top-bottom resolution
- null() NULL value
- </pre></div>
- Note, that the row(), col() and depth() indexing starts with 1.
- <h2>FLOATING POINT VALUES IN THE EXPRESSION</h2>
- Floating point numbers are allowed in the expression. A floating point
- number is a number which contains a decimal point:
- <div class="code"><pre>
- 2.3 12.0 12. .81
- </pre></div>
- 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.
- <p>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).
- <p>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():
- <div class="code"><pre>
- r3.mapcalc "ratio = float(soil.4 - soil.3) / soil.3)"
- </pre></div>
- <h2>NULL support</h2>
- <ul>
- <li>Division by zero should result in NULL.
- <li>Modulus by zero should result in NULL.
- <li>NULL-values in any arithmetic or logical operation should result
- in NULL. (however, &&& and ||| are treated specially, as described below).
- <li>The &&& and ||| operators observe the following axioms even when x is NULL:
- <div class="code"><pre>
- x &&& false == false
- false &&& x == false
- x ||| true == true
- true ||| x == true
- </pre></div>
- <li>NULL-values in function arguments should result in NULL (however,
- if(), eval() and isnull() are treated specially, as described below).
- <li>The eval() function always returns its last argument
- <li>The situation for if() is:
- <div class="code"><pre>
- if(x)
- NULL if x is NULL; 0 if x is zero; 1 otherwise
- if(x,a)
- NULL if x is NULL; a if x is non-zero; 0 otherwise
- if(x,a,b)
- NULL if x is NULL; a if x is non-zero; b otherwise
- if(x,n,z,p)
- NULL if x is NULL; n if x is negative;
- z if x is zero; p if x is positive
- </pre></div>
- <li>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.
- <li>Non-NULL, but invalid, arguments to functions should result in NULL.
- <div class="code"><pre>
- Examples:
- log(-2)
- sqrt(-2)
- pow(a,b) where a is negative and b is not an integer
- </pre></div>
- </ul>
- <p>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.
- <p>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:<p><div class="code"><pre>
- C=A + if(isnull(B),0,B)
- </pre></div>
- <p><b>NULL and conditions:</b>
- <p>For the one argument form:
- <div class="code"><pre>
- if(x) = NULL if x is NULL
- if(x) = 0 if x = 0
- if(x) = 1 otherwise (i.e. x is neither NULL nor 0).
- </pre></div>
- <p>For the two argument form:
- <div class="code"><pre>
- if(x,a) = NULL if x is NULL
- if(x,a) = 0 if x = 0
- if(x,a) = a otherwise (i.e. x is neither NULL nor 0).
- </pre></div>
- <p>For the three argument form:
- <div class="code"><pre>
- if(x,a,b) = NULL if x is NULL
- if(x,a,b) = b if x = 0
- if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0).
- </pre></div>
- <p>For the four argument form:
- <div class="code"><pre>
- if(x,a,b,c) = NULL if x is NULL
- if(x,a,b,c) = a if x > 0
- if(x,a,b,c) = b if x = 0
- if(x,a,b,c) = c if x < 0
- </pre></div>
- 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.
- <p>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.:
- <div class="code"><pre>
- if(0,a,b) = b regardless of whether a is NULL
- if(1,a,b) = a regardless of whether b is NULL
- </pre></div>
- eval() always returns its last argument, so it only returns NULL if
- the last argument is NULL.
- <p><b>Note</b>: 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
- "x == 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.
- <h2>EXAMPLES</h2>
- To compute the average of two 3D grids
- <em>a</em> and <em>b</em>:
- <div class="code"><pre>
- ave = (a + b)/2
- </pre></div>
- To form a weighted average:
- <div class="code"><pre>
- ave = (5*a + 3*b)/8.0
- </pre></div>
- To produce a binary representation of 3D grid
- <em>a</em> so that category 0 remains 0 and all other categories become 1:
- <div class="code"><pre>
- mask = a != 0
- </pre></div>
- This could also be accomplished by:
- <div class="code"><pre>
- mask = if(a)
- </pre></div>
- To mask 3D grid <em>b</em> by 3D grid <em>a</em>:
- <div class="code"><pre>
- result = if(a,b)
- </pre></div>
- To change all values below 5 to NULL:
- <div class="code"><pre>
- newmap = if(map<5, null(), 5)
- </pre></div>
- The graph function allows users to specify a x-y conversion using
- pairs of x,y coordinates.
- In some situations a transformation from one value to another is not
- easily established mathematically, but can be represented by a 2-D
- graph. The graph() function provides the opportunity to accomplish
- this. An x-axis value is provided to the graph function along with
- the associated graph represented by a series of x,y pairs. The x
- values must be monotonically increasing (each larger than or equal to
- the previous). The graph function linearly interpolates between
- pairs. Any x value lower the lowest x value (i.e. first) will have
- the associated y value returned. Any x value higher than the last
- will similarly have the associated y value returned. Consider the
- request:
- <div class="code"><pre>
- newmap = graph(map, 1,10, 2,25, 3,50)
- </pre></div>
- X (map) values supplied and y (newmap) values returned:
- <div class="code"><pre>
- 0, 10
- 1, 10,
- 1.5, 16.5
- 2.9, 47.5
- 4, 50
- 100, 50
- </pre></div>
- <h2>NOTES</h2>
- 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:
- <p>* ( ) > & |
- <p>It is advisable to put single quotes around the expression; e.g.:
- <div class="code"><pre>
- 'result = elevation * 2'
- </pre></div>
- Without the quotes, the *, which has special meaning to the UNIX shell,
- would be altered and <em>r3.mapcalc</em> would see something other than the *.
- <p>For formulas that the user enters from standard input (rather than from
- the command line), a line continuation feature now exists. If the user
- adds \e to the end of an input line, <em>r3.mapcalc</em> assumes that the
- formula being entered by the user continues on to the next input line.
- There is no limit to the possible number of input lines or to the length
- of a formula.
- <p>If the <em>r3.mapcalc</em> formula entered by the user is very long, the
- map title will contain only some of it, but most (if not all) of the formula
- will be placed into the history file for the <em>result</em> map.
- <p>The environment variable GRASS_RND_SEED is read to initialise the
- random number generator.
- <h2>BUGS</h2>
- Continuation lines must end with a \ and have NO trailing white space
- (blanks or tabs). If the user does leave white space at the end of
- continuation lines, the error messages produced by <em>r3.mapcalc</em> will
- be meaningless and the equation will not work as the user intended.
- This is important for the eval() function.
- <p><!-- STILL TRUE ??-->
- Currently, there is no comment mechanism in <em>r3.mapcalc</em>. Perhaps
- adding a capability that would cause the entire line to be ignored when
- the user inserted a # at the start of a line as if it were not present,
- would do the trick.
- <p>The function should require the user to type "end" or "exit" instead
- of simply a blank line. This would make separation of multiple scripts
- separable by white space.
- <p>r3.mapcalc does not print a warning in case of operations on NULL cells.
- It is left to the user to utilize the isnull() function.
- <h2>SEE ALSO</h2>
- <b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc-algebra.pdf">r3.mapcalc: An Algebra for GIS and Image
- Processing</a></b>, by Michael Shapiro and Jim Westervelt, U.S. Army
- Construction Engineering Research Laboratory (March/1991).
- <p>
- <b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc.pdf">Performing Map Calculations on GRASS Data:
- r.mapcalc Program Tutorial</a></b>, by Marji Larson, Michael Shapiro and Scott
- Tweddale, U.S. Army Construction Engineering Research Laboratory (December
- 1991)
- <p><em><a href="r.mapcalc.html">r.mapcalc</a></em>
- <h2>AUTHORS</h2>
- Tomas Paudits & Jaro Hofierka, funded by GeoModel s.r.o., Slovakia
- <br><a href="mailto:tpaudits@mailbox.sk">tpaudits@mailbox.sk</a>,
- <a href="MAILTO:hofierka@geomodel.sk">hofierka@geomodel.sk</a>
- <p><i>Last changed: $Date$</i>
|