[GRASS5] r.mapcalc question

Since I'm relatively new to GRASS, I just ran across the r.mapcalc command, and I started playing with it for merging image data. I've tried the following, some which works (but it's annoying) and some which doesn't work but I'd like to know why. For both, assume the region is set correctly.

First of all, what works:
r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0.red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Thanks,
David

On Wed, 2 Feb 2005, David Piasecki wrote:

r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0. red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Assuming none of your non-NULL cells have a value of 0, perhaps convert all the nulls to 0 using r.null? What you have written probably would have worked in GRASS 4 before NULLs were introduced. In GRASS 5, if any of the input maps for a cell in r.mapcalc are null then the output cell is null.

Something like
r.null image0.red null=0
r.null image1.red null=0
might do what you want. But your first way is more correct IMHO and probably the way it should be done.

Paul

what about r.patch?

else one could write some perl script, which would generate the sctring, full
    of 'if(isnull(.....' for mapcalc.

    Jáchym

On Wed, Feb 02, 2005 at 11:14:35AM -0800, David Piasecki wrote:

Since I'm relatively new to GRASS, I just ran across the r.mapcalc
command, and I started playing with it for merging image data. I've
tried the following, some which works (but it's annoying) and some
which doesn't work but I'd like to know why. For both, assume the
region is set correctly.

First of all, what works:
r.mapcalc
'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0.
red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating
these expressions since image0.red is null wherever image1.red is not
null. However, no error messages are given. Is there an easier way to
do this than the first command given above?

Thanks,
David

_______________________________________________
grass5 mailing list
grass5@grass.itc.it
http://grass.itc.it/mailman/listinfo/grass5

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://www.fle.czu.cz/~jachym/gnupg_public_key/

On Wed, 2 Feb 2005, Jachym Cepicky wrote:

what about r.patch?

Oh right I didn't understand the problem. Yes I agree too that r.patch is the answer for merging 42 images together like this.

Paul

Yeah, I'm not sure if the r.null thing will work since the images are of adjacent coordinates. I'm trying to build a big map out of smaller ones, and I was looking for something faster than r.patch.

David

On Feb 2, 2005, at 12:37 PM, Paul Kelly wrote:

On Wed, 2 Feb 2005, David Piasecki wrote:

r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0. red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Assuming none of your non-NULL cells have a value of 0, perhaps convert all the nulls to 0 using r.null? What you have written probably would have worked in GRASS 4 before NULLs were introduced. In GRASS 5, if any of the input maps for a cell in r.mapcalc are null then the output cell is null.

Something like
r.null image0.red null=0
r.null image1.red null=0
might do what you want. But your first way is more correct IMHO and probably the way it should be done.

Paul

Actually, I'm starting to think r.null may work for this purpose. Since each image is completely separate from the others, there is no overlap, and I can combine them all in 2 commands instead of 42.

Thanks,
David

On Feb 2, 2005, at 12:37 PM, Paul Kelly wrote:

On Wed, 2 Feb 2005, David Piasecki wrote:

r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0. red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Assuming none of your non-NULL cells have a value of 0, perhaps convert all the nulls to 0 using r.null? What you have written probably would have worked in GRASS 4 before NULLs were introduced. In GRASS 5, if any of the input maps for a cell in r.mapcalc are null then the output cell is null.

Something like
r.null image0.red null=0
r.null image1.red null=0
might do what you want. But your first way is more correct IMHO and probably the way it should be done.

Paul

Hmmm... maybe r.null isn't such a good idea after all. Turns out that because my small images do not encompass the full extent of the region, adding them together after doing an r.null on the large map doesn't work. The null values creep in from all the small images. Dang! Well, I guess I'll think of something.

Thanks,
David

On Feb 2, 2005, at 12:37 PM, Paul Kelly wrote:

On Wed, 2 Feb 2005, David Piasecki wrote:

r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0. red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Assuming none of your non-NULL cells have a value of 0, perhaps convert all the nulls to 0 using r.null? What you have written probably would have worked in GRASS 4 before NULLs were introduced. In GRASS 5, if any of the input maps for a cell in r.mapcalc are null then the output cell is null.

Something like
r.null image0.red null=0
r.null image1.red null=0
might do what you want. But your first way is more correct IMHO and probably the way it should be done.

Paul

Paul,

You were right. Seems the best plan of action is to do the following:

r.mapcalc calc.red=image0.red // creates the image with the correct region information
r.mapcalc 'calc.red=if(isnull(calc.red),if(not(isnull(image1.red)),image1.red,if(not(isnull(image2.red)),image2.red)),calc.red)' // and so on... imageN.red

// do the same for green and blue

r.composite r_map=calc.red g_map=calc.green b_map=calc.blue output=cmap // form an rgb composite image

And now I can do whatever I want with it :slight_smile:

Perhaps this isn't quite the fastest method, but it works. If anyone knows of a more efficient way of doing the same thing, I'd be interested in taking suggestions.

Thanks,
David

On Feb 2, 2005, at 12:37 PM, Paul Kelly wrote:

On Wed, 2 Feb 2005, David Piasecki wrote:

r.mapcalc 'calc.red=if(isnull(image0.red),image1.red,if(isnull(image1.red),image0. red))'

That's great; however, I have 42 images. So I tried some logic...
r.mapcalc 'calc.red=( image0.red || image1.red )'
r.mapcalc 'calc.red=( image0.red + image1.red )'

I think these aren't working because GRASS has a problem evaluating these expressions since image0.red is null wherever image1.red is not null. However, no error messages are given. Is there an easier way to do this than the first command given above?

Assuming none of your non-NULL cells have a value of 0, perhaps convert all the nulls to 0 using r.null? What you have written probably would have worked in GRASS 4 before NULLs were introduced. In GRASS 5, if any of the input maps for a cell in r.mapcalc are null then the output cell is null.

Something like
r.null image0.red null=0
r.null image1.red null=0
might do what you want. But your first way is more correct IMHO and probably the way it should be done.

Paul

Perhaps this isn't quite the fastest method, but it works. If anyone
knows of a more efficient way of doing the same thing, I'd be
interested in taking suggestions.

so what is wrong with r.patch exactly? If it does what you want it's a
bit less prone to user error than r.mapcalc and in theory it should
cycle through the raster cells at about the same speed?

r.patch can take a couple hundred input maps at once (in preferential order).

Hamish

When I tried running r.patch, it took a really long time. Since I'd never used r.mapcalc before, I thought it might be faster. The line I wrote in r.mapcalc seems to be checking if a pixel is null, then finding the image with that pixel and drawing it. I assumed r.patch was writing all pixels from every image every time. If it's not any faster, then yes, I might as well use r.patch.

Although my r.mapcalc line worked the first time, I have yet to be able to repeat it. For some reason, the region information changes to the (x,y) projection, and then I assume it's attempting to draw someplace else. So I may end up returning to r.patch anyway.

David

On Feb 2, 2005, at 5:26 PM, Hamish wrote:

Perhaps this isn't quite the fastest method, but it works. If anyone
knows of a more efficient way of doing the same thing, I'd be
interested in taking suggestions.

so what is wrong with r.patch exactly? If it does what you want it's a
bit less prone to user error than r.mapcalc and in theory it should
cycle through the raster cells at about the same speed?

r.patch can take a couple hundred input maps at once (in preferential order).

Hamish

Of course it was my fault the region changed. Anyway, the r.mapcalc way works, so I can compare speed issues to r.patch. At first glance, it seems faster, but the real test is when I throw the full dataset at it.

Thanks again,
David

On Feb 2, 2005, at 5:26 PM, Hamish wrote:

Perhaps this isn't quite the fastest method, but it works. If anyone
knows of a more efficient way of doing the same thing, I'd be
interested in taking suggestions.

so what is wrong with r.patch exactly? If it does what you want it's a
bit less prone to user error than r.mapcalc and in theory it should
cycle through the raster cells at about the same speed?

r.patch can take a couple hundred input maps at once (in preferential order).

Hamish

Yeah, I think r.patch may, in fact, run faster, but it sounds like you already knew this. Anyway, I've got another small question. Right now I'm determining what the region encompassing a set of jpg images is by doing a d.rast -o image# for all images, followed by a d.extend. That works, but it's kind of annoying to have to draw to a graphics monitor when I don't need to. Is there a way to set the region based on a collection of raster data without having to draw?

Thanks,
David

On Feb 2, 2005, at 5:26 PM, Hamish wrote:

Perhaps this isn't quite the fastest method, but it works. If anyone
knows of a more efficient way of doing the same thing, I'd be
interested in taking suggestions.

so what is wrong with r.patch exactly? If it does what you want it's a
bit less prone to user error than r.mapcalc and in theory it should
cycle through the raster cells at about the same speed?

r.patch can take a couple hundred input maps at once (in preferential order).

Hamish

David Piasecki wrote:

Anyway, I've got another small question. Right now
I'm determining what the region encompassing a set of jpg images is by
doing a d.rast -o image# for all images, followed by a d.extend. That
works, but it's kind of annoying to have to draw to a graphics monitor
when I don't need to. Is there a way to set the region based on a
collection of raster data without having to draw?

In 5.7, you can use:

  g.region raster=map1,map2,map3,...

This will set the region to encompass all of the specified raster
maps.

BTW, the manpage is out of date; it implies that the raster= option
only accepts a single argument.

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

Great! Thanks!

David

On Feb 7, 2005, at 12:10 PM, Glynn Clements wrote:

David Piasecki wrote:

Anyway, I've got another small question. Right now
I'm determining what the region encompassing a set of jpg images is by
doing a d.rast -o image# for all images, followed by a d.extend. That
works, but it's kind of annoying to have to draw to a graphics monitor
when I don't need to. Is there a way to set the region based on a
collection of raster data without having to draw?

In 5.7, you can use:

  g.region raster=map1,map2,map3,...

This will set the region to encompass all of the specified raster
maps.

BTW, the manpage is out of date; it implies that the raster= option
only accepts a single argument.

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