[GRASS5] region growing - r.mapcalc bug?

Glynn Clements glynn at gclements.plus.com
Fri Mar 4 04:32:43 EST 2005


Jachym Cepicky wrote:

> But I need to detect neighbourhood of the central point, if it is
> "notnull()"...

> OOh, I have it: 
>     if(x) = TRUE if x != 0, but
>     if(x) = TRUE if isnull(x) 

No. if(x) is NULL if x is NULL.

For the one argument form:

	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).

For the two argument form:

	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).

For the three argument form:

	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).

For the four argument form:

	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

More generally, all operators and most functions return NULL if *any*
of their arguments are NULL.

if(), isnull() and eval() are exceptions.

isnull() returns 1 if its argument is NULL and 0 otherwise. If you
want the opposite, use the ! operator, e.g. "!isnull(x)".

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.:

	if(0,a,b) = b	regardless of whether a is NULL
	if(1,a,b) = a	regardless of whether b is NULL

eval() always returns its last argument, so it only returns NULL if
the last argument is NULL.

Note that you 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.

The behaviour makes sense if you consider 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, you don't know whether or not they both have the same value.

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




More information about the grass-dev mailing list