[GRASS-user] compositing rasters

Colleagues,

    I know this must be easy, but I haven't found it.

    Suppose I have three grids (a, b, and c) where each grid is 0 or 1, and the 1s are mutually exclusive. I want a new grid where if grid
a=1 then newgrid = 1; if grid b=1 then new grid = 2; if grid c=1 then newgrid = 3.

     Something like r.mapcalc "new=if(a,1) || if(b,2) || if(c,3)"

seems like it ought to work but I can't seem to use multiple input file that way.

Thanks in advance for any help, Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts@montana.edu
Montana State University
Bozeman, MT 59717-3460

Friends,

    I did get an off-list solution (thanks Jiao) that works for two rasters but not for n (using the implied else).

    I think what GRASS needs is an r.update routine that works like an SQL update statement, i.e.

r.update map=composite.map value=1 where="map a = 1"
r.update map=composite.map value=2 where="map b = 1"
etc

     This would solve my immediate problem but also have general utility. I can export the grids and do it in FORTRAN easily enough, but I haven't cracked the GRASS API yet to actually make a GRASS function out of it.

Maybe there already is such a thing?

Thanks, Dave

On 12/31/2013 06:00 PM, Dave Roberts wrote:

Colleagues,

    I know this must be easy, but I haven't found it.

    Suppose I have three grids (a, b, and c) where each grid is 0 or 1,
and the 1s are mutually exclusive. I want a new grid where if grid
a=1 then newgrid = 1; if grid b=1 then new grid = 2; if grid c=1 then
newgrid = 3.

     Something like r.mapcalc "new=if(a,1) || if(b,2) || if(c,3)"

seems like it ought to work but I can't seem to use multiple input file
that way.

Thanks in advance for any help, Dave

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts@montana.edu
Montana State University
Bozeman, MT 59717-3460

Dave Roberts dvrbts@ecology.msu.montana.edu wrote:

Colleagues,

I know this must be easy, but I haven't found it.

Suppose I have three grids (a, b, and c) where each grid is 0 or 1, 
and the 1s are mutually exclusive.  I want a new grid where if grid
a=1 then newgrid = 1; if grid b=1 then new grid = 2; if grid c=1 then 
newgrid = 3.

Something like  r.mapcalc "new=if(a,1) || if(b,2) || if(c,3)"

seems like it ought to work but I can't seem to use multiple input file 
that way.

Thanks in advance for any help, Dave

What about

r.mapcalc new=a + b2 + c3

If the maps are mutually exclusive, only one of the three will be non-zero for each cell.

Tyler

Tyler,

     Well, that's simply brilliant. I knew it should be easy. Plus, when I tested it I found out my rasters weren't truly mutually exclusive after all, as some cells = 3. This is apparently an artifact of v.to.rast polygon boundary issues but it's a pretty small problem.

     Nonetheless, I have begun work on the r.update function I mentioned in my last post. It has general utility (e.g. fixing the non-exclusive problems I just mentioned) and seems easy enough as a crude hack. If it shows general utility I'll post it and see if someone with GRASS coding skills wants to take it up in C with the API.

Thanks Tyler!

On 01/01/2014 09:56 AM, Tyler Smith wrote:

Dave Roberts <dvrbts@ecology.msu.montana.edu> wrote:

    Colleagues,

    I know this must be easy, but I haven't found it.

    Suppose I have three grids (a, b, and c) where each grid is 0 or 1,
    and the 1s are mutually exclusive. I want a new grid where if grid
    a=1 then newgrid = 1; if grid b=1 then new grid = 2; if grid c=1 then
    newgrid = 3.

    Something like r.mapcalc "new=if(a,1) || if(b,2) || if(c,3)"

    seems like it ought to work but I can't seem to use multiple input file
    that way.

    Thanks in advance for any help, Dave

What about

r.mapcalc new=a + b*2 + c*3

If the maps are mutually exclusive, only one of the three will be
non-zero for each cell.

Tyler

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts@montana.edu
Montana State University
Bozeman, MT 59717-3460