r3.mapcalc.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <h2>DESCRIPTION</h2>
  2. <em>r3.mapcalc</em> performs arithmetic on raster map layers.
  3. New raster map layers can be created which are arithmetic expressions
  4. involving existing raster map layers, integer or floating point constants,
  5. and functions.
  6. <h2>PROGRAM USE</h2>
  7. <em>r3.mapcalc</em> expects its input to have the form:
  8. <p><b>result =</b><em> expression</em>
  9. <p>where <em>result</em> is the name of a raster map layer
  10. to contain the result of the calculation and
  11. <em>expression</em> is any legal arithmetic expression involving existing
  12. raster map layers, integer or floating point constants,
  13. and functions known to the calculator.
  14. Parentheses are allowed in the expression and may be nested to any depth.
  15. <em>result</em> will be created in the user's current mapset.
  16. <p>
  17. As <em>expression=</em> is the first option, it is the default. This
  18. means that passing an expression on the command line is possible
  19. as long as the expression is quoted and a space is included before the
  20. first <em>=</em> sign.
  21. Example ('foo' is the resulting map):
  22. <div class="code"><pre>
  23. r3.mapcalc "foo = 1"
  24. </pre></div>
  25. or:
  26. <div class="code"><pre>
  27. r3.mapcalc 'foo = 1'
  28. </pre></div>
  29. An unquoted expression (i.e. split over multiple arguments) won't
  30. work, nor will omitting the space before the = sign:
  31. <div class="code"><pre>
  32. r3.mapcalc 'foo=1'
  33. Sorry, &lt;foo&gt; is not a valid parameter
  34. </pre></div>
  35. If no options are given, it manufactures "input=-" (which reads from
  36. stdin), so you can continue to use e.g.:
  37. <div class="code"><pre>
  38. r3.mapcalc &lt; file
  39. </pre></div>
  40. or:
  41. <div class="code"><pre>
  42. r3.mapcalc &lt;&lt;EOF
  43. foo = 1
  44. EOF
  45. </pre></div>
  46. But unless you need compatibility with previous versions, use input=
  47. explicitly, e.g.:
  48. <div class="code"><pre>
  49. r3.mapcalc input=file
  50. </pre></div>
  51. or:
  52. <div class="code"><pre>
  53. r3.mapcalc input=- &lt;&lt;EOF
  54. foo = 1
  55. EOF
  56. </pre></div>
  57. <p>The formula entered to <em>r3.mapcalc</em> by the user is recorded both in the
  58. <em>result</em> map title (which appears in the category file for <em>result</em>)
  59. and in the history file for <em>result</em>.
  60. <p>Some characters have special meaning to the command shell. If the user
  61. is entering input to <em>r3.mapcalc</em> on the command line, expressions
  62. should be enclosed within single quotes. See NOTES, below.
  63. <p>
  64. <h2>OPERATORS AND ORDER OF PRECEDENCE</h2>
  65. The following operators are supported:
  66. <div class="code"><pre>
  67. Operator Meaning Type Precedence
  68. --------------------------------------------------------------
  69. - negation Arithmetic 12
  70. ~ one's complement Bitwise 12
  71. ! not Logical 12
  72. ^ exponentiation Arithmetic 11
  73. % modulus Arithmetic 10
  74. / division Arithmetic 10
  75. * multiplication Arithmetic 10
  76. + addition Arithmetic 9
  77. - subtraction Arithmetic 9
  78. &lt;&lt; left shift Bitwise 8
  79. &gt;&gt; right shift Bitwise 8
  80. &gt;&gt;&gt; right shift (unsigned) Bitwise 8
  81. &gt; greater than Logical 7
  82. &gt;= greater than or equal Logical 7
  83. &lt; less than Logical 7
  84. &lt;= less than or equal Logical 7
  85. == equal Logical 6
  86. != not equal Logical 6
  87. &amp; bitwise and Bitwise 5
  88. | bitwise or Bitwise 4
  89. &amp;&amp; logical and Logical 3
  90. &amp;&amp;&amp; logical and[1] Logical 3
  91. || logical or Logical 2
  92. ||| logical or[1] Logical 2
  93. ?: conditional Logical 1
  94. </pre></div>
  95. (modulus is the remainder upon division)
  96. <p>[1] The &amp;&amp;&amp; and ||| operators handle null values differently to other
  97. operators. See the section entitled <b>NULL support</b> below for more
  98. details.
  99. <p>The operators are applied from left to right, with those of higher precedence
  100. applied before those with lower precedence.
  101. Division by 0 and modulus by 0 are acceptable and give a NULL result.
  102. The logical operators give a 1 result if the comparison is true, 0 otherwise.
  103. <p><p>
  104. <h2>3D GRID NAMES</h2>
  105. Anything in the expression which is not a number, operator, or function
  106. name is taken to be a 3D grid name. Examples:
  107. <p><div class="code"><pre>
  108. volume
  109. x3
  110. 3d.his
  111. </pre></div>
  112. <p>Most GRASS raster map layers and 3D grids meet this naming convention.
  113. However, if a 3D grid has a name which conflicts with the above rule, it
  114. should be quoted. For example, the expression
  115. <p><div class="code"><pre>
  116. x = a-b
  117. </pre></div>
  118. <p>would be interpreted as: x equals a minus b, whereas
  119. <p><div class="code"><pre>
  120. x = "a-b"
  121. </pre></div>
  122. <p>would be interpreted as: x equals the 3D grid named <em>a-b</em>
  123. <p>Also
  124. <p><div class="code"><pre>
  125. x = 3107
  126. </pre></div>
  127. <p>would create <em>x</em> filled with the number 3107, while
  128. <p><div class="code"><pre>
  129. x = "3107"
  130. </pre></div>
  131. <p>would copy the 3D grid <em>3107</em> to the 3D grid <em>x</em>.
  132. <p>Quotes are not required unless the 3D grid names look like numbers or
  133. contain operators, OR unless the program is run non-interactively. Examples
  134. given here assume the program is run interactively. See NOTES, below.
  135. <p><em>r3.mapcalc</em> will look for the 3D grids according to the user's
  136. current mapset search path. It is possible to override the search path
  137. and specify the mapset from which to select the 3D grid. This is done by
  138. specifying the 3D grid name in the form:
  139. <p><div class="code"><pre>
  140. name@mapset
  141. </pre></div>
  142. <p>For example, the following is a legal expression:
  143. <p><div class="code"><pre>
  144. result = x@PERMANENT / y@SOILS
  145. </pre></div>
  146. <p>The mapset specified does not have to be in the mapset search path. (This
  147. method of overriding the mapset search path is common to all GRASS commands,
  148. not just <em>r3.mapcalc</em>.)
  149. <p>
  150. <h2>THE NEIGHBORHOOD MODIFIER</h2>
  151. 3D grids are data base files stored in voxel format, i.e., three-dimensional
  152. matrices of float/double values. In <em>r3.mapcalc</em>, 3D grids may be
  153. followed by a <em>neighborhood</em> modifier that specifies a relative offset
  154. from the current cell being evaluated. The format is
  155. <em>map[r,c,d]</em>,
  156. where <em>r</em> is the row offset, <em>c</em> is the column offset and <em>d</em>
  157. is the depth offset. For example, <em>map[1,2,3]</em> refers to the cell
  158. one row below, two columns to the right and 3 levels below of the current
  159. cell, <em>map[-3,-2,-1]</em> refers to the cell three rows above, two columns
  160. to the left and one level below of the current cell, and <em>map[0,1,0]</em>
  161. refers to the cell one column to the right of the current cell. This syntax
  162. permits the development of neighborhood-type filters within a single 3D
  163. grid or across multiple 3D grids.
  164. <p>
  165. <h2>FUNCTIONS</h2>
  166. The functions currently supported are listed in the table below.
  167. The type of the result is indicated in the last column.
  168. <em>F</em>
  169. means that the functions always results in a floating point value,
  170. <em>I</em>
  171. means that the function gives an integer result, and
  172. <em>*</em>
  173. indicates that the result is float if any of the arguments to the function
  174. are floating point values and integer if all arguments are integer.
  175. <p><div class="code"><pre>
  176. function description type
  177. ---------------------------------------------------------------------------
  178. abs(x) return absolute value of x *
  179. acos(x) inverse cosine of x (result is in degrees) F
  180. asin(x) inverse sine of x (result is in degrees) F
  181. atan(x) inverse tangent of x (result is in degrees) F
  182. atan(x,y) inverse tangent of y/x (result is in degrees) F
  183. cos(x) cosine of x (x is in degrees) F
  184. double(x) convert x to double-precision floating point F
  185. eval([x,y,...,]z) evaluate values of listed expr, pass results to z
  186. exp(x) exponential function of x F
  187. exp(x,y) x to the power y F
  188. float(x) convert x to single-precision floating point F
  189. graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
  190. if decision options: *
  191. if(x) 1 if x not zero, 0 otherwise
  192. if(x,a) a if x not zero, 0 otherwise
  193. if(x,a,b) a if x not zero, b otherwise
  194. if(x,a,b,c) a if x &gt; 0, b if x is zero, c if x &lt; 0
  195. int(x) convert x to integer [ truncates ] I
  196. isnull(x) check if x = NULL
  197. log(x) natural log of x F
  198. log(x,b) log of x base b F
  199. max(x,y[,z...]) largest value of those listed *
  200. median(x,y[,z...]) median value of those listed *
  201. min(x,y[,z...]) smallest value of those listed *
  202. mode(x,y[,z...]) mode value of those listed *
  203. nmax(x,y[,z...]) largest value of those listed, excluding NULLs *
  204. nmedian(x,y[,z...]) median value of those listed, excluding NULLs *
  205. nmin(x,y[,z...]) smallest value of those listed, excluding NULLs *
  206. nmode(x,y[,z...]) mode value of those listed, excluding NULLs *
  207. not(x) 1 if x is zero, 0 otherwise
  208. pow(x,y) x to the power y *
  209. rand(a,b) random value x : a &lt;= x &lt; b
  210. round(x) round x to nearest integer I
  211. sin(x) sine of x (x is in degrees) F
  212. sqrt(x) square root of x F
  213. tan(x) tangent of x (x is in degrees) F
  214. xor(x,y) exclusive-or (XOR) of x and y I
  215. </pre></div>
  216. <div class="code"><pre>
  217. Internal variables:
  218. row() current row of moving window
  219. col() current col of moving window
  220. depth() return current depth
  221. x() current x-coordinate of moving window
  222. y() current y-coordinate of moving window
  223. z() return current z value
  224. ewres() current east-west resolution
  225. nsres() current north-south resolution
  226. tbres() current top-bottom resolution
  227. null() NULL value
  228. </pre></div>
  229. Note, that the row(), col() and depth() indexing starts with 1.
  230. <h2>FLOATING POINT VALUES IN THE EXPRESSION</h2>
  231. Floating point numbers are allowed in the expression. A floating point
  232. number is a number which contains a decimal point:
  233. <div class="code"><pre>
  234. 2.3 12.0 12. .81
  235. </pre></div>
  236. Floating point values in the expression are handled in a special way.
  237. With arithmetic and logical operators, if either operand is float,
  238. the other is converted to float and the result of the operation is float.
  239. This means, in particular that division of integers results in a
  240. (truncated) integer, while division of floats results in an accurate
  241. floating point value. With functions of type * (see table above),
  242. the result is float if any argument is float, integer otherwise.
  243. <p>Note: If you calculate with integer numbers, the resulting map will
  244. be integer. If you want to get a float result, add the decimal point
  245. to integer number(s).
  246. <p>If you want floating point division, at least one of the arguments has
  247. to be a floating point value. Multiplying one of them by 1.0 will
  248. produce a floating-point result, as will using float():
  249. <div class="code"><pre>
  250. r3.mapcalc "ratio = float(soil.4 - soil.3) / soil.3)"
  251. </pre></div>
  252. <h2>NULL support</h2>
  253. <ul>
  254. <li>Division by zero should result in NULL.
  255. <li>Modulus by zero should result in NULL.
  256. <li>NULL-values in any arithmetic or logical operation should result
  257. in NULL. (however, &amp;&amp;&amp; and ||| are treated specially, as described below).
  258. <li>The &amp;&amp;&amp; and ||| operators observe the following axioms even when x is NULL:
  259. <div class="code"><pre>
  260. x &&& false == false
  261. false &&& x == false
  262. x ||| true == true
  263. true ||| x == true
  264. </pre></div>
  265. <li>NULL-values in function arguments should result in NULL (however,
  266. if(), eval() and isnull() are treated specially, as described below).
  267. <li>The eval() function always returns its last argument
  268. <li>The situation for if() is:
  269. <div class="code"><pre>
  270. if(x)
  271. NULL if x is NULL; 0 if x is zero; 1 otherwise
  272. if(x,a)
  273. NULL if x is NULL; a if x is non-zero; 0 otherwise
  274. if(x,a,b)
  275. NULL if x is NULL; a if x is non-zero; b otherwise
  276. if(x,n,z,p)
  277. NULL if x is NULL; n if x is negative;
  278. z if x is zero; p if x is positive
  279. </pre></div>
  280. <li>The (new) function isnull(x) returns: 1 if x is NULL;
  281. 0 otherwise. The (new) function null()
  282. (which has no arguments) returns an integer NULL.
  283. <li>Non-NULL, but invalid, arguments to functions should result in NULL.
  284. <div class="code"><pre>
  285. Examples:
  286. log(-2)
  287. sqrt(-2)
  288. pow(a,b) where a is negative and b is not an integer
  289. </pre></div>
  290. </ul>
  291. <p>NULL support: Please note that any math performed with NULL cells always
  292. results in a NULL value for these cells. If you want to replace a NULL cell
  293. on-the-fly, use the isnull() test function in a if-statement.
  294. <p>Example: The users wants the NULL-valued cells to be treated like zeros. To
  295. add maps A and B (where B contains NULLs) to get a map C the user can use a
  296. construction like:<p><div class="code"><pre>
  297. C=A + if(isnull(B),0,B)
  298. </pre></div>
  299. <p><b>NULL and conditions:</b>
  300. <p>For the one argument form:
  301. <div class="code"><pre>
  302. if(x) = NULL if x is NULL
  303. if(x) = 0 if x = 0
  304. if(x) = 1 otherwise (i.e. x is neither NULL nor 0).
  305. </pre></div>
  306. <p>For the two argument form:
  307. <div class="code"><pre>
  308. if(x,a) = NULL if x is NULL
  309. if(x,a) = 0 if x = 0
  310. if(x,a) = a otherwise (i.e. x is neither NULL nor 0).
  311. </pre></div>
  312. <p>For the three argument form:
  313. <div class="code"><pre>
  314. if(x,a,b) = NULL if x is NULL
  315. if(x,a,b) = b if x = 0
  316. if(x,a,b) = a otherwise (i.e. x is neither NULL nor 0).
  317. </pre></div>
  318. <p>For the four argument form:
  319. <div class="code"><pre>
  320. if(x,a,b,c) = NULL if x is NULL
  321. if(x,a,b,c) = a if x > 0
  322. if(x,a,b,c) = b if x = 0
  323. if(x,a,b,c) = c if x &lt; 0
  324. </pre></div>
  325. More generally, all operators and most functions return NULL if *any*
  326. of their arguments are NULL.
  327. <br>
  328. The functions if(), isnull() and eval() are exceptions.
  329. <br>
  330. The function isnull() returns 1 if its argument is NULL and 0 otherwise.
  331. If the user wants the opposite, the ! operator, e.g. "!isnull(x)" must be
  332. used.
  333. <p>All forms of if() return NULL if the first argument is NULL. The 2, 3
  334. and 4 argument forms of if() return NULL if the "selected" argument is
  335. NULL, e.g.:
  336. <div class="code"><pre>
  337. if(0,a,b) = b regardless of whether a is NULL
  338. if(1,a,b) = a regardless of whether b is NULL
  339. </pre></div>
  340. eval() always returns its last argument, so it only returns NULL if
  341. the last argument is NULL.
  342. <p><b>Note</b>: The user cannot test for NULL using the == operator, as that
  343. returns NULL if either or both arguments are NULL, i.e. if x and y are
  344. both NULL, then "x == y" and "x != y" are both NULL rather than 1 and
  345. 0 respectively.
  346. <br>
  347. The behaviour makes sense if the user considers NULL as representing an
  348. unknown quantity. E.g. if x and y are both unknown, then the values of
  349. "x == y" and "x != y" are also unknown; if they both have unknown
  350. values, the user doesn't know whether or not they both have the same value.
  351. <h2>EXAMPLES</h2>
  352. To compute the average of two 3D grids
  353. <em>a</em> and <em>b</em>:
  354. <div class="code"><pre>
  355. ave = (a + b)/2
  356. </pre></div>
  357. To form a weighted average:
  358. <div class="code"><pre>
  359. ave = (5*a + 3*b)/8.0
  360. </pre></div>
  361. To produce a binary representation of 3D grid
  362. <em>a</em> so that category 0 remains 0 and all other categories become 1:
  363. <div class="code"><pre>
  364. mask = a != 0
  365. </pre></div>
  366. This could also be accomplished by:
  367. <div class="code"><pre>
  368. mask = if(a)
  369. </pre></div>
  370. To mask 3D grid <em>b</em> by 3D grid <em>a</em>:
  371. <div class="code"><pre>
  372. result = if(a,b)
  373. </pre></div>
  374. To change all values below 5 to NULL:
  375. <div class="code"><pre>
  376. newmap = if(map&lt;5, null(), 5)
  377. </pre></div>
  378. The graph function allows users to specify a x-y conversion using
  379. pairs of x,y coordinates.
  380. In some situations a transformation from one value to another is not
  381. easily established mathematically, but can be represented by a 2-D
  382. graph. The graph() function provides the opportunity to accomplish
  383. this. An x-axis value is provided to the graph function along with
  384. the associated graph represented by a series of x,y pairs. The x
  385. values must be monotonically increasing (each larger than or equal to
  386. the previous). The graph function linearly interpolates between
  387. pairs. Any x value lower the lowest x value (i.e. first) will have
  388. the associated y value returned. Any x value higher than the last
  389. will similarly have the associated y value returned. Consider the
  390. request:
  391. <div class="code"><pre>
  392. newmap = graph(map, 1,10, 2,25, 3,50)
  393. </pre></div>
  394. X (map) values supplied and y (newmap) values returned:
  395. <div class="code"><pre>
  396. 0, 10
  397. 1, 10,
  398. 1.5, 16.5
  399. 2.9, 47.5
  400. 4, 50
  401. 100, 50
  402. </pre></div>
  403. <h2>NOTES</h2>
  404. Extra care must be taken if the expression is given on the command line.
  405. Some characters have special meaning to the UNIX shell.
  406. These include, among others:
  407. <p>* ( ) &gt; &amp; |
  408. <p>It is advisable to put single quotes around the expression; e.g.:
  409. <div class="code"><pre>
  410. 'result = elevation * 2'
  411. </pre></div>
  412. Without the quotes, the *, which has special meaning to the UNIX shell,
  413. would be altered and <em>r3.mapcalc</em> would see something other than the *.
  414. <p>For formulas that the user enters from standard input (rather than from
  415. the command line), a line continuation feature now exists. If the user
  416. adds \e to the end of an input line, <em>r3.mapcalc</em> assumes that the
  417. formula being entered by the user continues on to the next input line.
  418. There is no limit to the possible number of input lines or to the length
  419. of a formula.
  420. <p>If the <em>r3.mapcalc</em> formula entered by the user is very long, the
  421. map title will contain only some of it, but most (if not all) of the formula
  422. will be placed into the history file for the <em>result</em> map.
  423. <p>The environment variable GRASS_RND_SEED is read to initialise the
  424. random number generator.
  425. <h2>BUGS</h2>
  426. Continuation lines must end with a \ and have NO trailing white space
  427. (blanks or tabs). If the user does leave white space at the end of
  428. continuation lines, the error messages produced by <em>r3.mapcalc</em> will
  429. be meaningless and the equation will not work as the user intended.
  430. This is important for the eval() function.
  431. <p><!-- STILL TRUE ??-->
  432. Currently, there is no comment mechanism in <em>r3.mapcalc</em>. Perhaps
  433. adding a capability that would cause the entire line to be ignored when
  434. the user inserted a # at the start of a line as if it were not present,
  435. would do the trick.
  436. <p>The function should require the user to type "end" or "exit" instead
  437. of simply a blank line. This would make separation of multiple scripts
  438. separable by white space.
  439. <p>r3.mapcalc does not print a warning in case of operations on NULL cells.
  440. It is left to the user to utilize the isnull() function.
  441. <h2>SEE ALSO</h2>
  442. <b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc-algebra.pdf">r3.mapcalc: An Algebra for GIS and Image
  443. Processing</a></b>, by Michael Shapiro and Jim Westervelt, U.S. Army
  444. Construction Engineering Research Laboratory (March/1991).
  445. <p>
  446. <b><a href="http://grass.osgeo.org/uploads/grass/history_docs/mapcalc.pdf">Performing Map Calculations on GRASS Data:
  447. r.mapcalc Program Tutorial</a></b>, by Marji Larson, Michael Shapiro and Scott
  448. Tweddale, U.S. Army Construction Engineering Research Laboratory (December
  449. 1991)
  450. <p><em><a href="r.mapcalc.html">r.mapcalc</a></em>
  451. <h2>AUTHORS</h2>
  452. Tomas Paudits &amp; Jaro Hofierka, funded by GeoModel s.r.o., Slovakia
  453. <br><a href="mailto:tpaudits@mailbox.sk">tpaudits@mailbox.sk</a>,
  454. <a href="MAILTO:hofierka@geomodel.sk">hofierka@geomodel.sk</a>
  455. <p><i>Last changed: $Date$</i>