openSTARS icon indicating copy to clipboard operation
openSTARS copied to clipboard

Problems with openSTARS and GRASS on Windows

Open MiKatt opened this issue 1 year ago • 2 comments

Several users experience problems when trying to run GRASS and openSTARS on Windows systems. I would like to share what worked for a collaborator of mine (Erin Peterson, https://www.linkedin.com/in/erinepeterson/?originalSubdomain=au). I have not tried this myself and it comes without any warrenty. However, I would appreciate any feedback.

Here is, what she wrote:

Setting up GRASS/R/GDAL on a Hyper-V virtual machine running Windows 11 Home. I chose to set up GRASS, R, and GDAL on a virtual machine (VM) after having numerous issues related to Windows paths and environment variables, when QGIS and multiple versions of GDAL and Python were installed. There are no guarantees this will work for everyone, but I thought I’d share it in case it helps.

Hyper-V Microsoft Hyper-V allows you to set up a VM on Windows. It doesn’t come with Windows 11 Home, but this website provides some instructions about how to install it. https://beebom.com/how-enable-hyper-v-windows-11-home/ The following software packages were then installed on the VM.

Python It may not be necessary to install Python separately, but it was part of my legacy process and so I’ve included it here.

  1. Install Python 3 a. Customise installation to include the optional py.exe. Check that it has been installed properly by typing py --version at the command prompt. b. For me, it was installed here: c:\Python\Python310, but it may be different for each user.
  2. Install Python 2.7. a. Installed at C:/Python27
  3. Check that both versions have been installed properly a. Windows command > py -0p b. Should list both versions
  4. Calling python from Windows command a. py –[version number] or py-[part of version number]. Don’t use the [ ].

GRASS

  1. Download the OSGeo4W installer version 2 from the GRASS website https://grass.osgeo.org/download/windows/#OSGeo4W
  2. Run osgeo4w-setup.exe as administrator. Install in default location. Only install GRASS and GDAL.
  3. Check to make sure GRASS opens properly and you can install addons within the GUI. There may be a series of warnings and/or error messages in the console, but this doesn’t necessarily mean that the addon wasn’t installed. Check where the addons are being installed -they may be in one of these locations: a. %AppData%\roaming\GRASS7\addons b. C:\OSGeo4W\apps\grass\grass78\bin

R

  1. Follow the instructions on this website to create a setup script for your R session (e.g. rgrass7_setup.R). https://userwikis.fu-berlin.de/display/gis/2020/06/24/Grass%2C+R+and+rgrass7+and+its+problems Do not include the last 5 lines (#create a location so you can start with grass tasks). Note that paths may/will differ – check carefully and modify. See R script below
  2. Source the rgrass7_setup.R script: source(“rgrass7_setup.R”)
  3. In the R console, run the last 5 lines of code from the website to create a location using initGRASS(). If it works, then it’s installed and set up correctly. #create a location so you can start with grass tasks loc <- rgrass7::initGRASS(gisBase=grass.gis.base, home=tempdir(), mapset='PERMANENT', override=TRUE) This setup script will need to be run at the beginning of each R session where rgrass7 is used.
  4. Installing and accessing addon extensions in rgrass7. There are (at least) three ways to install addons for use in rgrass7 on Windows and my experience is that they are installed and accessed in different directories depending on whether you’re using the GRASS GUI or calling functions from R using rgrass7. a. Install manually through the GRASS GUI. https://www.youtube.com/watch?v=pFHHsAeLqyM&ab_channel=APear b. Install in R using g.extension. execGRASS(“g.extension”, flags = “s”, extension = “extension name in quotes”) You will see a few error messages in the R console, but it doesn’t necessarily mean the extension wasn’t installed correctly. c. Download the addon, unzip the file, and install it manually. For GRASS version 7.8.1, the addons can be found here: https://wingrass.fsv.cvut.cz/grass78/x86_64/addons/grass-7.8.1/

If you don’t know where to install the files, go back to step 4b and then search for the file you installed.

GDAL Command Line

  1. Follow the directions on this webpage and set three System Environment variables https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows a. Add gdal to the PATH. To find the correct path, search for gdal_translate.exe. b. Add a new variable called GDAL_DATA. “c:\OSGeo4W\share”. Not entirely sure this is correct, but it is where epsg_csv and proj folders reside. c. Add a new variable called GDAL_DRIVER_PATH. This is the directory where gdalplugins resides.
  2. Open the cmd prompt and type gdalinfo –version. If it finds it, it’s installed correctly.

rgrass7_setup.R

library(rgrass7)
use_sp()
osgeo4w.root<-"C:\\OSGeo4W"
Sys.setenv(OSGEO4W_ROOT=osgeo4w.root)
# define GISBASE
grass.gis.base<-paste0(osgeo4w.root,"\\apps\\grass\\grass78")
Sys.setenv(GISBASE=grass.gis.base)

Sys.setenv(GRASS_PYTHON=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\bin\\python.exe"))
Sys.setenv(PYTHONHOME=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\apps\\Python39"))
Sys.setenv(PYTHONPATH=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\apps\\grass\\grass78\\etc\\python"))
Sys.setenv(GRASS_PROJSHARE=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\proj"))
Sys.setenv(PROJ_LIB=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\proj"))
Sys.setenv(GDAL_DATA=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share"))
Sys.setenv(GEOTIFF_CSV=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\share\\epsg_csv"))
Sys.setenv(FONTCONFIG_FILE=paste0(Sys.getenv("OSGEO4W_ROOT"),"\\etc\\fonts.conf"))
## GRASS_ADD_ON_BASE - this does not work for me
##Sys.setenv(GRASS_ADDON_BASE=paste0(Sys.getenv("OSGEO4W_ROOT"), "\\apps\\grass\\grass78\\bin"))
##Sys.setenv(GRASS_ADDON_BASE=paste0(Sys.getenv("APPDATA"),"\\GRASS7\\addons"))

# call all OSGEO4W settings
system("C:/OSGeo4W64/bin/o-help.bat")

# create PATH variable - make sure you specify the correct Python version
Sys.setenv(PATH=paste0(grass.gis.base,";",
"C:\\OSGeo4w\\apps\\Python39\\lib\\site-packages\\numpy\\core",";",
"C:\\OSGeo4w\\apps\\grass\\grass78\\bin",";",
"C:\\OSGeo4w\\apps\\grass\\grass78\\lib",";",
"C:\\OSGeo4w\\apps\\grass\\grass78\\etc",";",
"C:\\OSGeo4w\\apps\\grass\\grass78\\etc\\python",";",
"C:\\OSGeo4w\\apps\\Python39\\Scripts",";",
"C:\\OSGeo4w\\bin",";",
"c:\\OSGeo4w\\apps",";",
"C:\\OSGeo4w\\apps\\saga",";",
paste0(Sys.getenv("WINDIR"),"/WBem"),";",
Sys.getenv("PATH")))

# initial again to be sure
use_sp()

#create a location so you can start with grass tasks
##loc <- rgrass7::initGRASS(gisBase=grass.gis.base,
##home=tempdir(),
##mapset='PERMANENT',
##override=TRUE)

MiKatt avatar Oct 15 '22 19:10 MiKatt