[GRASS-user] r3.mapcalc / r.mapcalc: Logical Operators &&, || behave differently

Glynn Clements glynn at gclements.plus.com
Wed Aug 20 13:14:10 PDT 2014


"Peter Löwe" wrote:

> for r3.mapcalc and r.mapcalc, there seems to be a difference
> regarding the logical operators (&& ||, etc.):
> 
> Assuming that two raster maps BAR and BAZ exist, the following
> statement creates new map content FOO whenever BAZ and BAR are not
> null:
> 
> r.mapcalc FOO='if((BAZ && BAR),42,null())'

Not quite.

The result will be 42 if both BAZ and BAR are non-null and non-zero. 
If either are null or either are zero, the result will be null.

Regarding &&:

* A && B is null if either A or B are null, otherwise ...

* A && B is zero if either A or B are zero, otherwise ...

* A && B is one.

Regarding if():

* if(A,B,C) is null if A is null, otherwise ...

* if(A,B,C) is B if A is non-zero, otherwise ...

* if(A,B,C) is C

So if either BAZ or BAR are null, (BAZ && BAR) will be null and the
if() function will evaluate to null regardless of its second and third
arguments.

If neither are null, then (BAZ && BAR) will be 0 if either BAZ or BAR
are zero, and 1 if both are non-zero.

> Doing the same with volumes (BAZ3, BAR3) results in an error message:
> 
> r3.mapcalc FOO3='if((BAZ3 && BAR3),42,null())'

FWIW, you should change the quoting to:

	r3.mapcalc 'FOO3 = if((BAZ3 && BAR3),42,null())'

This will work in both 6.x and 7.x, whereas your original version will
only work in 6.x (in 7.x, r.mapcalc/r3.mapcalc use the GRASS argument
parser, so the expression musn't match the "option=value" syntax;
adding spaces around the "=" avoids this issue).

> =>
> 
> Incorrect argument types to function or()
> Parse error
> 
> Where's my mistake ?

I suspect that either BAZ3 or BAR3 is a floating-point volume. The
arguments to logical operators must be integers.

Also, I don't see how you can get a reference to "or()" from an
expression involving only "&&".

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list