# NOT RUN {
# Initiate GRASS session
if(.Platform$OS.type == "windows"){
gisbase = "c:/Program Files/GRASS GIS 7.2.0"
} else {
gisbase = "/usr/lib/grass72/"
}
initGRASS(gisBase = gisbase,
home = tempdir(),
override = TRUE)
# Load files into GRASS
dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS")
sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS")
setup_grass_environment(dem = dem_path, sites = sites_path)
import_data(dem = dem_path, sites = sites_path)
gmeta()
# Derive streams from DEM
derive_streams(burn = 0, accum_threshold = 700, condition = TRUE, clean = TRUE)
# Prepare edges
calc_edges()
execGRASS("r.slope.aspect", flags = c("overwrite","quiet"),
parameters = list(
elevation = "dem",
slope = "slope"
))
calc_attributes_edges(input_raster = "slope", stat = "max", attr_name = "maxSlo")
# Prepare sites
calc_sites()
calc_attributes_sites_approx(input_attr_name = "maxSlo", stat = "max")
calc_attributes_sites_exact(input_raster = "slope", attr_name = "maxSloE", stat = "max")
# Plot data
dem <- readRAST('dem', ignore.stderr = TRUE)
edges <- readVECT('edges', ignore.stderr = TRUE)
sites <- readVECT('sites', ignore.stderr = TRUE)
plot(dem, col = terrain.colors(20))
mm <- range(c(edges$maxSlo_e, sites$maxSlo, sites$maxSloE))
b <- seq(from = mm[1], to = mm[2] + diff(mm) * 0.01, length.out = 10)
c_ramp <- colorRampPalette(c("blue", "red"))
cols <- c_ramp(length(b))[as.numeric(cut(edges$maxSlo_e, breaks = b, right = FALSE))]
lines(edges,col = cols, lwd = 2)
cols <- c_ramp(length(b))[as.numeric(cut(sites$maxSlo,breaks = b,right = FALSE))]
points(sites, pch = 19, col = cols)
cols <- c_ramp(length(b))[as.numeric(cut(sites$maxSloE,breaks = b,right = FALSE))]
points(sites, pch = 21, bg = cols, cex = 0.7)
# Some points in the centre of the map indicate a difference in max slope between
# approximate and exact calculation (different colors for inner and outer points)
# }
Run the code above in your browser using DataCamp Workspace