Friends,
After some alternative approaches to a simple problem of compositing rasters (see previous posts, esp. compositing rasters @9:32)
I developed a very crude function to update rasters
r.update target mask x y
updates raster target by substituting y everywhere that raster mask has x. A more GRASS-like syntax would be
r.update target=string mask=string current=integer replace=integer
but my bash skills are pretty limited and I elected (for the time being) not to parse the arguments that way. The current function relies on a short bash script and a FORTRAN executable. The crude part is exporting both the target an mask rasters as ascii exports, creating a new ascii file, and doing an r.in.ascii to bring the updated raster back in. It only works (at present) for integer rasters, but it's intended for thematic maps, so that seems OK.
r.update.sh just below.
r.out.ascii inp=$1 out=$1.asc null=-1
r.out.ascii inp=$2 out=$2.asc null=-1
g.remove rast=$1
r_update $1.asc $2.asc $3 $4
r.in.ascii inp=tmp.file out=$1 nv=-1
rm $1.asc
rm $2.asc
rm tmp.file
You have to "alias r_update 'sh r_update.sh'" or name the script r.update and chmod +x r.update to make it run as shown.
The FORTRAN code for r_update is at
ecology.msu.montana.edu/GRASS/r_update.f90
The code compiles with gfortran. The executable must be in your path, or you can modify the bash script with a full path to it.
It is my sincere hope that someone with GRASS chops will write a function for g.extension that uses the API and avoids all this ascii input and output, but this serves as a demo and useful (if clumsy) approach in the meantime.
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
Hi Anna,
On 01/01/2014 01:50 PM, Anna Petrášová wrote:
Hi Dave,
On Wed, Jan 1, 2014 at 3:10 PM, Dave Roberts
<dvrbts@ecology.msu.montana.edu <mailto:dvrbts@ecology.msu.montana.edu>>
wrote:
Friends,
After some alternative approaches to a simple problem of
compositing rasters (see previous posts, esp. compositing rasters @9:32)
I developed a very crude function to update rasters
r.update target mask x y
updates raster target by substituting y everywhere that raster mask
has x. A more GRASS-like syntax would be
r.update target=string mask=string current=integer replace=integer
I'm not sure if I don't miss anything but why don't you use r.mapcalc
expression like this:
new = if(mask == x, y, target)
and then
g.rename new,target --overwrite
well probably because I'm not too bright. Obviously your elegant solution would allow multiple sequential updates just the way I needed as long as I do the rename --overwrite very time. I knew there had to e GRASS way to do this, I just hadn't figured it out yet.
Thanks!
Anna
but my bash skills are pretty limited and I elected (for the time
being) not to parse the arguments that way. The current function
relies on a short bash script and a FORTRAN executable. The crude
part is exporting both the target an mask rasters as ascii exports,
creating a new ascii file, and doing an r.in.ascii to bring the
updated raster back in. It only works (at present) for integer
rasters, but it's intended for thematic maps, so that seems OK.
r.update.sh <http://r.update.sh> just below.
r.out.ascii inp=$1 out=$1.asc null=-1
r.out.ascii inp=$2 out=$2.asc null=-1
g.remove rast=$1
r_update $1.asc $2.asc $3 $4
r.in.ascii inp=tmp.file out=$1 nv=-1
rm $1.asc
rm $2.asc
rm tmp.file
You have to "alias r_update 'sh r_update.sh'" or name the script
r.update and chmod +x r.update to make it run as shown.
The FORTRAN code for r_update is at
ecology.msu.montana.edu/GRASS/__r_update.f90
<http://ecology.msu.montana.edu/GRASS/r_update.f90>
The code compiles with gfortran. The executable must be in your
path, or you can modify the bash script with a full path to it.
It is my sincere hope that someone with GRASS chops will write
a function for g.extension that uses the API and avoids all this
ascii input and output, but this serves as a demo and useful (if
clumsy) approach in the meantime.
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~__~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~__~~~~~~~~~~~~
David W. Roberts office
406-994-4548 <tel:406-994-4548>
Professor and Head FAX
406-994-3190 <tel:406-994-3190>
Department of Ecology email
droberts@montana.edu <mailto:droberts@montana.edu>
Montana State University
Bozeman, MT 59717-3460
_________________________________________________
grass-user mailing list
grass-user@lists.osgeo.org <mailto:grass-user@lists.osgeo.org>
http://lists.osgeo.org/__mailman/listinfo/grass-user
<http://lists.osgeo.org/mailman/listinfo/grass-user>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
The other quick and dirty workaround could be a combination of r.reclass and r.series echo “1=2 \ *=NULL” | r.reclass in=b out=b2 echo “1=3 \ *=NULL” | r.reclass in=c out=c3 then: r.series in=a,b2,c3 out=target method=max Using method=max will avoid the problem of cells with values in more than one original. Cheers, Micha
···
On 01/02/2014 12:11 AM, Dave Roberts wrote:
Hi Anna,
On 01/01/2014 01:50 PM, Anna Petrášová wrote:
Hi Dave,
On Wed, Jan 1, 2014 at 3:10 PM, Dave Roberts
<dvrbts@ecology.msu.montana.edu mailto:dvrbts@ecology.msu.montana.edu>
wrote:
Friends,
After some alternative approaches to a simple problem of
compositing rasters (see previous posts, esp. compositing rasters @9:32)
I developed a very crude function to update rasters
r.update target mask x y
updates raster target by substituting y everywhere that raster mask
has x. A more GRASS-like syntax would be
r.update target=string mask=string current=integer replace=integer
I’m not sure if I don’t miss anything but why don’t you use r.mapcalc
expression like this:
new = if(mask == x, y, target)
and then
g.rename new,target --overwrite
well probably because I’m not too bright. Obviously your elegant solution would allow multiple sequential updates just the way I needed as long as I do the rename --overwrite very time. I knew there had to e GRASS way to do this, I just hadn’t figured it out yet.
Thanks!
Anna
but my bash skills are pretty limited and I elected (for the time
being) not to parse the arguments that way. The current function
relies on a short bash script and a FORTRAN executable. The crude
part is exporting both the target an mask rasters as ascii exports,
creating a new ascii file, and doing an r.in.ascii to bring the
updated raster back in. It only works (at present) for integer
rasters, but it’s intended for thematic maps, so that seems OK.
r.update.sh http://r.update.sh just below.
r.out.ascii inp=$1 out=$1.asc null=-1
r.out.ascii inp=$2 out=$2.asc null=-1
g.remove rast=$1
r_update $1.asc $2.asc $3 $4
r.in.ascii inp=tmp.file out=$1 nv=-1
rm $1.asc
rm $2.asc
rm tmp.file
You have to “alias r_update ‘sh r_update.sh’” or name the script
r.update and chmod +x r.update to make it run as shown.
The FORTRAN code for r_update is at
ecology.msu.montana.edu/GRASS/__r_update.f90
http://ecology.msu.montana.edu/GRASS/r_update.f90
The code compiles with gfortran. The executable must be in your
path, or you can modify the bash script with a full path to it.
It is my sincere hope that someone with GRASS chops will write
a function for g.extension that uses the API and avoids all this
ascii input and output, but this serves as a demo and useful (if
clumsy) approach in the meantime.
Dave
David W. Roberts office
406-994-4548 <tel:406-994-4548>
Professor and Head FAX
406-994-3190 <tel:406-994-3190>
Department of Ecology email
[droberts@montana.edu](mailto:droberts@montana.edu) [<mailto:droberts@montana.edu>](mailto:droberts@montana.edu)
Montana State University
Bozeman, MT 59717-3460
_________________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org) [<mailto:grass-user@lists.osgeo.org>](mailto:grass-user@lists.osgeo.org)
[http://lists.osgeo.org/__mailman/listinfo/grass-user](http://lists.osgeo.org/__mailman/listinfo/grass-user)
[<http://lists.osgeo.org/mailman/listinfo/grass-user>](http://lists.osgeo.org/mailman/listinfo/grass-user)