[GRASS-stats] How to use r.what in rgrass7?

Roger Bivand Roger.Bivand at nhh.no
Thu Sep 10 04:39:58 PDT 2015


On Thu, 10 Sep 2015, Markus Neteler wrote:

> Dear list,
>
> I try to query from R a raster map at given vector point positions
> (both maps are in GRASS but it could well be that the points are in R
> and the (huge) raster map in GRASS).
>
> So, simple North Carolina data set example:
>
> goutput <- execGRASS("r.what", map="elev_state_500m",
> points="geodetic_pts", separator=",")

Did you try the intern=TRUE argument - it is NULL by default and taken 
from get.useInternOption(), initialised on load to FALSE?

What you get without is:

> goutput <- execGRASS("r.what", map="elevation", points="elev_points", 
separator=",")
...
> str(goutput)
  atomic [1:1] 0
  - attr(*, "resOut")= chr [1:6000] "638381.5,220749.5,,128.7498" 
"638400.5,220749.5,,129.5774" "638403.5,220749.5,,129.5774" 
"638441.5,220749.5,,131.3128" ...
  - attr(*, "resErr")= chr(0)

with the data as x,y,z,value:

> con <- textConnection(attr(goutput, "resOut"))
> go1 <- read.csv(con, header=FALSE)
> str(go1)
'data.frame':	6000 obs. of  4 variables:
  $ V1: num  638382 638400 638404 638442 638470 ...
  $ V2: num  220750 220750 220750 220750 220750 ...
  $ V3: logi  NA NA NA NA NA NA ...
  $ V4: num  129 130 130 131 132 ...
> close(con)

since we are splitting stdout and stderr results.

> goutput <- execGRASS("r.what", map="elevation", points="elev_points", 
separator=",", intern=TRUE)
> str(goutput)
  chr [1:6000] "638381.5,220749.5,,128.7498" ...

Now:

> con <- textConnection(goutput)
> go1 <- read.csv(con, header=FALSE)
> str(go1)
'data.frame':	6000 obs. of  4 variables:
  $ V1: num  638382 638400 638404 638442 638470 ...
  $ V2: num  220750 220750 220750 220750 220750 ...
  $ V3: logi  NA NA NA NA NA NA ...
  $ V4: num  129 130 130 131 132 ...
> close(con)

You'd need to check:

> ep <- readVECT("elev_points")
> all.equal(go1$V1, coordinates(ep)[,1])
[1] TRUE

but it looks OK.

Roger

>
> I would expect that the result is stored in "goutput" (seems it is)
> but it also dumps it into the terminal which I would like to avoid. I
> don't manage to switch that off.
>
> Then, following this blog
> https://pvanb.wordpress.com/2013/01/23/import-grass-function-console-output-as-data-frame-in-r/
>
> I tried to parse the output:
> con <- textConnection(goutput)
>
> which fails with
> Error in textConnection(goutput) : invalid 'text' argument
>
> Does anyone have a suggestion how to turn the output of r.what into an R object?
>
> Likewise suggestions are welcome how to feed R point positions into
> r.what from R.
>
> Hope I am not asking too much :-)
>
> thanks,
> Markus
> _______________________________________________
> grass-stats mailing list
> grass-stats at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-stats
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the grass-stats mailing list