How to use r.what in rgrass7?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use r.what in rgrass7?

Markus Neteler
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=",")

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
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Reply | Threaded
Open this post in threaded view
|

Re: How to use r.what in rgrass7?

RKrug
Markus Neteler <[hidden email]> writes:

> 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=",")
Which data set do you mean? I don't have the maps in the
nc_basic_spm_grass7 dataset?

did you try to use intern=TRUE :

,----
| intern: default NULL, in which case set internally from
|           ‘get.useInternOption’; a logical (not 'NA') which indicates
|           whether to make the output of the command an R object. Not
|           available unless 'popen' is supported on the platform
`----

Other options to try:

,----
| Sys_ignore.stdout, Sys_wait, Sys_input: pass extra arguments to
|           ‘system’
|
| Sys_show.output.on.console, Sys_minimized, Sys_invisible: pass extra
|           arguments to ‘system’ on Windows systems only
`----

Cheers,

Rainer

>
> 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
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-stats
--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      [hidden email]

Skype:      RMkrug

PGP: 0x0F52F982

_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats

signature.asc (490 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to use r.what in rgrass7?

Roger Bivand
In reply to this post by Markus Neteler
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
> [hidden email]
> 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: [hidden email]

_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
NHH Norwegian School of Economics, Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: How to use r.what in rgrass7?

Markus Neteler
In reply to this post by RKrug
On Thu, Sep 10, 2015 at 1:19 PM, Rainer M Krug <[hidden email]> wrote:

> Markus Neteler <[hidden email]> writes:
>
>> 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=",")
>
> Which data set do you mean? I don't have the maps in the
> nc_basic_spm_grass7 dataset?

I am using "nc_spm_08_grass7" (the full sample dataset).


> did you try to use intern=TRUE :

Great, intern=TRUE does the job (warnings still printed which is fine):

> goutput <- execGRASS("r.what", map="elev_state_500m",
+ points="geodetic_pts", separator=",", intern=TRUE)
Warning message:
In execGRASS("r.what", map = "elev_state_500m", points = "geodetic_pts",  :
  The command:
r.what map=elev_state_500m points=geodetic_pts separator=,
produced at least one warning during execution:
WARNING: 375528.09289275 323036.936425953 is outside your current region
WARNING: 366626.43789275 326709.311425953 is outside your current region
WARNING: 373413.90589275 321705.812425953 is outside your current region
WARNING: 121862.14889275 154448.453425953 is outside your current region
WARNING: 500126.65589275 327185.625425953 is outside your current region
WARNING: 497992.12589275 319289.780425953 is outside your current region
WARNING: 446393.81189275 320264.688425953 is outside your current region
WARNING: 446144.84289275 320081.218425953 is outside your current region
WARNING: 453882.74989275 319223.249425953 is outside your current region
>

Perhaps intern=TRUE should be the default when assigning to an object?

In any case, I now get

> str(goutput)
 chr [1:29939] "571530.81289275,265739.968425953,,187.8082200648" ...

which looks good and simplifies the rest:

> con <- textConnection(goutput)
> go1 <- read.csv(con, header=FALSE)
> str(go1)
'data.frame':    29939 obs. of  4 variables:
 $ V1: num  571531 571359 571976 572391 573011 ...
 $ V2: num  265740 265987 267049 267513 269615 ...
 $ V3: logi  NA NA NA NA NA NA ...
 $ V4: Factor w/ 22738 levels "-0.0048115728",..: 6859 6642 6749 6411
6356 6904 7506 7224 6908 7167 ...

Thanks for the help!

Markus

PS: I'll add that to the rgrass7 Wiki page.
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats