[GRASS-user] combining r.cross and reclassification

Glynn Clements glynn at gclements.plus.com
Wed Aug 13 10:34:00 EDT 2008

Wout Bijkerk wrote:

> As a former user of SPANS, I was charmed by its possibillity of making a
> cross of two raster maps and reclassifying it as well within one module.
> It worked with a table written in a text file, where the row and column
> headers represent the category values of the input maps, while the cells
> give the category values that are assigned to the combination of the two
> input maps. For exaple, suppose we heve two suitabillity assignmens for
> different criteria (say temperature and soil-type, where 1 is not
> suitable, 2 is marginally suitable and 3 is well suitable) and we want
> to make an overall suitabillity. I then made a table as below:
>         1   2   3    
> 1      1   1   1
> 2      1   1  2 
> 3      1   2   3
> This table was the input for the crossing of the suitabillity map for
> temparature and the suitabillity map for soils. The result was an new
> raster map, with categories ranging from 1 - 3.
> Is there something like this in Grass as well? Of course one can write
> long if-statements with mapcalc, but the way SPANS had it organised was
> more clear and more flexible.

One option would be to reclass the output from r.cross, but the way
that it labels the categories is inconvenient for this purpose.

If you don't need r.cross' ability to minimise the number of
categories, you could just use e.g.:

	r.mapcalc "result = temperature * 10 + suitability"

This will produce category numbers which are more suitable for
reclassing (i.e. category 23 => temperature = 2, suitability = 3").

You can generate the reclass rules from a 2D table such as the above
with an awk script, e.g.:

#!/usr/bin/awk -f
(NR==1) {

(NR>1) {
    n = split($0,col)
    rcat = $1
    for (i = 1; i < n; i++) {
	ccat = ccats[i]
	printf "%d = %d\n", rcat * 10 + ccat, col[i+1]

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list