Software installation

Software (required):

Do not copy and paste the code from this page. R script used in this tutorial you can download from github. A gentle introduction to R programming languange and soil classes in R you can find via the ISRIC block-course pages. Some more example of SAGA GIS + R usage you can find here. To visualize spatial predictions in a web-browser or Google Earth you could also consider following this tutorial. If you are completely novel to R, you should consider following some crash course first and/or obtain the R reference card.

RStudio is, in principle, the main R scripting environment and can be used to control all other software used in the course. A more detailed RStudio tutorial is available at: RStudio — Online Learning. Consider also following some spatial data tutorials by James Cheshire. Below is an example of RStudio session with R editor on right and R console on left.

RStudio is a commonly used R editor written in C++.

To install all required R packages at once, you can use:

> list.of.packages <- c("rgdal", "raster" "GSIF", "plotKML", "nnet", "plyr", "ROCR", "randomForest", "psych", "mda", "h2o", "dismo", "grDevices", "snowfall", "hexbin", "lattice", "ranger", "xgboost", "parallel", "doParallel", "caret")
> new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
> if(length(new.packages)) install.packages(new.packages)

You could put this line at top of each R script that you share so that anybody using that script will automatically get all missing packages.

Many examples in the GSIF course rely on the top 5 most commonly used packages for spatial data: (1) sp and rgdal, (2) raster, (3) plotKML and (4) GSIF. To install most up-to-date version of plotKML/GSIF, you can also use the R-Forge versions of the package:

> install.packages("GSIF", repos=c("http://R-Forge.R-project.org"), type = "source", dependencies = TRUE)
trying URL 'http://R-Forge.R-project.org/src/contrib/GSIF_0.5-1.tar.gz'
Content type 'application/x-gzip' length 1783069 bytes (1.7 MB)
downloaded 1.7 MB
 
* installing *source* package 'GSIF' ...
** R
** data
** demo
** inst
** preparing package for lazy loading
Creating a generic function for 'subset' from package 'base' in package 'GSIF'
Creating a generic function for 'as.data.frame' from package 'base' in package 'GSIF'
Creating a generic function for 'predict' from package 'stats' in package 'GSIF'
Creating a generic function from function 'sample.grid' in package 'GSIF'
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (GSIF)

A copy of the most-up-to-date stable versions of plotKML and GSIF is also available on github. To run only some specific function from GSIF package you could do for example:

> source_https <- function(url, ...) {
+   # load package
+   require(RCurl)
+   # download:
+   cat(getURL(url, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")), file = basename(url))
+   source(basename(url))
+ }
> source_https("https://raw.githubusercontent.com/cran/GSIF/master/R/OCSKGM.R")

To test if these packages work properly create soil maps and visualize them in Google Earth by running the following lines of code (see also function: fit.gstatModel):

> library(GSIF)
GSIF version 0.5-1 (2016-05-02)
URL: http://gsif.r-forge.r-project.org/
> library(sp)
> library(boot)
> library(aqp)
This is aqp 1.9.3
> library(plyr)
> library(rpart)
> library(splines)
> library(gstat)
> library(quantregForest)
Loading required package: randomForest
randomForest 4.6-12
Type rfNews() to see new features/changes/bug fixes.
> library(plotKML)
plotKML version 0.5-6 (2016-05-02)
URL: http://plotkml.r-forge.r-project.org/
> demo(meuse, echo=FALSE)
> omm <- fit.gstatModel(meuse, om~dist+ffreq, meuse.grid, method="quantregForest")
Warning: Shapiro-Wilk normality test and Anderson-Darling normality test report probability of < .05 indicating lack of normal distribution for residuals
Fitting a 2D variogram...
Saving an object of class 'gstatModel'...
> om.rk <- predict(omm, meuse.grid)
Subsetting observations to fit the prediction domain in 2D...
Prediction error for 'randomForest' model estimated using the 'quantreg' package.
Generating predictions using the trend model (RK method)...
[using ordinary kriging]
100% done
Running 5-fold cross validation using 'krige.cv'...
Creating an object of class "SpatialPredictions"
> plotKML(om.rk)
KML file opened for writing...
Reprojecting to +proj=longlat +datum=WGS84 ...

Example of plotKML output.

SAGA GIS is an extensive GIS geoprocessor software with over 600 functions. SAGA GIS can not be installed from RStudio (it is not a package for R). Instead, you need to install SAGA GIS using the installation instructions from the software homepage. After you have installed SAGA GIS, you can send processes from R to SAGA GIS by using the saga_cmd command line interface:

> saga_cmd = "C:/Progra~1/SAGA-GIS/saga_cmd.exe"
> system(paste(saga_cmd))
____________________________
 
   #####   ##   #####    ##
  ###     ###  ##       ###
   ###   # ## ##  #### # ##
    ### ##### ##    # #####
 ##### #   ##  ##### #   ##
____________________________
 
SAGA Version: 2.2.7 (64 bit)
 
78 loaded tool libraries (669 tools):
...

To use some SAGA GIS function you need to carefully follow the SAGA GIS command line arguments. For example,

> library(rgdal)
> library(raster)
> data("eberg_grid")
> gridded(eberg_grid) <- ~x+y
> proj4string(eberg_grid) <- CRS("+init=epsg:31467")
> writeGDAL(eberg_grid["DEMSRT6"], "DEMSRT6.sdat", "SAGA")
> system(paste(saga_cmd, 'ta_lighting 0 -ELEVATION "DEMSRT6.sgrd" -SHADE "hillshade.sgrd" -EXAGGERATION 2'))
____________________________
 
   #####   ##   #####    ##
  ###     ###  ##       ###
   ###   # ## ##  #### # ##
    ### ##### ##    # #####
 ##### #   ##  ##### #   ##
____________________________
 
SAGA Version: 2.2.7 (64 bit)
 
____________________________
library path: C:\Progra~1\SAGA-GIS\modules\
library name: ta_lighting
library     : Lighting, Visibility
tool        : Analytical Hillshading
author      : O.Conrad, V.Wichmann (c) 2003-2013
processors  : 12 [12]
____________________________
 
Load grid: dist.sgrd...
 
100%okay
 
Parameters
 
Grid system: 40; 78x 104y; 178460x 329620y
Elevation: dist
Analytical Hillshading: Analytical Hillshading
Shading Method: Standard
Azimuth [Degree]: 315.000000
Height [Degree]: 45.000000
Exaggeration: 2.000000
 
101%Save grid: hillshade...
 
100%okay

Deriving hillshading using SAGA GIS and then visualizing the result in R.

Another very important software for handling spatial data (and especially for exchanging / converting spatial data) is GDAL. GDAL also needs to be installed separately (for Windows machines use e.g. "gdal-201-1800-x64-core.msi") and then can be called from command line:

if(.Platform$OS.type == "windows"){
  gdal.dir <- shortPathName("C:/Program files/GDAL")
  gdal_translate <- paste0(gdal.dir, "/gdal_translate.exe")
  gdalwarp <- paste0(gdal.dir, "/gdalwarp.exe")
} else {
  gdal_translate = "gdal_translate"
  gdalwarp = "gdalwarp"
}
> system(paste(gdalwarp))
Usage: gdalwarp [--help-general] [--formats]
    [-s_srs srs_def] [-t_srs srs_def] [-to "NAME=VALUE"]
    [-order n | -tps | -rpc | -geoloc] [-et err_threshold]
    [-refine_gcps tolerance [minimum_gcps]]
    [-te xmin ymin xmax ymax] [-tr xres yres] [-tap] [-ts width height]
    [-ovr level|AUTO|AUTO-n|NONE] [-wo "NAME=VALUE"] [-ot Byte/Int16/...] [-wt Byte/Int16]
    [-srcnodata "value [value...]"] [-dstnodata "value [value...]"] -dstalpha
    [-r resampling_method] [-wm memory_in_mb] [-multi] [-q]
    [-cutline datasource] [-cl layer] [-cwhere expression]
    [-csql statement] [-cblend dist_in_pixels] [-crop_to_cutline]
    [-of format] [-co "NAME=VALUE"]* [-overwrite]
    [-nomd] [-cvmd meta_conflict_value] [-setci] [-oo NA\ME=VALUE]*
    [-doo NA\ME=VALUE]*
    srcfile* dstfile
 
Available resampling methods:
    near (default), bilinear, cubic, cubicspline, lanczos, average, mode,  max, min, med, Q1, Q3.
 
FAILURE: No target filename specified.

We can use GDAL to reproject grid from the previous example:

> system(paste(gdalwarp, ' DEMSRT6.sdat DEMSRT6_ll.tif -t_srs \"+proj=longlat +datum=WGS84\"'))
0...10...20...30...40...50...60...70...80...90...100 - done.
> plot(raster("DEMSRT6_ll.tif"))

wiki software
 stars  from 1 votes (Details)