[GIS] System call gdal function in R on Mac

gdalmacr

Hopefully someone can help me figure this issue out. I am trying to system call gdal functions inside of R using the system / system2 functions. Here is an example of the code:

> system("gdalinfo --version")
sh: gdalinfo: command not found

> system2("gdalinfo --version")
sh: gdalinfo --version: command not found

As you can see system cannot call the gdal functions. Anyone know how to fix this?

Inside of the Mac terminal, I can call any of the gdal command line functions:

user-MacBook-Pro:~ user$ gdalinfo --version
GDAL 1.10.1, released 2013/08/26

Here is a Sys.info — I am running Yosemite on Mac 10.10.1:

Sys.info()
# sysname        "Darwin"                                                                                         
# release        "14.0.0"                                                                                         
# version        "Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64"
# nodename       "user-MacBook-Pro.local"                                                                         
# machine        "x86_64"                                                                                         
# login          "user"                                                                                           
# user           "user"                                                                                           
# effective_user "user" 

Also see the difference between these two calls. First in R:

system("echo $PATH")
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

And now in terminal, where GDAL is explicitly mentioned:

user-Pro:~ user$ echo $PATH
/Library/Frameworks/GDAL.framework/Programs:/Library/Frameworks/GDAL.framework/Programs:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

enter image description here

Best Answer

It looks like there are some issues with the path variable, i.e. the shell opened by R doesn't know the path to the gdal binaries. There are two ways to fix this:

Specifying the full path

You can always use the whole path to gdalinfo in your system call to make it work:

path <- "/path/to/gdal/bin/gdalinfo" system2(path, "--version")

This may be the easiest to implement if you are running your script on different systems and OSs since you don't have to mess with environment variables. It's also easier to understand for other people that may be reading your code.

Note that with system2 each argument has to passed separately.

Setting the path variable

As pointed out by jbaums, you can modify the .Renviron file. From your terminal call

nano /path/to/R_HOME/.Renviron

If there's no reference to PATH in there (or if it's empty), add PATH=$PATH:/path/to/gdal/bin somewhere, then save and exit (CTRL+X). If there's already a reference to PATH, just append :/path/to/gdal/bin to it, save and exit.

Start R, call Sys.getenv("PATH") and see if your changes show up. Or just call `system("gdalinfo --version").

Note that this will work differently on windows.