source: grass/trunk/raster/r.mapcalc/r.mapcalc.html

Last change on this file was 73802, checked in by neteler, 6 years ago

r.mapcalc manual: refer to r.mapcalc.simple

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/html
File size: 33.0 KB
Line 
1<h2>DESCRIPTION</h2>
2<em>r.mapcalc</em> performs arithmetic on raster map layers.
3New raster map layers can be created which are arithmetic expressions
4involving existing raster map layers, integer or floating point constants,
5and functions.
6
7<h3>Program use</h3>
8
9<em>r.mapcalc</em> expression have the form:
10<p><b>result =</b><em> expression</em>
11<p>where <em>result</em> is the name of a raster map layer
12to contain the result of the calculation and
13<b>expression</b> is any legal arithmetic expression involving existing
14raster map layers (except <em>result</em> itself), integer or floating point constants,
15and functions known to the calculator.
16Parentheses are allowed in the expression and may be nested to any depth.
17<em>result</em> will be created in the user's current mapset.
18<p>
19As <b>expression=</b> is the first option, it is the default. This
20means that passing an expression on the command line is possible
21as long as the expression is quoted and a space is included before the
22first <em>=</em> sign.
23
24Example ('foo' is the resulting map):
25<div class="code"><pre>
26r.mapcalc "foo = 1"
27</pre></div>
28or:
29<div class="code"><pre>
30r.mapcalc 'foo = 1'
31</pre></div>
32
33An unquoted expression (i.e. split over multiple arguments) won't
34work, nor will omitting the space before the = sign:
35
36<div class="code"><pre>
37r.mapcalc 'foo=1'
38Sorry, &lt;foo&gt; is not a valid parameter
39</pre></div>
40
41To read command from the file, use file=
42explicitly, e.g.:
43
44<div class="code"><pre>
45r.mapcalc file=file
46</pre></div>
47or:
48<div class="code"><pre>
49r.mapcalc file=- &lt; file
50</pre></div>
51or:
52<div class="code"><pre>
53r.mapcalc file=- &lt;&lt;EOF
54foo = 1
55EOF
56</pre></div>
57
58<p>The formula entered to <em>r.mapcalc</em> by the user is recorded both in the
59<em>result</em> map title (which appears in the category file for <em>result</em>)
60and in the history file for <em>result</em>.
61<p>Some characters have special meaning to the command shell. If the user
62 is entering input to <em>r.mapcalc</em> on the command line, expressions
63 should be enclosed within single quotes. See NOTES, below.
64</p>
65<h3>Computational regions in r.mapcalc</h3>
66<p>
67 By default <em>r.mapcalc</em> uses the current region
68 as computational region that
69 was set with <a href="g.region.html">g.region</a> for processing.
70 Sometimes it is necessary to use a region that is derived from the
71 raster maps in the expression to set the computational region.
72 This is of high importance for modules that use r.mapcalc internally
73 to process time series of satellite images that all have different
74 spatial extents. A module that requires this feature
75 is <a href="t.rast.algebra.html">t.rast.algebra</a>.
76 The <em>region</em> option of <em>r.mapcalc</em>
77 was implemented to address this requirement.
78 It allows computing and using a region based on
79 all raster maps in an expression. Three modes are supported:
80</p>
81<ul>
82 <li>
83 Setting the <em>region</em> parameter to <em>current</em>
84 will result in the use of the current region
85 as computational region. This is the default.
86 The current region can be set with <a href="g.region.html">g.region</a>.
87 </li>
88 <li>
89 The parameter <em>union</em> will force r.mapcalc
90 to compute the disjoint union of all regions from raster
91 maps specified in the expression. This
92 computed region will then be used as computational region at runtime.
93 The region of the mapset will not be modified.
94 The smallest spatial resolution
95 of all raster maps will be used for processing.
96 </li>
97 <li>
98 The parameter <em>intersect</em> will force r.mapcalc
99 to compute the intersection of all regions from raster
100 maps specified in the expression. This
101 computed region will then be used as computational region at runtime.
102 The region of the mapset will not be modified.
103 The smallest spatial resolution
104 of all raster maps will be used for processing.
105 </li>
106</ul>
107
108<h3>Operators and order of precedence</h3>
109
110The following operators are supported:
111<div class="code"><pre>
112 Operator Meaning Type Precedence
113 --------------------------------------------------------------
114 - negation Arithmetic 12
115 ~ one's complement Bitwise 12
116 ! not Logical 12
117 ^ exponentiation Arithmetic 11
118 % modulus Arithmetic 10
119 / division Arithmetic 10
120 * multiplication Arithmetic 10
121 + addition Arithmetic 9
122 - subtraction Arithmetic 9
123 &lt;&lt; left shift Bitwise 8
124 &gt;&gt; right shift Bitwise 8
125 &gt;&gt;&gt; right shift (unsigned) Bitwise 8
126 &gt; greater than Logical 7
127 &gt;= greater than or equal Logical 7
128 &lt; less than Logical 7
129 &lt;= less than or equal Logical 7
130 == equal Logical 6
131 != not equal Logical 6
132 &amp; bitwise and Bitwise 5
133 | bitwise or Bitwise 4
134 &amp;&amp; logical and Logical 3
135 &amp;&amp;&amp; logical and[1] Logical 3
136 || logical or Logical 2
137 ||| logical or[1] Logical 2
138 ?: conditional Logical 1
139</pre></div>
140(modulus is the remainder upon division)
141<p>[1] The &amp;&amp;&amp; and ||| operators handle null values differently to other
142operators. See the section entitled <b>NULL support</b> below for more
143details.
144<p>The operators are applied from left to right, with those of higher precedence
145applied before those with lower precedence.
146Division by 0 and modulus by 0 are acceptable and give a NULL result.
147The logical operators give a 1 result if the comparison is true, 0 otherwise.
148<p><p>
149
150
151<h3>Raster map layer names</h3>
152
153Anything in the expression which is not a number, operator, or function name
154is taken to be a raster map layer name.
155Examples:
156<p><div class="code"><pre>
157elevation
158x3
1593d.his
160</pre></div>
161<p>Most GRASS raster map layers meet this naming convention.
162However, if a raster map layer has a name which conflicts with the
163above rule, it should be quoted. For example, the expression
164<p><div class="code"><pre>
165x = a-b
166</pre></div>
167<p>would be interpreted as: x equals a minus b, whereas
168<p><div class="code"><pre>
169x = "a-b"
170</pre></div>
171<p>would be interpreted as: x equals the raster map layer named <em>a-b</em>
172<p>Also
173<p><div class="code"><pre>
174x = 3107
175</pre></div>
176<p>would create <em>x</em> filled with the number 3107, while
177<p><div class="code"><pre>
178x = "3107"
179</pre></div>
180<p>would copy the raster map layer <em>3107</em> to the raster map layer <em>x</em>.
181
182<p>Quotes are not required unless the raster map layer names
183look like numbers or contain operators, OR unless the program
184is run non-interactively. Examples given here assume the
185program is run interactively. See NOTES, below.
186<p><em>r.mapcalc</em> will look for the raster map layers according to the
187user's current mapset search path.
188It is possible to override the search path and specify the mapset
189from which to select the raster map layer.
190This is done by specifying the raster map layer name in the form:
191<p><div class="code"><pre>
192name@mapset
193</pre></div>
194<p>For example, the following is a legal expression:
195<p><div class="code"><pre>
196result = x@PERMANENT / y@SOILS
197</pre></div>
198<p>The mapset specified does not have to be in the mapset search path.
199(This method of overriding the mapset search path is common to all
200GRASS commands, not just <em>r.mapcalc</em>.)
201<p>
202
203
204<h3>The neighborhood modifier</h3>
205
206Maps and images are data base files stored in raster format, i.e.,
207two-dimensional matrices of integer values.
208In <em>r.mapcalc</em>, maps may be followed by a <em>neighborhood</em> modifier that
209specifies a relative offset from the current cell being evaluated. The format is
210<em>map[r,c]</em>, where <em>r</em> is the row offset and <em>c</em> is the column offset.
211For example, <em>map[1,2]</em> refers to the cell one row below and two columns
212to the right of the current cell, <em>map[-2,-1]</em> refers to the cell
213two rows above and one column to the left of the current cell,
214and <em>map[0,1]</em> refers to the cell one column to the right of the current cell.
215This syntax permits the development of neighborhood-type filters within a single
216map or across multiple maps.
217<p>
218
219
220<h3>Raster map layer values from the category file</h3>
221
222Sometimes it is desirable to use a value associated with a category's
223<em>label</em> instead of the category value itself. If a raster
224map layer name is preceded by the <b>@</b>
225operator, then the labels in the category file for the raster map layer
226are used in the expression instead of the category value.
227<p>For example, suppose that the raster map layer <em>soil.ph</em>
228(representing soil pH values) has a category file with labels as follows:
229<p><div class="code"><pre>
230cat label
231------------------
2320 no data
2331 1.4
2342 2.4
2353 3.5
2364 5.8
2375 7.2
2386 8.8
2397 9.4
240</pre></div>
241<p>Then the expression:
242<p><div class="code"><pre>
243result = @soils.ph
244</pre></div>
245<p>would produce a result with category values
2460, 1.4, 2.4, 3.5, 5.8, 7.2, 8.8 and 9.4.
247<p>Note that this operator may only be applied to raster map layers
248and produces a floating point value in the expression.
249Therefore, the category label must start with a valid number.
250If the category label is integer, it will be represented by
251a floating point number. I the category label does not start
252with a number or is missing, it will be represented by NULL
253(no data) in the resulting raster map.
254
255
256<h3>Grey scale equivalents and color separates</h3>
257
258It is often helpful to manipulate the colors assigned to map categories.
259This is particularly useful when the spectral properties of cells have meaning
260(as with imagery data), or when the map category values represent real
261quantities (as when category values reflect true elevation values).
262Map color manipulation can also aid visual recognition, and map printing.
263<p>The # operator can be used to either convert map category values to their
264grey scale equivalents or to extract the red, green, or blue components
265of a raster map layer into separate raster map layers.
266<p><div class="code"><pre>
267result = #map
268</pre></div>
269<p>converts each category value in <em>map</em> to a value in the range 0-255 which
270represents the grey scale level implied by the color for the category.
271If the map has a grey scale color table, then the grey level is what
272#map evaluates to. Otherwise, it is computed as:
273<p><div class="code"><pre>
274 0.10 * red + 0.81 * green + 0.01 * blue
275</pre></div>
276<p>Alternatively, you can use:
277<p><div class="code"><pre>
278result = y#map
279</pre></div>
280<p>to use the NTSC weightings:
281<p><div class="code"><pre>
282 0.30 * red + 0.59 * green + 0.11 * blue
283</pre></div>
284<p>Or, you can use:
285<p><div class="code"><pre>
286result = i#map
287</pre></div>
288<p>to use equal weightings:
289<p><div class="code"><pre>
290 0.33 * red + 0.33 * green + 0.33 * blue
291</pre></div>
292<p>The # operator has three other forms: r#map, g#map, b#map.
293These extract the red, green, or blue components in the named raster map,
294respectively. The GRASS shell script <em><a href="r.blend.html">r.blend</a></em> extracts each of these
295components from two raster map layers, and combines them by a user-specified
296percentage.
297These forms allow color separates to be made. For example, to
298extract the red component from <em>map</em>
299and store it in the new 0-255 map layer <em>red</em>,
300the user could type:
301<p><div class="code"><pre>
302red = r#map
303</pre></div>
304<p>To assign this map grey colors type:
305<p><div class="code"><pre>
306<a href="r.colors.html">r.colors</a> map=red color=rules
307black
308white
309</pre></div>
310<p>To assign this map red colors type:
311<p><div class="code"><pre>
312<a href="r.colors.html">r.colors</a> map=red color=rules
313black
314red
315</pre></div>
316<p>
317
318
319<h3>Functions</h3>
320
321The functions currently supported are listed in the table below.
322The type of the result is indicated in the last column.
323<em>F</em> means that the functions always results in a floating point value,
324<em>I</em> means that the function gives an integer result, and
325<em>*</em> indicates that the result is float if any of the arguments to the
326function are floating point values and integer if all arguments are integer.
327
328<p><div class="code"><pre>
329function description type
330---------------------------------------------------------------------------
331abs(x) return absolute value of x *
332acos(x) inverse cosine of x (result is in degrees) F
333asin(x) inverse sine of x (result is in degrees) F
334atan(x) inverse tangent of x (result is in degrees) F
335atan(x,y) inverse tangent of y/x (result is in degrees) F
336ceil(x) the smallest integral value not less than x *
337cos(x) cosine of x (x is in degrees) F
338double(x) convert x to double-precision floating point F
339eval([x,y,...,]z) evaluate values of listed expr, pass results to z
340exp(x) exponential function of x F
341exp(x,y) x to the power y F
342float(x) convert x to single-precision floating point F
343floor(x) the largest integral value not greater than x *
344graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
345graph2(x,x1[,x2,..],y1[,y2..])
346 alternative form of graph() F
347if decision options: *
348if(x) 1 if x not zero, 0 otherwise
349if(x,a) a if x not zero, 0 otherwise
350if(x,a,b) a if x not zero, b otherwise
351if(x,a,b,c) a if x &gt; 0, b if x is zero, c if x &lt; 0
352int(x) convert x to integer [ truncates ] I
353isnull(x) check if x = NULL
354log(x) natural log of x F
355log(x,b) log of x base b F
356max(x,y[,z...]) largest value of those listed *
357median(x,y[,z...]) median value of those listed *
358min(x,y[,z...]) smallest value of those listed *
359mode(x,y[,z...]) mode value of those listed *
360nmax(x,y[,z...]) largest value of those listed, excluding NULLs *
361nmedian(x,y[,z...]) median value of those listed, excluding NULLs *
362nmin(x,y[,z...]) smallest value of those listed, excluding NULLs *
363nmode(x,y[,z...]) mode value of those listed, excluding NULLs *
364not(x) 1 if x is zero, 0 otherwise
365pow(x,y) x to the power y *
366rand(a,b) random value x : a &lt;= x &lt; b *
367round(x) round x to nearest integer I
368round(x,y) round x to nearest multiple of y
369round(x,y,z) round x to nearest y*i+z for some integer i
370sin(x) sine of x (x is in degrees) F
371sqrt(x) square root of x F
372tan(x) tangent of x (x is in degrees) F
373xor(x,y) exclusive-or (XOR) of x and y I
374</pre></div>
375
376<div class="code"><pre>
377Internal variables:
378 row() current row of moving window I
379 col() current col of moving window I
380 nrows() number of rows in computation region I
381 ncols() number of columns in computation region I
382 x() current x-coordinate of moving window F
383 y() current y-coordinate of moving window F
384 ewres() current east-west resolution F
385 nsres() current north-south resolution F
386 area() area of current cell in square meters F
387 null() NULL value
388</pre></div>
389Note, that the row() and col() indexing starts with 1.
390
391
392<h3>Floating point values in the expression</h3>
393
394Floating point numbers are allowed in the expression. A floating point
395number is a number which contains a decimal point:
396<div class="code"><pre>
397 2.3 12.0 12. .81
398</pre></div>
399Floating point values in the expression are handled in a special way.
400With arithmetic and logical operators, if either operand is float,
401the other is converted to float and the result of the operation is float.
402This means, in particular that division of integers results in a
403(truncated) integer, while division of floats results in an accurate
404floating point value. With functions of type * (see table above),
405the result is float if any argument is float, integer otherwise.
406<p>Note: If you calculate with integer numbers, the resulting map will
407be integer. If you want to get a float result, add the decimal point
408to integer number(s).
409<p>If you want floating point division, at least one of the arguments has
410to be a floating point value. Multiplying one of them by 1.0 will
411produce a floating-point result, as will using float():
412<div class="code"><pre>
413 r.mapcalc "ndvi = float(lsat.4 - lsat.3) / (lsat.4 + lsat.3)"
414</pre></div>
415
416
417<h3>NULL support</h3>
418
419<ul>
420<li>Division by zero should result in NULL.
421<li>Modulus by zero should result in NULL.
422<li>NULL-values in any arithmetic or logical operation should result
423in NULL. (however, &amp;&amp;&amp; and ||| are treated specially, as described below).
424<li>The &amp;&amp;&amp; and ||| operators observe the following axioms even when x is NULL:
425<div class="code"><pre>
426 x &&& false == false
427 false &&& x == false
428 x ||| true == true
429 true ||| x == true
430</pre></div>
431<li>NULL-values in function arguments should result in NULL (however,
432if(), eval() and isnull() are treated specially, as described below).
433<li>The eval() function always returns its last argument
434<li>The situation for if() is:
435<div class="code"><pre>
436if(x)
437 NULL if x is NULL; 0 if x is zero; 1 otherwise
438if(x,a)
439 NULL if x is NULL; a if x is non-zero; 0 otherwise
440if(x,a,b)
441 NULL if x is NULL; a if x is non-zero; b otherwise
442if(x,n,z,p)
443 NULL if x is NULL; n if x is negative;
444z if x is zero; p if x is positive
445</pre></div>
446<li>The (new) function isnull(x) returns: 1 if x is NULL;
4470 otherwise. The (new) function null()
448(which has no arguments) returns an integer NULL.
449<li>Non-NULL, but invalid, arguments to functions should result in NULL.
450<div class="code"><pre>
451Examples:
452log(-2)
453sqrt(-2)
454pow(a,b) where a is negative and b is not an integer
455</pre></div>
456</ul>
457<p>NULL support: Please note that any math performed with NULL cells always
458results in a NULL value for these cells. If you want to replace a NULL cell
459on-the-fly, use the isnull() test function in a if-statement.
460<p>Example: The users wants the NULL-valued cells to be treated like zeros. To
461add maps A and B (where B contains NULLs) to get a map C the user can use a
462construction like:<p><div class="code"><pre>
463C = A + if(isnull(B),0,B)
464</pre></div>
465<p><b>NULL and conditions:</b>
466<p>For the one argument form:
467<div class="code"><pre>
468if(x) = NULL if x is NULL
469if(x) = 0 if x = 0
470if(x) = 1 otherwise (i.e. x is neither NULL nor 0).
471</pre></div>
472<p>For the two argument form:
473<div class="code"><pre>
474if(x,a) = NULL if x is NULL
475if(x,a) = 0 if x = 0
476if(x,a) = a otherwise (i.e. x is neither NULL nor 0).
477</pre></div>
478<p>For the three argument form:
479<div class="code"><pre>
480if(x,a,b) = NULL if x is NULL
481if(x,a,b) = b if x = 0
482if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0).
483</pre></div>
484<p>For the four argument form:
485<div class="code"><pre>
486if(x,a,b,c) = NULL if x is NULL
487if(x,a,b,c) = a if x > 0
488if(x,a,b,c) = b if x = 0
489if(x,a,b,c) = c if x &lt; 0
490</pre></div>
491More generally, all operators and most functions return NULL if *any*
492of their arguments are NULL.
493<br>
494The functions if(), isnull() and eval() are exceptions.
495<br>
496The function isnull() returns 1 if its argument is NULL and 0 otherwise.
497If the user wants the opposite, the ! operator, e.g. "!isnull(x)" must be
498used.
499<p>All forms of if() return NULL if the first argument is NULL. The 2, 3
500and 4 argument forms of if() return NULL if the "selected" argument is
501NULL, e.g.:
502<div class="code"><pre>
503if(0,a,b) = b regardless of whether a is NULL
504if(1,a,b) = a regardless of whether b is NULL
505</pre></div>
506eval() always returns its last argument, so it only returns NULL if
507the last argument is NULL.
508<p><b>Note</b>: The user cannot test for NULL using the == operator, as that
509returns NULL if either or both arguments are NULL, i.e. if x and y are
510both NULL, then "x == y" and "x != y" are both NULL rather than 1 and
5110 respectively.
512<br>
513The behaviour makes sense if the user considers NULL as representing an
514unknown quantity. E.g. if x and y are both unknown, then the values of
515"x == y" and "x != y" are also unknown; if they both have unknown
516values, the user doesn't know whether or not they both have the same value.
517
518
519<h2>NOTES</h2>
520
521<h3>Usage from command line</h3>
522
523Extra care must be taken if the expression is given on the command line.
524Some characters have special meaning to the UNIX shell.
525These include, among others:
526<div class="code"><pre>
527* ( ) &gt; &amp; |
528</pre></div>
529<p>It is advisable to put single quotes around the expression; e.g.:
530<div class="code"><pre>
531'result = elevation * 2'
532</pre></div>
533Without the quotes, the *, which has special meaning to the UNIX shell,
534would be altered and <em>r.mapcalc</em> would see something other than the *.
535
536<h3>Multiple computations</h3>
537<p>
538In general, it's preferable to do as much as possible in each
539r.mapcalc command. E.g. rather than:
540<div class="code"><pre>
541 r.mapcalc "$GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND"
542 r.mapcalc "$GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND"
543 r.mapcalc "$GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND"
544</pre></div>
545<p>use:
546<div class="code"><pre>
547 r.mapcalc &lt;&lt;EOF
548 $GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND
549 $GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND
550 $GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 - .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND
551 EOF
552</pre></div>
553<p>as the latter will read each input map only once.
554
555<h3>Backwards compatibility</h3>
556
557For the backwards compatibility with GRASS 6,
558<!-- check wording: -->
559if no options are given, it manufactures <tt>file=-</tt> (which reads from
560stdin), so you can continue to use e.g.:
561<div class="code"><pre>
562r.mapcalc &lt; file
563</pre></div>
564or:
565<div class="code"><pre>
566r.mapcalc &lt;&lt;EOF
567foo = 1
568EOF
569</pre></div>
570But unless you need compatibility with previous GRASS GIS versions, use <tt>file=</tt>
571explicitly, as stated above.
572<p>
573When the map name contains uppercase letter(s) or a dot which are not
574allowed to be in module option names, the <em>r.mapcalc</em> command will
575be valid also without quotes:
576<div class="code"><pre>
577r.mapcalc elevation_A=1
578r.mapcalc elevation.1=1
579</pre></div>
580However, this syntax is not recommended as quotes as stated above more safe.
581Using quotes is both backwards compatible and valid in future.
582
583<h3>Interactive input in command line</h3>
584
585For formulas that the user enters from standard input
586(rather than from the command line), a line continuation feature now exists.
587If the user adds a backslash to the end of an input line, <em>r.mapcalc</em> assumes that
588the formula being entered by the user continues on to the next input line.
589There is no limit to the possible number of input lines
590or to the length of a formula.
591<p>If the <em>r.mapcalc</em> formula entered by the user is very long,
592the map title will contain only some of it, but most (if not all) of
593the formula will be placed into the history file for the <em>result</em> map.
594
595<h3>Raster MASK handling</h3>
596<p>
597<em>r.mapcalc</em> follows the common GRASS behavior of raster MASK handling,
598so the MASK is only applied when reading an existing GRASS raster map.
599This implies that, for example, the command:
600<div class="code"><pre>
601r.mapcalc "elevation_exaggerated = elevation * 3"
602</pre></div>
603create a map respecting the masked pixels if MASK is active.
604
605<p>
606However, when creating a map which is not based on any map,
607e.g. a map from a constant:
608<div class="code"><pre>
609r.mapcalc "base_height = 200.0"
610</pre></div>
611the created raster map is limited only by a computation region
612but it is not affected by an active MASK.
613This is expected because, as mentioned above, MASK is only applied when reading,
614not when writing a raster map.
615<p>
616If also in this case the MASK should be applied, an if() statement including the
617MASK should be used, e.g.:
618<div class="code"><pre>
619r.mapcalc "base_height = if(MASK, 200.0, null())"
620</pre></div>
621
622When testing MASK related expressions keep in mind that when MASK is active
623you don't see data in masked areas even if they are not NULL.
624See <em><a href="r.mask.html">r.mask</a></em> for details.
625
626<h3>eval function</h3>
627If the output of the computation should be only one map
628but the expression is so complex that it is better to split it
629to several expressions, the <tt>eval</tt> function can be used:
630<div class="code"><pre>
631r.mapcalc &lt;&lt; EOF
632eval(elev_200 = elevation - 200, \
633 elev_5 = 5 * elevation, \
634 elev_p = pow(elev_5, 2))
635elevation_result = (0.5 * elev_200) + 0.8 * elev_p
636EOF
637</pre></div>
638This example uses unix-like <tt>&lt;&lt; EOF</tt> syntax to provide
639input to <em>r.mapcalc</em>.
640<p>
641Note that the temporary variables (maps) are not created
642and thus it does not matter whether they exists or not.
643In the example above, if map <tt>elev_200</tt> exists it will not be
644overwritten and no error will be generated.
645The reason is that the name <tt>elev_200</tt> now denotes the temporary
646variable (map) and not the existing map.
647The following parts of the expression will use the temporary <tt>elev_200</tt>
648and the existing <tt>elev_200</tt> will be left intact and will not be used.
649If a user want to use the existing map, the name of the temporary variable
650(map) must be changed.
651
652<h3>Using the same map for input and output results</h3>
653A map cannot be used both as an input and as an output as in
654this invalid expression <tt>oldmap = oldmap + 1</tt>, instead
655a subsequent rename using <em><a href="g.rename.html">g.rename</a></em> is
656needed when the same name is desired:
657
658<div class="code"><pre>
659r.mapcalc "newmap = oldmap + 1"
660g.rename raster=newmap,oldmap
661</pre></div>
662
663<h3>Random number generator initialization</h3>
664<p>The pseudo-random number generator used by the rand() function can
665be initialised to a specific value using the <b>seed</b> option.
666This can be used to replicate a previous calculation.
667<p>Alternatively, it can be initialised from the system time and the
668PID using the <b>-r</b> flag. This should result in a different seed
669being used each time.
670<p>In either case, the seed will be written to the map's history, and
671can be seen using <em>r.info</em>.
672<p>If you want other people to be able to verify your results, it's
673preferable to use the <b>seed</b> option to supply a seed which is
674either specified in the script or generated from a determenistic process
675such as a pseudo-random number generator given an explicit seed.
676<p>Note that the rand() function will generate a fatal error if neither
677the <b>seed</b> option nor the <b>-s</b> flag are given.
678
679
680<h2>EXAMPLES</h2>
681
682To compute the average of two raster map layers
683<em>a</em> and <em>b</em>:
684<div class="code"><pre>
685ave = (a + b)/2
686</pre></div>
687
688<p>
689To form a weighted average:
690<div class="code"><pre>
691ave = (5*a + 3*b)/8.0
692</pre></div>
693
694<p>
695To produce a binary representation of the raster map layer
696<em>a</em> so that category 0 remains 0 and all other categories become 1:
697<div class="code"><pre>
698mapmask = a != 0
699</pre></div>
700This could also be accomplished by:
701<div class="code"><pre>
702mapmask = if(a)
703</pre></div>
704
705<p>
706To mask raster map layer <em>b</em> by raster map layer <em>a</em>:
707<div class="code"><pre>
708result = if(a,b)
709</pre></div>
710
711<p>
712To change all values below 5 to NULL:
713<div class="code"><pre>
714newmap = if(map&lt;5, null(), 5)
715</pre></div>
716
717<p>
718To create a map with random values in a defined range (needs either the
719usage of <b>-s</b> flag or the <em>seed</em> parameter). The precision of
720the input values determines the output precision (the resulting
721<a href="rasterintro.html#raster-format">raster map type</a>):
722
723<div class="code"><pre>
724# write result as integer map (CELL)
725random_int = rand(-100,100)
726
727# write result as double precision floating point map (DCELL)
728random_dcell = rand(-100.0,100.0)
729
730# write result as single precision floating point map (FCELL)
731random_fcell = float(rand(-100.0,100.0))
732</pre></div>
733
734<p>
735The graph() function allows users to specify a x-y conversion using
736pairs of x,y coordinates.
737In some situations a transformation from one value to another is not
738easily established mathematically, but can be represented by a 2-D
739graph and then linearly interpolated. The graph() function provides
740the opportunity to accomplish this.
741An x-axis value is provided to the graph function along with
742the associated graph represented by a series of x,y pairs. The x
743values must be monotonically increasing (each larger than or equal to
744the previous). The graph function linearly interpolates between
745pairs. Any x value lower the lowest x value (i.e. first) will have
746the associated y value returned. Any x value higher than the last
747will similarly have the associated y value returned. Consider the
748request:
749<div class="code"><pre>
750newmap = graph(map, 1,10, 2,25, 3,50)
751</pre></div>
752X (map) values supplied and y (newmap) values returned:
753<div class="code"><pre>
7540, 10
7551, 10
7561.5, 17.5
7572.9, 47.5
7584, 50
759100, 50
760</pre></div>
761
762
763<h2>KNOWN ISSUES</h2>
764
765The <em>result</em> variable on the left hand side of the equation should not
766appear in the <em>expression</em> on the right hand side.
767
768<div class="code"><pre>
769<s>mymap = if( mymap &gt; 0, mymap, 0)</s>
770</pre></div>
771
772<p>Any maps generated by a <em>r.mapcalc</em> command only exist after the entire
773command has completed. All maps are generated concurrently, row-by-row
774(i.e. there is an implicit "for row in rows {...}" around the entire expression).
775Thus the <tt>#</tt>, <tt>@</tt>, and <tt>[ ]</tt> operators cannot be used on a map
776generated within same <em>r.mapcalc</em> command run.
777Consequently, the following (strikethrough code) does not work:
778
779<div class="code"><pre>
780newmap = oldmap * 3.14
781<s>othermap = newmap[-1, 0] / newmap[1, 0]</s>
782</pre></div>
783
784<p>Continuation lines must end with a <tt>\</tt> and have <em>no</em> trailing
785white space (blanks or tabs). If the user does leave white space at the end of
786continuation lines, the error messages produced by <em>r.mapcalc</em> will
787be meaningless and the equation will not work as the user intended.
788This is particularly important for the <tt>eval()</tt> function.
789<p><!-- STILL TRUE ??-->
790Currently, there is no comment mechanism in <em>r.mapcalc</em>.
791Perhaps adding a capability that would cause the entire line to be
792ignored when the user inserted a # at the start of a line
793as if it were not present, would do the trick.
794<p>The function should require the user to type "end" or "exit" instead
795of simply a blank line. This would make separation of multiple scripts
796separable by white space.
797<p><em>r.mapcalc</em> does not print a warning in case of operations on
798NULL cells. It is left to the user to utilize the <tt>isnull()</tt> function.
799
800
801<h2>SEE ALSO</h2>
802
803<em>
804<a href="g.region.html">g.region</a>,
805<a href="r.bitpattern.html">r.bitpattern</a>,
806<a href="r.blend.html">r.blend</a>,
807<a href="r.colors.html">r.colors</a>,
808<a href="r.fillnulls.html">r.fillnulls</a>,
809<a href="r.mapcalc.simple.html">r.mapcalc.simple</a>
810</em>
811
812
813<h2>REFERENCES</h2>
814
815<b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc-algebra.pdf">r.mapcalc: An Algebra for GIS and Image
816Processing</a></b>, by Michael Shapiro and Jim Westervelt, U.S. Army
817Construction Engineering Research Laboratory (March/1991).
818<p>
819<b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc.pdf">Performing Map Calculations on GRASS Data:
820r.mapcalc Program Tutorial</a></b>, by Marji Larson, Michael Shapiro and Scott
821Tweddale, U.S. Army Construction Engineering Research Laboratory (December
8221991)
823<p>Grey scale conversion is based on the C.I.E. x,y,z system where y represents
824luminance. See "Fundamentals of Digital Image Processing,"
825by Anil K. Jain (Prentice Hall, NJ, 1989; p 67).
826
827
828<h2>AUTHORS</h2>
829Michael Shapiro, U.S.Army Construction Engineering
830Research Laboratory
831<p>Glynn Clements
832
833<p>
834<i>Last changed: $Date: 2018-12-14 21:41:06 +0000 (Fri, 14 Dec 2018) $</i>
Note: See TracBrowser for help on using the repository browser.