| 1 |
<h2>DESCRIPTION</h2> |
| 2 |
|
| 3 |
<em>r3.mapcalc</em> performs arithmetic on raster map layers. |
| 4 |
New raster map layers can be created which are arithmetic expressions |
| 5 |
involving existing raster map layers, integer or floating point constants, |
| 6 |
and functions. |
| 7 |
|
| 8 |
<h3>Program use</h3> |
| 9 |
|
| 10 |
<em>r3.mapcalc</em> expression have the form: |
| 11 |
<p><b>result =</b><em> expression</em> |
| 12 |
<p>where <em>result</em> is the name of a raster map layer |
| 13 |
to contain the result of the calculation and |
| 14 |
<b>expression</b> is any legal arithmetic expression involving existing |
| 15 |
raster map layers (except <em>result</em> itself), integer or floating point constants, |
| 16 |
and functions known to the calculator. |
| 17 |
Parentheses are allowed in the expression and may be nested to any depth. |
| 18 |
<em>result</em> will be created in the user's current mapset. |
| 19 |
<p> |
| 20 |
As <b>expression=</b> is the first option, it is the default. This |
| 21 |
means that passing an expression on the command line is possible |
| 22 |
as long as the expression is quoted and a space is included before the |
| 23 |
first <em>=</em> sign. |
| 24 |
|
| 25 |
Example ('foo' is the resulting map): |
| 26 |
<div class="code"><pre> |
| 27 |
r3.mapcalc "foo = 1" |
| 28 |
</pre></div> |
| 29 |
or: |
| 30 |
<div class="code"><pre> |
| 31 |
r3.mapcalc 'foo = 1' |
| 32 |
</pre></div> |
| 33 |
|
| 34 |
An unquoted expression (i.e. split over multiple arguments) won't |
| 35 |
work, nor will omitting the space before the = sign: |
| 36 |
|
| 37 |
<div class="code"><pre> |
| 38 |
r3.mapcalc 'foo=1' |
| 39 |
Sorry, <foo> is not a valid parameter |
| 40 |
</pre></div> |
| 41 |
|
| 42 |
<!-- sync to r.mapcalc? --> |
| 43 |
If no options are given, it manufactures "file=-" (which reads from |
| 44 |
stdin), so you can continue to use e.g.: |
| 45 |
|
| 46 |
<div class="code"><pre> |
| 47 |
r3.mapcalc < file |
| 48 |
</pre></div> |
| 49 |
or: |
| 50 |
<div class="code"><pre> |
| 51 |
r3.mapcalc <<EOF |
| 52 |
foo = 1 |
| 53 |
EOF |
| 54 |
</pre></div> |
| 55 |
But unless you need compatibility with previous versions, use file= |
| 56 |
explicitly, e.g.: |
| 57 |
|
| 58 |
<div class="code"><pre> |
| 59 |
r3.mapcalc file=file |
| 60 |
</pre></div> |
| 61 |
or: |
| 62 |
<div class="code"><pre> |
| 63 |
r3.mapcalc file=- <<EOF |
| 64 |
foo = 1 |
| 65 |
EOF |
| 66 |
</pre></div> |
| 67 |
|
| 68 |
<p>The formula entered to <em>r3.mapcalc</em> by the user is recorded both in the |
| 69 |
<em>result</em> map title (which appears in the category file for <em>result</em>) |
| 70 |
and in the history file for <em>result</em>. |
| 71 |
<p>Some characters have special meaning to the command shell. If the user |
| 72 |
is entering input to <em>r3.mapcalc</em> on the command line, expressions |
| 73 |
should be enclosed within single quotes. See NOTES, below. |
| 74 |
|
| 75 |
<h3>Operators and order of precedence</h3> |
| 76 |
|
| 77 |
The following operators are supported: |
| 78 |
<div class="code"><pre> |
| 79 |
Operator Meaning Type Precedence |
| 80 |
-------------------------------------------------------------- |
| 81 |
- negation Arithmetic 12 |
| 82 |
~ one's complement Bitwise 12 |
| 83 |
! not Logical 12 |
| 84 |
^ exponentiation Arithmetic 11 |
| 85 |
% modulus Arithmetic 10 |
| 86 |
/ division Arithmetic 10 |
| 87 |
* multiplication Arithmetic 10 |
| 88 |
+ addition Arithmetic 9 |
| 89 |
- subtraction Arithmetic 9 |
| 90 |
<< left shift Bitwise 8 |
| 91 |
>> right shift Bitwise 8 |
| 92 |
>>> right shift (unsigned) Bitwise 8 |
| 93 |
> greater than Logical 7 |
| 94 |
>= greater than or equal Logical 7 |
| 95 |
< less than Logical 7 |
| 96 |
<= less than or equal Logical 7 |
| 97 |
== equal Logical 6 |
| 98 |
!= not equal Logical 6 |
| 99 |
& bitwise and Bitwise 5 |
| 100 |
| bitwise or Bitwise 4 |
| 101 |
&& logical and Logical 3 |
| 102 |
&&& logical and[1] Logical 3 |
| 103 |
|| logical or Logical 2 |
| 104 |
||| logical or[1] Logical 2 |
| 105 |
?: conditional Logical 1 |
| 106 |
</pre></div> |
| 107 |
(modulus is the remainder upon division) |
| 108 |
<p>[1] The &&& and ||| operators handle null values differently to other |
| 109 |
operators. See the section entitled <b>NULL support</b> below for more |
| 110 |
details. |
| 111 |
<p>The operators are applied from left to right, with those of higher precedence |
| 112 |
applied before those with lower precedence. |
| 113 |
Division by 0 and modulus by 0 are acceptable and give a NULL result. |
| 114 |
The logical operators give a 1 result if the comparison is true, 0 otherwise. |
| 115 |
<p><p> |
| 116 |
|
| 117 |
<h3>3D Grid names</h3> |
| 118 |
|
| 119 |
Anything in the expression which is not a number, operator, or function name |
| 120 |
is taken to be a 3D grid name. |
| 121 |
Examples: |
| 122 |
<p><div class="code"><pre> |
| 123 |
volume |
| 124 |
x3 |
| 125 |
3d.his |
| 126 |
</pre></div> |
| 127 |
<p>Most GRASS raster map layers and 3D grids meet this naming convention. |
| 128 |
However, if a 3D grid has a name which conflicts with the above rule, it |
| 129 |
should be quoted. For example, the expression |
| 130 |
<p><div class="code"><pre> |
| 131 |
x = a-b |
| 132 |
</pre></div> |
| 133 |
<p>would be interpreted as: x equals a minus b, whereas |
| 134 |
<p><div class="code"><pre> |
| 135 |
x = "a-b" |
| 136 |
</pre></div> |
| 137 |
<p>would be interpreted as: x equals the 3D grid named <em>a-b</em> |
| 138 |
<p>Also |
| 139 |
<p><div class="code"><pre> |
| 140 |
x = 3107 |
| 141 |
</pre></div> |
| 142 |
<p>would create <em>x</em> filled with the number 3107, while |
| 143 |
<p><div class="code"><pre> |
| 144 |
x = "3107" |
| 145 |
</pre></div> |
| 146 |
<p>would copy the 3D grid <em>3107</em> to the 3D grid <em>x</em>. |
| 147 |
|
| 148 |
<p>Quotes are not required unless the 3D grid names look like numbers or |
| 149 |
contain operators, OR unless the program is run non-interactively. Examples |
| 150 |
given here assume the program is run interactively. See NOTES, below. |
| 151 |
<p><em>r3.mapcalc</em> will look for the 3D grids according to the user's |
| 152 |
current mapset search path. It is possible to override the search path |
| 153 |
and specify the mapset from which to select the 3D grid. This is done by |
| 154 |
specifying the 3D grid name in the form: |
| 155 |
<p><div class="code"><pre> |
| 156 |
name@mapset |
| 157 |
</pre></div> |
| 158 |
<p>For example, the following is a legal expression: |
| 159 |
<p><div class="code"><pre> |
| 160 |
result = x@PERMANENT / y@SOILS |
| 161 |
</pre></div> |
| 162 |
<p>The mapset specified does not have to be in the mapset search path. |
| 163 |
(This method of overriding the mapset search path is common to all |
| 164 |
GRASS commands, not just <em>r3.mapcalc</em>.) |
| 165 |
<p> |
| 166 |
|
| 167 |
<h3>The neighborhood modifier</h3> |
| 168 |
|
| 169 |
3D grids are data base files stored in voxel format, i.e., three-dimensional |
| 170 |
matrices of float/double values. In <em>r3.mapcalc</em>, 3D grids may be |
| 171 |
followed by a <em>neighborhood</em> modifier that specifies a relative offset |
| 172 |
from the current cell being evaluated. The format is |
| 173 |
<em>map[r,c,d]</em>, |
| 174 |
where <em>r</em> is the row offset, <em>c</em> is the column offset and <em>d</em> |
| 175 |
is the depth offset. For example, <em>map[1,2,3]</em> refers to the cell |
| 176 |
one row below, two columns to the right and 3 levels below of the current |
| 177 |
cell, <em>map[-3,-2,-1]</em> refers to the cell three rows above, two columns |
| 178 |
to the left and one level below of the current cell, and <em>map[0,1,0]</em> |
| 179 |
refers to the cell one column to the right of the current cell. This syntax |
| 180 |
permits the development of neighborhood-type filters within a single 3D |
| 181 |
grid or across multiple 3D grids. |
| 182 |
|
| 183 |
<h3>Functions</h3> |
| 184 |
|
| 185 |
The functions currently supported are listed in the table below. |
| 186 |
The type of the result is indicated in the last column. |
| 187 |
<em>F</em> means that the functions always results in a floating point value, |
| 188 |
<em>I</em> means that the function gives an integer result, and |
| 189 |
<em>*</em> indicates that the result is float if any of the arguments to the |
| 190 |
function are floating point values and integer if all arguments are integer. |
| 191 |
|
| 192 |
<p><div class="code"><pre> |
| 193 |
function description type |
| 194 |
--------------------------------------------------------------------------- |
| 195 |
abs(x) return absolute value of x * |
| 196 |
acos(x) inverse cosine of x (result is in degrees) F |
| 197 |
asin(x) inverse sine of x (result is in degrees) F |
| 198 |
atan(x) inverse tangent of x (result is in degrees) F |
| 199 |
atan(x,y) inverse tangent of y/x (result is in degrees) F |
| 200 |
ceil(x) the smallest integral value not less than x * |
| 201 |
cos(x) cosine of x (x is in degrees) F |
| 202 |
double(x) convert x to double-precision floating point F |
| 203 |
eval([x,y,...,]z) evaluate values of listed expr, pass results to z |
| 204 |
exp(x) exponential function of x F |
| 205 |
exp(x,y) x to the power y F |
| 206 |
float(x) convert x to single-precision floating point F |
| 207 |
floor(x) the largest integral value not greater than x * |
| 208 |
graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F |
| 209 |
graph2(x,x1[,x2,..],y1[,y2..]) |
| 210 |
alternative form of graph() F |
| 211 |
if decision options: * |
| 212 |
if(x) 1 if x not zero, 0 otherwise |
| 213 |
if(x,a) a if x not zero, 0 otherwise |
| 214 |
if(x,a,b) a if x not zero, b otherwise |
| 215 |
if(x,a,b,c) a if x > 0, b if x is zero, c if x < 0 |
| 216 |
int(x) convert x to integer [ truncates ] I |
| 217 |
isnull(x) check if x = NULL |
| 218 |
log(x) natural log of x F |
| 219 |
log(x,b) log of x base b F |
| 220 |
max(x,y[,z...]) largest value of those listed * |
| 221 |
median(x,y[,z...]) median value of those listed * |
| 222 |
min(x,y[,z...]) smallest value of those listed * |
| 223 |
mod(x,y) return the modulus (the remainder) of x/y * |
| 224 |
mode(x,y[,z...]) mode value of those listed * |
| 225 |
nmax(x,y[,z...]) largest value of those listed, excluding NULLs * |
| 226 |
nmedian(x,y[,z...]) median value of those listed, excluding NULLs * |
| 227 |
nmin(x,y[,z...]) smallest value of those listed, excluding NULLs * |
| 228 |
nmode(x,y[,z...]) mode value of those listed, excluding NULLs * |
| 229 |
not(x) 1 if x is zero, 0 otherwise |
| 230 |
pow(x,y) x to the power y * |
| 231 |
rand(a,b) random value x : a <= x < b * |
| 232 |
round(x) round x to nearest integer I |
| 233 |
round(x,y) round x to nearest multiple of y |
| 234 |
round(x,y,z) round x to nearest y*i+z for some integer i |
| 235 |
sin(x) sine of x (x is in degrees) F |
| 236 |
sqrt(x) square root of x F |
| 237 |
tan(x) tangent of x (x is in degrees) F |
| 238 |
xor(x,y) exclusive-or (XOR) of x and y I |
| 239 |
</pre></div> |
| 240 |
|
| 241 |
<div class="code"><pre> |
| 242 |
Internal variables: |
| 243 |
row() current row of moving window I |
| 244 |
col() current col of moving window I |
| 245 |
depth() return current depth I |
| 246 |
nrows() number of rows in computation region I |
| 247 |
ncols() number of columns in computation region I |
| 248 |
ndepths() number of depth levels in computation region I |
| 249 |
x() current x-coordinate of moving window F |
| 250 |
y() current y-coordinate of moving window F |
| 251 |
z() return current z value F |
| 252 |
ewres() current east-west resolution F |
| 253 |
nsres() current north-south resolution F |
| 254 |
tbres() current top-bottom resolution F |
| 255 |
area() area of current cell in square meters F |
| 256 |
null() NULL value |
| 257 |
</pre></div> |
| 258 |
Note, that the row(), col() and depth() indexing starts with 1. |
| 259 |
|
| 260 |
<h3>Floating point values in the expression</h3> |
| 261 |
|
| 262 |
Floating point numbers are allowed in the expression. A floating point |
| 263 |
number is a number which contains a decimal point: |
| 264 |
<div class="code"><pre> |
| 265 |
2.3 12.0 12. .81 |
| 266 |
</pre></div> |
| 267 |
Floating point values in the expression are handled in a special way. |
| 268 |
With arithmetic and logical operators, if either operand is float, |
| 269 |
the other is converted to float and the result of the operation is float. |
| 270 |
This means, in particular that division of integers results in a |
| 271 |
(truncated) integer, while division of floats results in an accurate |
| 272 |
floating point value. With functions of type * (see table above), |
| 273 |
the result is float if any argument is float, integer otherwise. |
| 274 |
<p>Note: If you calculate with integer numbers, the resulting map will |
| 275 |
be integer. If you want to get a float result, add the decimal point |
| 276 |
to integer number(s). |
| 277 |
<p>If you want floating point division, at least one of the arguments has |
| 278 |
to be a floating point value. Multiplying one of them by 1.0 will |
| 279 |
produce a floating-point result, as will using float(): |
| 280 |
<div class="code"><pre> |
| 281 |
r3.mapcalc "ratio = float(soil.4 - soil.3) / soil.3)" |
| 282 |
</pre></div> |
| 283 |
|
| 284 |
<h3>NULL support</h3> |
| 285 |
|
| 286 |
<ul> |
| 287 |
<li>Division by zero should result in NULL.</li> |
| 288 |
<li>Modulus by zero should result in NULL.</li> |
| 289 |
<li>NULL-values in any arithmetic or logical operation should result |
| 290 |
in NULL. (however, &&& and ||| are treated specially, as described below).</li> |
| 291 |
<li>The &&& and ||| operators observe the following axioms even when x is NULL: |
| 292 |
<div class="code"><pre> |
| 293 |
x &&& false == false |
| 294 |
false &&& x == false |
| 295 |
x ||| true == true |
| 296 |
true ||| x == true |
| 297 |
</pre></div></li> |
| 298 |
<li>NULL-values in function arguments should result in NULL (however, |
| 299 |
if(), eval() and isnull() are treated specially, as described below).</li> |
| 300 |
<li>The eval() function always returns its last argument</li> |
| 301 |
<li>The situation for if() is: |
| 302 |
<div class="code"><pre> |
| 303 |
if(x) |
| 304 |
NULL if x is NULL; 0 if x is zero; 1 otherwise |
| 305 |
if(x,a) |
| 306 |
NULL if x is NULL; a if x is non-zero; 0 otherwise |
| 307 |
if(x,a,b) |
| 308 |
NULL if x is NULL; a if x is non-zero; b otherwise |
| 309 |
if(x,n,z,p) |
| 310 |
NULL if x is NULL; n if x is negative; |
| 311 |
z if x is zero; p if x is positive |
| 312 |
</pre></div></li> |
| 313 |
<li>The (new) function isnull(x) returns: 1 if x is NULL; |
| 314 |
0 otherwise. The (new) function null() |
| 315 |
(which has no arguments) returns an integer NULL.</li> |
| 316 |
<li>Non-NULL, but invalid, arguments to functions should result in NULL. |
| 317 |
<div class="code"><pre> |
| 318 |
Examples: |
| 319 |
log(-2) |
| 320 |
sqrt(-2) |
| 321 |
pow(a,b) where a is negative and b is not an integer |
| 322 |
</pre></div></li> |
| 323 |
</ul> |
| 324 |
|
| 325 |
<p>NULL support: Please note that any math performed with NULL cells always |
| 326 |
results in a NULL value for these cells. If you want to replace a NULL cell |
| 327 |
on-the-fly, use the isnull() test function in a if-statement. |
| 328 |
<p>Example: The users wants the NULL-valued cells to be treated like zeros. To |
| 329 |
add maps A and B (where B contains NULLs) to get a map C the user can use a |
| 330 |
construction like:<p><div class="code"><pre> |
| 331 |
C = A + if(isnull(B),0,B) |
| 332 |
</pre></div> |
| 333 |
<p><b>NULL and conditions:</b> |
| 334 |
<p>For the one argument form: |
| 335 |
<div class="code"><pre> |
| 336 |
if(x) = NULL if x is NULL |
| 337 |
if(x) = 0 if x = 0 |
| 338 |
if(x) = 1 otherwise (i.e. x is neither NULL nor 0). |
| 339 |
</pre></div> |
| 340 |
<p>For the two argument form: |
| 341 |
<div class="code"><pre> |
| 342 |
if(x,a) = NULL if x is NULL |
| 343 |
if(x,a) = 0 if x = 0 |
| 344 |
if(x,a) = a otherwise (i.e. x is neither NULL nor 0). |
| 345 |
</pre></div> |
| 346 |
<p>For the three argument form: |
| 347 |
<div class="code"><pre> |
| 348 |
if(x,a,b) = NULL if x is NULL |
| 349 |
if(x,a,b) = b if x = 0 |
| 350 |
if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0). |
| 351 |
</pre></div> |
| 352 |
<p>For the four argument form: |
| 353 |
<div class="code"><pre> |
| 354 |
if(x,a,b,c) = NULL if x is NULL |
| 355 |
if(x,a,b,c) = a if x > 0 |
| 356 |
if(x,a,b,c) = b if x = 0 |
| 357 |
if(x,a,b,c) = c if x < 0 |
| 358 |
</pre></div> |
| 359 |
More generally, all operators and most functions return NULL if *any* |
| 360 |
of their arguments are NULL. |
| 361 |
<br> |
| 362 |
The functions if(), isnull() and eval() are exceptions. |
| 363 |
<br> |
| 364 |
The function isnull() returns 1 if its argument is NULL and 0 otherwise. |
| 365 |
If the user wants the opposite, the ! operator, e.g. "!isnull(x)" must be |
| 366 |
used. |
| 367 |
<p>All forms of if() return NULL if the first argument is NULL. The 2, 3 |
| 368 |
and 4 argument forms of if() return NULL if the "selected" argument is |
| 369 |
NULL, e.g.: |
| 370 |
<div class="code"><pre> |
| 371 |
if(0,a,b) = b regardless of whether a is NULL |
| 372 |
if(1,a,b) = a regardless of whether b is NULL |
| 373 |
</pre></div> |
| 374 |
eval() always returns its last argument, so it only returns NULL if |
| 375 |
the last argument is NULL. |
| 376 |
<p><b>Note</b>: The user cannot test for NULL using the == operator, as that |
| 377 |
returns NULL if either or both arguments are NULL, i.e. if x and y are |
| 378 |
both NULL, then "x == y" and "x != y" are both NULL rather than 1 and |
| 379 |
0 respectively. |
| 380 |
<br> |
| 381 |
The behaviour makes sense if the user considers NULL as representing an |
| 382 |
unknown quantity. E.g. if x and y are both unknown, then the values of |
| 383 |
"x == y" and "x != y" are also unknown; if they both have unknown |
| 384 |
values, the user doesn't know whether or not they both have the same value. |
| 385 |
|
| 386 |
<h2>NOTES</h2> |
| 387 |
|
| 388 |
<h3>Usage from command line</h3> |
| 389 |
|
| 390 |
Extra care must be taken if the expression is given on the command line. |
| 391 |
Some characters have special meaning to the UNIX shell. |
| 392 |
These include, among others: |
| 393 |
<div class="code"><pre> |
| 394 |
* ( ) > & | |
| 395 |
</pre></div> |
| 396 |
<p>It is advisable to put single quotes around the expression; e.g.: |
| 397 |
<div class="code"><pre> |
| 398 |
'result = volume * 2' |
| 399 |
</pre></div> |
| 400 |
Without the quotes, the <code>*</code>, which has special meaning to the UNIX shell, |
| 401 |
would be altered and <em>r3.mapcalc</em> would see something other than the <code>*</code>. |
| 402 |
|
| 403 |
<h3>Multiple computations</h3> |
| 404 |
<p> |
| 405 |
In general, it's preferable to do as much as possible in each |
| 406 |
r3.mapcalc command using multi-line input. |
| 407 |
|
| 408 |
<h3>Backwards compatibility</h3> |
| 409 |
|
| 410 |
For the backwards compatibility with GRASS 6, |
| 411 |
<!-- check wording: --> |
| 412 |
if no options are given, it manufactures <code>file=-</code> (which reads from |
| 413 |
stdin), so you can continue to use e.g.: |
| 414 |
<div class="code"><pre> |
| 415 |
r3.mapcalc < file |
| 416 |
</pre></div> |
| 417 |
or: |
| 418 |
<div class="code"><pre> |
| 419 |
r3.mapcalc <<EOF |
| 420 |
foo = 1 |
| 421 |
EOF |
| 422 |
</pre></div> |
| 423 |
But unless you need compatibility with previous GRASS versions, use <code>file=</code> |
| 424 |
explicitly, as stated above. |
| 425 |
<p> |
| 426 |
When the map name contains uppercase letter(s) or a dot which are not |
| 427 |
allowed to be in module option names, the <em>r3.mapcalc</em> command will |
| 428 |
be valid also without quotes: |
| 429 |
<div class="code"><pre> |
| 430 |
r3.mapcalc volume_A=1 |
| 431 |
r3.mapcalc volume.1=1 |
| 432 |
</pre></div> |
| 433 |
However, this syntax is not recommended as quotes as stated above more safe. |
| 434 |
Using quotes is both backwards compatible and valid in future. |
| 435 |
|
| 436 |
<h3>Interactive input in command line</h3> |
| 437 |
|
| 438 |
For formulas that the user enters from standard input |
| 439 |
(rather than from the command line), a line continuation feature now exists. |
| 440 |
If the user adds a backslash to the end of an input line, <em>r3.mapcalc</em> assumes that |
| 441 |
the formula being entered by the user continues on to the next input line. |
| 442 |
There is no limit to the possible number of input lines |
| 443 |
or to the length of a formula. |
| 444 |
<p>If the <em>r3.mapcalc</em> formula entered by the user is very long, |
| 445 |
the map title will contain only some of it, but most (if not all) of |
| 446 |
the formula will be placed into the history file for the <em>result</em> map. |
| 447 |
<p>When the user enters input to <em>r3.mapcalc</em> non-interactively on |
| 448 |
the command line, the program will not warn the user not to overwrite |
| 449 |
existing map layers. Users should therefore take care to assign program |
| 450 |
outputs raster map names that do not yet exist in their current mapsets. |
| 451 |
|
| 452 |
<h3>3D GRID mask handling</h3> |
| 453 |
<p> |
| 454 |
<em>r3.mapcalc</em> follows the common GRASS behavior of raster mask handling, |
| 455 |
so the mask is only applied when reading an existing GRASS raster map. |
| 456 |
This implies that, for example, the command: |
| 457 |
<div class="code"><pre> |
| 458 |
r3.mapcalc "volume_amplified = volume * 3" |
| 459 |
</pre></div> |
| 460 |
create a map with NULL cells for the masked-out cells if raster mask is active. |
| 461 |
|
| 462 |
<p> |
| 463 |
However, when creating a map which is not based on any map, |
| 464 |
e.g. a map from a constant: |
| 465 |
<div class="code"><pre> |
| 466 |
r3.mapcalc "volume_const = 200.0" |
| 467 |
</pre></div> |
| 468 |
the created raster map is limited only by a computation region |
| 469 |
but it is not affected by an active raster mask. |
| 470 |
This is expected because, as mentioned above, the mask is only applied |
| 471 |
when reading, not when writing a raster map. |
| 472 |
If the raster mask should be applied in this case, an <code>if()</code> |
| 473 |
function including the mask raster should be used, e.g. |
| 474 |
(assuming the mask is called <code>MASK</code>): |
| 475 |
<div class="code"><pre> |
| 476 |
r3.mapcalc "volume_const = if(MASK, 200.0, null())" |
| 477 |
</pre></div> |
| 478 |
|
| 479 |
<p> |
| 480 |
When testing expressions related to mask handling keep in mind that |
| 481 |
when the raster mask is active, |
| 482 |
you don't see data in masked areas even if they are not NULL. |
| 483 |
See <em><a href="r3.mask.html">r3.mask</a></em> for details. |
| 484 |
|
| 485 |
<h3>Random number generator initialization</h3> |
| 486 |
<p>The pseudo-random number generator used by the rand() function can |
| 487 |
be initialised to a specific value using the <b>seed</b> option. |
| 488 |
This can be used to replicate a previous calculation. |
| 489 |
<p>Alternatively, it can be initialised from the system time and the |
| 490 |
PID using the <b>-r</b> flag. This should result in a different seed |
| 491 |
being used each time. |
| 492 |
<p>In either case, the seed will be written to the map's history, and |
| 493 |
can be seen using <em>r.info</em>. |
| 494 |
<p>If you want other people to be able to verify your results, it's |
| 495 |
preferable to use the <b>seed</b> option to supply a seed which is |
| 496 |
either specified in the script or generated from a deterministic process |
| 497 |
such as a pseudo-random number generator given an explicit seed. |
| 498 |
<p>Note that the rand() function will generate a fatal error if neither |
| 499 |
the <b>seed</b> option nor the <b>-s</b> flag are given. |
| 500 |
|
| 501 |
<h2>EXAMPLES</h2> |
| 502 |
|
| 503 |
To compute the average of two 3D grids |
| 504 |
<em>a</em> and <em>b</em>: |
| 505 |
<div class="code"><pre> |
| 506 |
ave = (a + b)/2 |
| 507 |
</pre></div> |
| 508 |
|
| 509 |
<p> |
| 510 |
To form a weighted average: |
| 511 |
<div class="code"><pre> |
| 512 |
ave = (5*a + 3*b)/8.0 |
| 513 |
</pre></div> |
| 514 |
<p> |
| 515 |
To produce a binary representation of the 3D grid |
| 516 |
<em>a</em> so that category 0 remains 0 and all other categories become 1: |
| 517 |
<div class="code"><pre> |
| 518 |
mask = a != 0 |
| 519 |
</pre></div> |
| 520 |
This could also be accomplished by: |
| 521 |
<div class="code"><pre> |
| 522 |
mask = if(a) |
| 523 |
</pre></div> |
| 524 |
|
| 525 |
<p> |
| 526 |
To mask 3D grid <em>b</em> by 3D grid <em>a</em>: |
| 527 |
<div class="code"><pre> |
| 528 |
result = if(a,b) |
| 529 |
</pre></div> |
| 530 |
|
| 531 |
<p> |
| 532 |
To change all values below 5 to NULL, keep value otherwise: |
| 533 |
<div class="code"><pre> |
| 534 |
newmap = if(map < 5, null(), map) |
| 535 |
</pre></div> |
| 536 |
The graph() function allows users to specify a x-y conversion using |
| 537 |
pairs of x,y coordinates. |
| 538 |
In some situations a transformation from one value to another is not |
| 539 |
easily established mathematically, but can be represented by a 2-D |
| 540 |
graph and then linearly interpolated. The graph() function provides |
| 541 |
the opportunity to accomplish this. |
| 542 |
An x-axis value is provided to the graph function along with |
| 543 |
the associated graph represented by a series of x,y pairs. The x |
| 544 |
values must be monotonically increasing (each larger than or equal to |
| 545 |
the previous). The graph function linearly interpolates between |
| 546 |
pairs. Any x value lower the lowest x value (i.e. first) will have |
| 547 |
the associated y value returned. Any x value higher than the last |
| 548 |
will similarly have the associated y value returned. Consider the |
| 549 |
request: |
| 550 |
<div class="code"><pre> |
| 551 |
newmap = graph(map, 1,10, 2,25, 3,50) |
| 552 |
</pre></div> |
| 553 |
X (map) values supplied and y (newmap) values returned: |
| 554 |
<div class="code"><pre> |
| 555 |
0, 10 |
| 556 |
1, 10 |
| 557 |
1.5, 17.5 |
| 558 |
2.9, 47.5 |
| 559 |
4, 50 |
| 560 |
100, 50 |
| 561 |
</pre></div> |
| 562 |
|
| 563 |
<h2>KNOWN ISSUES</h2> |
| 564 |
|
| 565 |
The <em>result</em> variable on the left hand side of the equation should not |
| 566 |
appear in the <em>expression</em> on the right hand side. |
| 567 |
|
| 568 |
<div class="code"><pre> |
| 569 |
<s>mymap = if( mymap > 0, mymap, 0)</s> |
| 570 |
</pre></div> |
| 571 |
|
| 572 |
<p>Any maps generated by a <em>r3.mapcalc</em> command only exist after the entire |
| 573 |
command has completed. All maps are generated concurrently, row-by-row |
| 574 |
(i.e. there is an implicit "for row in rows {...}" around the entire expression). |
| 575 |
Thus the <code>#</code>, <code>@</code>, and <code>[ ]</code> operators cannot be used on a map |
| 576 |
generated within same <em>r3.mapcalc</em> command run. |
| 577 |
|
| 578 |
<div class="code"><pre> |
| 579 |
newmap = oldmap * 3.14 |
| 580 |
<s>othermap = newmap[-1, 0] / newmap[1, 0]</s> |
| 581 |
</pre></div> |
| 582 |
|
| 583 |
<p>Continuation lines must end with a <code>\</code> and have <em>no</em> trailing |
| 584 |
white space (blanks or tabs). If the user does leave white space at the end of |
| 585 |
continuation lines, the error messages produced by <em>r3.mapcalc</em> will |
| 586 |
be meaningless and the equation will not work as the user intended. |
| 587 |
This is particularly important for the <code>eval()</code> function. |
| 588 |
<p><!-- STILL TRUE ??--> |
| 589 |
Currently, there is no comment mechanism in <em>r3.mapcalc</em>. |
| 590 |
Perhaps adding a capability that would cause the entire line to be |
| 591 |
ignored when the user inserted a # at the start of a line |
| 592 |
as if it were not present, would do the trick. |
| 593 |
<p>The function should require the user to type "end" or "exit" instead |
| 594 |
of simply a blank line. This would make separation of multiple scripts |
| 595 |
separable by white space. |
| 596 |
<p><em>r3.mapcalc</em> does not print a warning in case of operations on |
| 597 |
NULL cells. It is left to the user to utilize the <code>isnull()</code> function. |
| 598 |
|
| 599 |
<h2>REFERENCES</h2> |
| 600 |
|
| 601 |
<b><a href="https://grass.osgeo.org/gdp/raster/mapcalc-algebra.pdf">r.mapcalc: An Algebra for GIS and Image |
| 602 |
Processing</a></b>, by Michael Shapiro and Jim Westervelt, U.S. Army |
| 603 |
Construction Engineering Research Laboratory (March/1991). |
| 604 |
<p> |
| 605 |
<b><a href="https://grass.osgeo.org/history_docs/mapcalc.pdf">Performing Map Calculations on GRASS Data: |
| 606 |
r.mapcalc Program Tutorial</a></b>, by Marji Larson, Michael Shapiro and Scott |
| 607 |
Tweddale, U.S. Army Construction Engineering Research Laboratory (December |
| 608 |
1991) |
| 609 |
<p> |
| 610 |
Neteler, M. (2001): Volume modelling of soils using GRASS GIS 3D tools. - in: |
| 611 |
Brovelli, M. (ed.)(2001): The Geomatics Workbook N. 2. Politecnico di Milano, |
| 612 |
Italy (ISSN 1591-092X) (<a href="http://geomatica.como.polimi.it/workbooks/n2/articoli/mn2.pdf">PDF</a>) |
| 613 |
|
| 614 |
<h2>SEE ALSO</h2> |
| 615 |
|
| 616 |
<em> |
| 617 |
<a href="g.region.html">g.region</a>, |
| 618 |
<a href="r3.colors.html">r3.colors</a>, |
| 619 |
<a href="r.mapcalc.html">r.mapcalc</a> |
| 620 |
</em> |
| 621 |
|
| 622 |
<h2>AUTHORS</h2> |
| 623 |
|
| 624 |
Tomas Paudits & Jaro Hofierka, funded by GeoModel s.r.o., Slovakia |
| 625 |
<br><a href="mailto:[email protected]">[email protected]</a>, |
| 626 |
<a href="MAILTO:[email protected]">[email protected]</a> |
| 627 |
<p>Glynn Clements |