[GRASSLIST:1223] Re: Combining raster layers with r.mapcalc

Glynn Clements glynn.clements at virgin.net
Fri Sep 12 18:10:17 EDT 2003


Bill Dickinson Jr wrote:

> In running through the SEEDS tutorial, one of the tasks is combining 
> two raster layers into a single raster using r.mapcalc. The 
> individual layers show the data properly, but the resulting combined 
> layer contains no data. Here is what I do:
> 
> Mapset <wbdickinson> in Location <leics2>
> GRASS 5.0.2 > r.mapcalc
> Enter expressions, "end" when done.
> mapcalc> buffers = mwaybuf + railbuf
> mapcalc> end

The SEEDS tutorial was written for GRASS 4.x. One of the main changes
between 4.x and 5.x was the introduction of a distinguished "null"
(aka "no-data") value; 4.x used to use zero for this purpose (as well
as to represent actual zero values).

Whereas the 4.x version of r.buffer set cells outside of the buffer
zone to zero, the 5.x version sets them to null. The behaviour of
r.mapcalc is that, in most cases (and this is true of addition),
whenever one of the operands is null, the result is null.

So, in 4.x the above expression would result in computations such as:

	x + 0		=> x
	0 + y		=> y
	x + y		=> x + y

while in 5.x it would result in:

	x + null	=> null
	null + y	=> null
	x + y		=> x + y

Consequently, your result map is (almost) entirely null.

Possible solutions:

1. Use r.null to first change the nulls into zeroes:

	r.null map=mwaybuf null=0
	r.null map=railbuf null=0

then use r.mapcalc as above.

2. Re-write the r.mapcalc expression as:

GRASS 5.0.2 > r.mapcalc
mapcalc> buffers = if(isnull(mwaybuf),0,mwaybuf) + if(isnull(railbuf),0,railbuf)
mapcalc> end

3. Use r.patch instead of r.mapcalc:

	r.patch input=mwaybuf,railbuf output=buffers

This last option won't produce quite the same result, although it
might actually be more useful.

-- 
Glynn Clements <glynn.clements at virgin.net>




More information about the grass-user mailing list