rsbivand / rgrass

Interpreted interface between the GRASS geographical information system and R
https://rsbivand.github.io/rgrass/
24 stars 8 forks source link

initGRASS(): relax the need for explicit gisBase and override=TRUE #73

Closed florisvdh closed 1 year ago

florisvdh commented 1 year ago

Using GRASS_INSTALLATION

The essential piece is bc68d50, which fixes #71: the value of an environment variable GRASS_INSTALLATION, if present, is used for gisBase if gisBase is not provided. This makes initGRASS() behave as documented on that matter. If no GRASS_INSTALLATION is present, then the fallback with grass --config path is tried which @Robinlovelace had put in place in #63 (now using shell() in case of Windows, which may actually work in a OSGeo4W shell; not tested). Corresponding messages have been added or tweaked.

This gives us:

> initGRASS(override = TRUE)
No gisBase set. Trying to detect from the GRASS_INSTALLATION environment variable.
No GRASS_INSTALLATION environment variable was found.
Trying to set gisBase by running command `grass --config path` (requires grass in the system PATH).
Taking gisBase value from `grass --config path` output: /usr/lib/grass82
gisdbase    /tmp/RtmprC0NSo 
location    file35744eb85606 
mapset      file3574817ddd3 
rows        1 
columns     1 
north       1 
south       0 
west        0 
east        1 
nsres       1 
ewres       1 
Warning message:
In execGRASS("g.region", flags = c("g", "3"), intern = TRUE, ignore.stderr = ignore.stderr) :
  The command:
g.region -g -3
produced at least one warning during execution:
WARNING: <PROJ_INFO> file not found for location <file35744eb85606>
WARNING: <PROJ_UNITS> file not found for location <file35744eb85606>
> 
> Sys.setenv(GRASS_INSTALLATION="/usr/lib/grass82")
>
> initGRASS(override = TRUE)
No gisBase set. Trying to detect from the GRASS_INSTALLATION environment variable.
Taking gisBase value from GRASS_INSTALLATION: /usr/lib/grass82
gisdbase    /tmp/RtmprC0NSo 
location    file35746f90f466 
mapset      file3574224c3933 
rows        1 
columns     1 
north       1 
south       0 
west        0 
east        1 
nsres       1 
ewres       1 
Warning message:
In execGRASS("g.region", flags = c("g", "3"), intern = TRUE, ignore.stderr = ignore.stderr) :
  The command:
g.region -g -3
produced at least one warning during execution:
WARNING: <PROJ_INFO> file not found for location <file35746f90f466>
WARNING: <PROJ_UNITS> file not found for location <file35746f90f466>

This has also been documented in the use.Rmd vignette.

Behaviour when GISRC or GIS_LOCK are present

Secondly, this PR tries to make initGRASS() postpone the error if GISRC or GIS_LOCK are present, and when the user did not provide the override argument. These shell variables remain present after a first-time call to initGRASS() while still in the same shell (~ RStudio) session. The corresponding GISRC file may be more persistent across sessions.

77ba7ff: essentially, if initGRASS() is executed interactively and override is missing, then user will be asked what (s)he wants, allowing to override. This may be debated, as nothing is wrong with current behaviour – throwing error if override=FALSE (the default) in such case – so this is only a matter of how the user is treated. I think that in almost all cases, the user will want to override but just forgot to do so. The dialog with the user will tell about override=TRUE, just like the error did. If explicitly passing override=FALSE, then nothing changes.

As a bonus, initGRASS() works out of the box without arguments in more cases, which is what @Robinlovelace was trying in https://github.com/rsbivand/rgrass/pull/63#issuecomment-1222541153. However, the user must interact and is taught to add override=TRUE.

This was just an idea that I got by reading https://github.com/rsbivand/rgrass/pull/63#issuecomment-1222541153, feel free to drop it.

> initGRASS()
A GRASS location /home/floris/.grassrc8 is already in use.
Do you want to override ('no' will abort)? (y/n) y
Overriding. Avoid this question by setting override = TRUE
No gisBase set. Trying to detect from the GRASS_INSTALLATION environment variable.
Taking gisBase value from GRASS_INSTALLATION: /usr/lib/grass82
gisdbase    /tmp/RtmprC0NSo 
location    file3574cfdfa19 
mapset      file35742aae9377 
rows        1 
columns     1 
north       1 
south       0 
west        0 
east        1 
nsres       1 
ewres       1 
Warning message:
In execGRASS("g.region", flags = c("g", "3"), intern = TRUE, ignore.stderr = ignore.stderr) :
  The command:
g.region -g -3
produced at least one warning during execution:
WARNING: <PROJ_INFO> file not found for location <file3574cfdfa19>
WARNING: <PROJ_UNITS> file not found for location <file3574cfdfa19>
> 
> initGRASS()
A GRASS location /home/floris/.grassrc8 is already in use.
Do you want to override ('no' will abort)? (y/n) n
Error: Aborting. To override, set override = TRUE
> 
> initGRASS(override=FALSE)
Error: A GRASS location /home/floris/.grassrc8 is already in use; to override, set override = TRUE
florisvdh commented 1 year ago

6b5f5ac updates the rather misleading message:

A GRASS location /home/floris/.grassrc8 is already in use.

to:

A GRASS location (defined by /home/floris/.grassrc8) is already in use.