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

Martin Wegmann wegmann at biozentrum.uni-wuerzburg.de
Mon Jan 17 10:36:14 EST 2005


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
------------------




More information about the grass-user mailing list