Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Last active January 1, 2024 23:41
Show Gist options
  • Save obrl-soil/398e299559d5da845247c35a54f0eb1b to your computer and use it in GitHub Desktop.
Save obrl-soil/398e299559d5da845247c35a54f0eb1b to your computer and use it in GitHub Desktop.
# Source this file at the top of an R or Rmd file using `rgrass` together
# with GRASS 8 installed via OSGeo4W (https://trac.osgeo.org/osgeo4w/) on a
# Windows machine.
# e.g. source(file.path(getwd(), 'grass8-setup-win-osgeo4w.R'))
# You can alternatively add the contents to a project-specific .Rprofile.
# Its better to set these variables in an R session rather than in System,
# especially if you have other GIS software like PostGIS installed, which has
# its own GDAL_DATA path. This is also the only solution for Windows users on
# accounts lacking admin rights.
# Note that if you call `library(sf)` after setting these variables, GDAL_DATA
# will be overwritten!
# These paths are current to 2024-01-02 and may change in future. If you get
# stuck, open a question on gis.stackexchange or r-sig-geo. I'm not resourced
# to respond to one-on-one contacts on this topic.
# from /bin/o4w_env.bat:
Sys.setenv('OSGEO4W_ROOT' = file.path('C:', 'OSGeo4W'))
# NB Change the above if you installed OSGeo4W in a non-standard location.
# from %OSGEO4W_ROOT%/etc/ini/gdal.bat
Sys.setenv('GDAL_DATA' =
file.path(Sys.getenv('OSGEO4W_ROOT'),
'apps', 'gdal', 'share', 'gdal'))
Sys.setenv('GDAL_DRIVER_PATH' =
file.path(Sys.getenv('OSGEO4W_ROOT'),
'apps', 'gdal', 'lib', 'gdalplugins'))
# from %OSGEO4W_ROOT%/etc/ini/gs.bat (optional)
Sys.setenv('GS_LIB' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'gs', 'lib'))
# from %OSGEO4W_ROOT%/etc/ini/openssl.bat (optional)
Sys.setenv('OPENSSL_ENGINES' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'lib', 'engines-1_1'))
Sys.setenv('SSL_CERT_FILE' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'bin', 'curl-ca-bundle.crt'))
Sys.setenv('SSL_CERT_DIR' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'openssl', 'certs'))
# from %OSGEO4W_ROOT%/etc/ini/pdal.bat (optional)
Sys.setenv('PDAL_DRIVER_PATH' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'pdal', 'plugins'))
# from %OSGEO4W_ROOT%/etc/ini/proj-runtime-data.bat
Sys.setenv('PROJ_LIB' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'share', 'proj'))
# from %OSGEO4W_ROOT%/etc/ini/python3.bat
Sys.setenv('PYTHONHOME' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Python39'))
Sys.setenv('PYTHONUTF8' = 1)
# from %OSGEO4W_ROOT%/etc/ini/qt5.bat (optional?)
Sys.setenv('QT_PLUGIN_PATH' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'plugins'))
Sys.setenv('O4W_QT_PREFIX' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5'))
Sys.setenv('O4W_QT_BINARIES' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'bin'))
Sys.setenv('O4W_QT_PLUGINS' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'plugins'))
Sys.setenv('O4W_QT_LIBRARIES' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'lib'))
Sys.setenv('O4W_QT_TRANSLATIONS' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'translations'))
Sys.setenv('O4W_QT_HEADERS' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'include'))
Sys.setenv('O4W_QT_DOC' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'Qt5', 'doc'))
# from %OSGEO4W_ROOT%/apps/grass/grass83/etc/env.bat
Sys.setenv('GISBASE' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'grass', 'grass83'))
Sys.setenv('GRASS_PYTHON' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'bin', 'python3.exe'))
Sys.setenv('PYTHONPATH' =
paste(file.path(Sys.getenv('GISBASE'), 'etc', 'python'),
Sys.getenv('PYTHONPATH'),
sep = .Platform$path.sep))
Sys.setenv('GRASS_PROJSHARE' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'share', 'proj'))
Sys.setenv('FONTCONFIG_FILE'=
file.path(Sys.getenv("GISBASE"), 'etc', 'fonts.conf'))
# NB the appropriate path for the next two variables may vary depending on your
# Windows version and your account permissions. If in doubt, open GRASS-GUI,
# define a database, open a new location, exit, and then search your user
# directory for a folder called 'GRASS8'. Edit the paths below to match. This
# should only be necessary once after a clean installation of GRASS, or if you
# refresh your user profile.
# If GISRC is defined but no rc file exists at the specified location,
# `library('rgrass')` will fail with an error about XML. Either skip setting
# this variable, or manually initialise GRASS as described above before
# proceeding.
if(!file.exists(file.path(Sys.getenv('USERPROFILE'), 'AppData', 'Roaming',
'GRASS8', 'rc'))) {
message('Missing rc file in expected location.')
} else {
Sys.setenv('GISRC' =
file.path(Sys.getenv('USERPROFILE'), 'AppData', 'Roaming',
'GRASS8', 'rc'))
}
# Addons must not be installed system-wide for the next path to be valid. Admin
# rights may also be required.
Sys.setenv('GRASS_ADDON_BASE' =
file.path(Sys.getenv('USERPROFILE'), 'AppData', 'Roaming',
'GRASS8', 'addons'))
# lastly,
Sys.setenv('PATH' =
paste(file.path(Sys.getenv('GISBASE'), 'lib'),
file.path(Sys.getenv('GISBASE'), 'bin'),
Sys.getenv('GRASS_ADDON_BASE'),
Sys.getenv('O4W_QT_BINARIES'), # qt5.bat
file.path(Sys.getenv('PYTHONHOME'), 'Scripts'), # python3.bat
file.path(Sys.getenv('OSGEO4W_ROOT'), 'bin'), # o4w_env.bat
Sys.getenv("PATH"),
sep = .Platform$path.sep))
# Be aware that if you have other Python or GRASS installs on your PATH already,
# you might need to alter the relative position of `Sys.getenv("PATH")` above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment