Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Last active March 14, 2024 20:41
Show Gist options
  • Save obrl-soil/d74f58b137970364458ad41270a1c8f9 to your computer and use it in GitHub Desktop.
Save obrl-soil/d74f58b137970364458ad41270a1c8f9 to your computer and use it in GitHub Desktop.
rgrass7 and OsGeo4W on Windows - environment variables for R
# Source this file at the top of an R or Rmd file using rgrass7 together
# with GRASS installed via OSGeo4W (https://trac.osgeo.org/osgeo4w/) on a
# Windows machine.
# e.g. source(file.path(getwd(), 'rgrass7-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 2022-01-25 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'), 'share', 'gdal'))
Sys.setenv('GDAL_DRIVER_PATH' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'bin', 'gdalplugins'))
# from %OSGEO4W_ROOT%/etc/ini/libjpeg.bat (optional?)
Sys.setenv('JPEGMEM' = 1000000)
# from %OSGEO4W_ROOT%/etc/ini/openssl.bat (optional?)
Sys.setenv('OPENSSL_ENGINES' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'lib', 'engines-1_1'))
# from %OSGEO4W_ROOT%/etc/ini/proj.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/grass78/etc/env.bat
Sys.setenv('GISBASE' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'apps', 'grass', 'grass78'))
Sys.setenv('GRASS_PYTHON' =
file.path(Sys.getenv('OSGEO4W_ROOT'), 'bin', 'python3.exe'))
Sys.setenv('PYTHONPATH' =
file.path(Sys.getenv('GISBASE'), 'etc', 'python'))
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 'GRASS7'. 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('rgrass7')` 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',
'GRASS7', 'rc'))) {
message('Missing rc file in expected location.')
} else {
Sys.setenv('GISRC' =
file.path(Sys.getenv('USERPROFILE'), 'AppData', 'Roaming',
'GRASS7', 'rc'))
}
Sys.setenv('GRASS_ADDON_BASE' =
file.path(Sys.getenv('USERPROFILE'), 'AppData', 'Roaming',
'GRASS7', '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'),
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.
@obrl-soil
Copy link
Author

Ah interesting - I haven't had any problems myself lately (running a relative elevation model workflow, YMMV), but from the install path you posted above it looks like you're using a standalone installation of GRASS rather than the OSGeo4W version. GRASS 8.2 is available through the OSGeo4W package manager - use the advanced option in the installer to choose it.

@fgashakamba
Copy link

fgashakamba commented Jun 2, 2023

Yes @obrl-soil, it seems like the OSGeo4W version of GRASS is updated more constantly than the Windows standalone installer. Here are more details on that. I will try that one and see if it solves my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment