[GRASSLIST:5390] Script to 'sample' several grids with result saved to text output?

Dear R-geo and GRASS users,

-Before trying to reinvent the wheel I would like to check whether someone has tried this before.-

I have several rasters with environmental data. I also have point data, representing animal locations (collected with GPS collars).

I would like to use GRASS to 'sample' (apologies for the ArcInfo AML terminology, but it might help people to get what I need) the rasters with the point data and save the results in a text file for further analysis with R.

The resulting text file should contain a field/attribute of my choice from the points data file (some sort of ID for each point) and a field/attribute for each of the sampled rasters.

Preferably I would use an R script that calls GRASS, as both point data collection and further analysis is done within R. However a GRASS script working on input and output text files might also be sufficient.

Any help would be much appreciated,

Sander Oom.

PS: I am a total novice with GRASS.

On Monday 17 January 2005 15:59, Sander Oom wrote:

Dear R-geo and GRASS users,

-Before trying to reinvent the wheel I would like to check whether
someone has tried this before.-

I have several rasters with environmental data. I also have point data,
representing animal locations (collected with GPS collars).

I would like to use GRASS to 'sample' (apologies for the ArcInfo AML
terminology, but it might help people to get what I need) the rasters
with the point data and save the results in a text file for further
analysis with R.

The resulting text file should contain a field/attribute of my choice
from the points data file (some sort of ID for each point) and a
field/attribute for each of the sampled rasters.

Preferably I would use an R script that calls GRASS, as both point data
collection and further analysis is done within R. However a GRASS script
working on input and output text files might also be sufficient.

this script should do the work, just copy it into a txt file (make it
executable) and replace "rast1 rast2 ..." with your raster image names.
if you execute inside GRASS

sh name_of_text_file

you will be prompted to add you base file (your location file - must be a
raster not a vector file - just use v.to.rast).

your output will be csv files corresponding to your input raster images.
Finally you can just read these files into R via read.csv(). Be aware that all
raster values were multiplied with factor 100000, just divide you results in
R by 100000.

hope this script is what you want, probably there does also a direct-R-GRASS
solution exist, cheers Martin

------------

#!/bin/bash

# read in location file
echo -n "enter base ->"

read base

site=$base

#enter names of your raster files
for raster in rast1 rast2 rast3; do

r.mapcalc "rast1=int($raster*1000000)"

r.statistics base=$site cover=rast1 method=average out=rastavg

g.remove rast=rast1

#writing data to csv file
r.stats -l rastavg>>$raster.csv

g.remove rast=rastavg ; done

MESSAGE="finished"

echo $MESSAGE
------------------

On Mon, 17 Jan 2005, Sander Oom wrote:

Dear R-geo and GRASS users,

-Before trying to reinvent the wheel I would like to check whether
someone has tried this before.-

I have several rasters with environmental data. I also have point data,
representing animal locations (collected with GPS collars).

I would like to use GRASS to 'sample' (apologies for the ArcInfo AML
terminology, but it might help people to get what I need) the rasters
with the point data and save the results in a text file for further
analysis with R.

The resulting text file should contain a field/attribute of my choice
from the points data file (some sort of ID for each point) and a
field/attribute for each of the sampled rasters.

Preferably I would use an R script that calls GRASS, as both point data
collection and further analysis is done within R. However a GRASS script
working on input and output text files might also be sufficient.

Something like the following works with GRASS 5.4 and R 2.0.1, using the
spearfish test data set for GRASS 5.3:

rtmp <- tempfile()
system(paste("s.out.ascii bugsites | r.what -f vegcover,geology >",

+ rtmp))

system(paste("tail -5", rtmp))

601228|4917635||5|mixed forest|5|limestone
603146|4917453||3|coniferous forest|5|limestone
605627|4916428||3|coniferous forest|5|limestone
607098|4917437||3|coniferous forest|4|sandstone
608471|4915733||3|coniferous forest|8|claysand

input <- scan(rtmp, sep="|", what=list(x=numeric(0), y=numeric(0),

+ NULL, NULL, vegcover=character(0), NULL, geology=character(0)))
Read 90 records

str(input)

List of 7
$ x : num [1:90] 590232 590430 590529 590546 590612 ...
$ y : num [1:90] 4915039 4915204 4914625 4915353 4915320 ...
$ : NULL
$ : NULL
$ vegcover: chr [1:90] "coniferous forest" "coniferous forest"
"coniferous forest" "coniferous forest" ...
$ : NULL
$ geology : chr [1:90] "igneous" "igneous" "igneous" "sandstone" ...

input1 <- data.frame(input[c(1,2,5,7)])
str(input1)

`data.frame': 90 obs. of 4 variables:
$ x : num 590232 590430 590529 590546 590612 ...
$ y : num 4915039 4915204 4914625 4915353 4915320 ...
$ vegcover: Factor w/ 4 levels "coniferous forest",..: 1 1 1 1 1 4 1 1 1 1
$ geology : Factor w/ 5 levels "claysand","igneous",..: 2 2 2 5 5 5 5 3 2 5

If I had figured out how to read the output of r.what into s.in.ascii,
storing the data as a sites file, then using the R GRASS package and
sites.get() would have been neater, but this does work.

Hope this helps,

Roger Bivand

Any help would be much appreciated,

Sander Oom.

PS: I am a total novice with GRASS.

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand@nhh.no

I have several rasters with environmental data. I also have point
data, representing animal locations (collected with GPS collars).

I would like to use GRASS to 'sample' (apologies for the ArcInfo AML
terminology, but it might help people to get what I need) the rasters
with the point data and save the results in a text file for further
analysis with R.

The resulting text file should contain a field/attribute of my choice
from the points data file (some sort of ID for each point) and a
field/attribute for each of the sampled rasters.

Preferably I would use an R script that calls GRASS, as both point
data collection and further analysis is done within R. However a GRASS
script working on input and output text files might also be
sufficient.

r.what does this nicely:

http://grass.ibiblio.org/grass57/manuals/html57_user/r.what.html

I guess r.what could/should be modified to read from a sites (vector
points) list and write to a new sites (vector points) list with queried
attributes. (currently just pipe from stdin & to stdout)
Easy to do in a script I guess.

Roger:

If I had figured out how to read the output of r.what into s.in.ascii,
storing the data as a sites file

I don't think that would be too hard with GRASS 5.7/6:
v.in.ascii input=r_what_output.txt fs=',' cat=1 x=2 y=3

GRASS 5.0/5.4's s.in.ascii needs to rearrange column order though with
UNIX 'cut' and 'paste' commands first?

Hamish

On Monday 17 January 2005 06:59 am, Sander Oom wrote:

Dear R-geo and GRASS users,

-Before trying to reinvent the wheel I would like to check whether
someone has tried this before.-

I have several rasters with environmental data. I also have point data,
representing animal locations (collected with GPS collars).

I would like to use GRASS to 'sample' (apologies for the ArcInfo AML
terminology, but it might help people to get what I need) the rasters
with the point data and save the results in a text file for further
analysis with R.

The resulting text file should contain a field/attribute of my choice
from the points data file (some sort of ID for each point) and a
field/attribute for each of the sampled rasters.

Preferably I would use an R script that calls GRASS, as both point data
collection and further analysis is done within R. However a GRASS script
working on input and output text files might also be sufficient.

Any help would be much appreciated,

Sander Oom.

PS: I am a total novice with GRASS.

In the past I have used 's.sample' in GRASS54 to "sample" many rasters at
given site locations...

here is some sample code:

#export digitized gras57 vectors as sites (from GRASS57):
s.out.ascii input=b_sites > b_sites.xy

#load exported grass57 vector sites (GRASS53):
s.in.ascii sites=beam_sample_sites input=b_sites.xy

#sample the rasters in a for loop!
for x in `seq 1 364`; do a=`s.sample input=beam_sample_sites \
rast=beam$x -q -C | awk -F"%" '{ORS = " "}{print $2}'`; echo $x $a \
; done > data.out

Cheers

--
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341

On Tue, Jan 18, 2005 at 12:28:43PM +1300, Hamish wrote:

r.what does this nicely:

http://grass.ibiblio.org/grass57/manuals/html57_user/r.what.html

I guess r.what could/should be modified to read from a sites (vector
points) list and write to a new sites (vector points) list with queried
attributes. (currently just pipe from stdin & to stdout)

Isn't this functionality already there?
http://grass.itc.it/grass60/manuals/html60_user/v.what.rast.html

Markus