run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") run <- TRUE
oechoCmd <- get.echoCmdOption()
set.echoCmdOption(TRUE)
if (run) {
print(parseGRASS("r.slope.aspect"))
}
if (run) {
doGRASS("r.slope.aspect", flags=c("overwrite"),
elevation="elevation.dem", slope="slope", aspect="aspect")
}
if (run) {
pars <- list(elevation="elevation", slope="slope", aspect="aspect")
doGRASS("r.slope.aspect", flags=c("overwrite"), parameters=pars)
}
if (run) {
print(parseGRASS("r.buffer"))
}
if (run) {
doGRASS("r.buffer", flags=c("overwrite"), input="schools", output="bmap",
distances=seq(1000,15000,1000))
}
if (run) {
pars <- list(input="schools", output="bmap", distances=seq(1000,15000,1000))
doGRASS("r.buffer", flags=c("overwrite"), parameters=pars)
}
if (run) {
set.echoCmdOption(oechoCmd)
try(res <- execGRASS("r.stats", input = "fire_blocksgg", # no such file
flags = c("C", "n")), silent=FALSE)
}
if (run) {
res <- execGRASS("r.stats", input = "fire_blocksgg", flags = c("C", "n"),
legacyExec=TRUE)
print(res)
}
if (run) {
if (res != 0) {
resERR <- execGRASS("r.stats", input = "fire_blocksgg",
flags = c("C", "n"), redirect=TRUE, legacyExec=TRUE)
print(resERR)
}
}
if (run) {
res <- stringexecGRASS("r.stats -p -l input=geology", intern=TRUE)
print(res)
}
if (run) {
stringexecGRASS(paste("r.random.cells --overwrite --quiet output=samples",
"distance=1000 ncells=100 seed=1"))
}
if (run) {
execGRASS("r.random.cells", flags=c("overwrite", "quiet"), output="samples", distance=1000,
ncells=100L, seed=1L)
}
run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") run <- TRUE
run <- (run && !inherits(try(use_sp(), silent=TRUE), "try-error"))
if (run) {
G <- gmeta()
print(G)
}
if (run) {
cat(getLocationProj(), "\n")
}
if (run) {
cat(getLocationProj(g.proj_WKT=FALSE), "\n")
}
if (run) {
grd <- gmeta2grd()
print(grd)
}
if (run) {
ncells <- prod(slot(grd, "cells.dim"))
df <- data.frame(k=rep(1, ncells))
mask_SG <- sp::SpatialGridDataFrame(grd, data=df)
print(summary(mask_SG))
}
GRASS_INSTALLATION <- Sys.getenv("GRASS_INSTALLATION")
run <- FALSE
if (nzchar(GRASS_INSTALLATION)) run <- file.info(GRASS_INSTALLATION)$isdir
run <- run && require(terra, quietly=TRUE)
if (run) {
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
plot(r, col=grDevices::terrain.colors(50))
}
if (run) {
(loc <- initGRASS(GRASS_INSTALLATION, home=tempdir(), SG=r, override=TRUE))
}
if (run) {
write_RAST(r, "elev", flags="overwrite")
execGRASS("r.info", map="elev")
}
if (run) {
s <- rast(r)
values(s) <- values(r)
write_RAST(s, "elev1", flags="overwrite")
execGRASS("r.info", map="elev1")
}
if (run) {
execGRASS("r.slope.aspect", flags="overwrite", elevation="elev", slope="slope", aspect="aspect")
}
if (run) {
u1 <- read_RAST(c("elev", "slope", "aspect"), return_format="terra")
plot(u1[["elev"]], col=grDevices::terrain.colors(50))
}
run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") run <- TRUE
run <- (run && !inherits(try(use_sp(), silent=TRUE), "try-error"))
GV <- Sys.getenv("GRASS_VERBOSE")
Sys.setenv("GRASS_VERBOSE"=0)
# require(rgdal)
ois <- get.ignore.stderrOption()
set.ignore.stderrOption(TRUE)
get.useGDALOption()
if (run) {
nc_basic <- readRAST(c("geology", "elevation"), cat=c(TRUE, FALSE),
useGDAL=FALSE)
nc_basic <- readRAST(c("geology", "elevation"), cat=c(TRUE, FALSE),
useGDAL=TRUE)
print(table(nc_basic$geology))
}
if (run) {
execGRASS("r.stats", flags=c("c", "l", "quiet"), input="geology")
}
if (run) {
boxplot(nc_basic$elevation ~ nc_basic$geology)
}
if (run) {
nc_basic$sqdem <- sqrt(nc_basic$elevation)
}
if (run) {
if ("GRASS" %in% rgdal::gdalDrivers()$name) {
execGRASS("g.region", raster="elevation")
dem1 <- readRAST("elevation", plugin=TRUE, mapset="PERMANENT")
print(summary(dem1))
execGRASS("g.region", raster="elevation")
}
}
if (run) {
writeRAST(nc_basic, "sqdemSP", zcol="sqdem", flags=c("quiet", "overwrite"))
execGRASS("r.info", map="sqdemSP")
}
if (run) {
execGRASS("g.remove", flags="f", name="sqdemSP", type="raster")
}
if (run) {
writeRAST(nc_basic, "sqdemSP", zcol="sqdem", useGDAL=TRUE, flags=c("quiet", "overwrite"))
execGRASS("r.info", map="sqdemSP")
}
if (run) {
print(system.time(sqdemSP <- readRAST(c("sqdemSP", "elevation"),
useGDAL=TRUE, return_SGDF=FALSE)))
}
if (run) {
print(system.time(sqdemSP <- readRAST(c("sqdemSP", "elevation"),
useGDAL=TRUE, return_SGDF=TRUE)))
}
if (run) {
print(system.time(sqdemSP <- readRAST(c("sqdemSP", "elevation"),
useGDAL=FALSE, return_SGDF=TRUE)))
}
if (run) {
print(system.time(sqdemSP <- readRAST(c("sqdemSP", "elevation"),
useGDAL=FALSE, return_SGDF=FALSE)))
}
if (run) {
str(sqdemSP)
mat <- do.call("cbind", sqdemSP$dataList)
str(mat)
}
if (run) {
print(system.time(SGDF <- sp::SpatialGridDataFrame(grid=sqdemSP$grid,
proj4string=sqdemSP$proj4string, data=as.data.frame(sqdemSP$dataList))))
}
if (run) {
summary(SGDF)
}
if (run) {
execGRASS("g.remove", flags="f", name="sqdemSP", type="raster")
execGRASS("r.mapcalc", expression="basins0 = basins - 1")
execGRASS("r.stats", flags="c", input="basins0")
}
if (run) {
basins0 <- readRAST("basins0")
print(table(basins0$basins0))
}
if (run) {
basins0 <- readRAST("basins0", plugin=FALSE)
print(table(basins0$basins0))
}
if (run) {
execGRASS("g.remove", flags="f", name="basins0", type="raster")
}
run <- run && require("terra", quietly=TRUE)
if (run) {
v1 <- read_RAST("landuse", cat=TRUE, return_format="terra")
v1
inMemory(v1)
}
if (run) {
write_RAST(v1, "landuse1", flags=c("o", "overwrite"))
execGRASS("r.stats", flags="c", input="landuse1")
execGRASS("g.remove", flags="f", name="landuse1", type="raster")
}
Sys.setenv("GRASS_VERBOSE"=GV)
set.ignore.stderrOption(ois)
run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") run <- TRUE
run <- (run && !inherits(try(use_sp(), silent=TRUE), "try-error"))
GV <- Sys.getenv("GRASS_VERBOSE")
Sys.setenv("GRASS_VERBOSE"=0)
# require(rgdal)
ois <- get.ignore.stderrOption()
set.ignore.stderrOption(TRUE)
if (run) {
execGRASS("v.info", map="schools", layer="1")
}
if (run) {
print(vInfo("schools"))
schs <- readVECT("schools", plugin=NULL)
print(summary(schs))
}
if (run) {
schs1 <- readVECT("schools", plugin=FALSE)
print(summary(schs1))
}
if (run) {
writeVECT(schs, "newsch", v.in.ogr_flags=c("o", "overwrite"))
execGRASS("v.info", map="newsch", layer="1")
}
if (run) {
nschs <- readVECT("newsch")
print(summary(nschs))
}
if (run) {
print(all.equal(names(nschs), as.character(vColumns("newsch")[,2])))
}
if (run) {
names(nschs) <- paste("ABCDEFGHIJKLMNO", names(nschs), sep="")
writeVECT(nschs, "newsch1", v.in.ogr_flags=c("o", "overwrite"))
}
if (run) {
print(all.equal(names(nschs), as.character(vColumns("newsch1")[-1,2])))
}
if (run) {
nschs1 <- readVECT("newsch1")
print(all.equal(names(nschs), names(nschs1)[-1]))
}
if (run) {
print(summary(nschs1))
}
if (run) {
schs <- readVECT("schools", driver="ESRI Shapefile")
names(schs) <- paste("ABCDEFGHIJKLMNO", names(schs), sep="")
writeVECT(schs, "newsch", v.in.ogr_flags=c("o", "overwrite"),
driver="ESRI Shapefile")
print(all.equal(names(schs), as.character(vColumns("newsch")[-1,2])))
}
if (run) {
nschs <- readVECT("newsch", driver="ESRI Shapefile")
all.equal(names(schs), names(nschs)[-1])
}
if (run) {
print(vInfo("roadsmajor"))
}
if (run) {
roads <- readVECT("roadsmajor")
print(summary(roads))
}
if (run) {
cen_neig <- vect2neigh("census")
str(cen_neig)
}
if (run) {
execGRASS("g.remove", flags="f", name=c("newsch", "newsch1"), type="vector")
}
use_sf()
if (run) {
print(vInfo("schools"))
schs <- readVECT("schools", plugin=NULL)
print(summary(schs))
}
if (run) {
schs1 <- readVECT("schools", plugin=FALSE)
print(summary(schs1))
}
if (run) {
writeVECT(schs, "newsch", v.in.ogr_flags=c("o", "overwrite"))
execGRASS("v.info", map="newsch", layer="1")
}
if (run) {
nschs <- readVECT("newsch")
print(summary(nschs))
}
if (run) {
execGRASS("g.remove", flags="f", name="newsch", type="vector")
}
run <- run && require("terra", quietly=TRUE)
if (run) {
v1 <- read_VECT("census")
v1
}
if (run) {
write_VECT(v1, "census_sV")
execGRASS("v.info", map="census_sV")
}
if (run) {
execGRASS("g.remove", flags="f", name="census_sV", type="vector")
}
Sys.setenv("GRASS_VERBOSE"=GV)
set.ignore.stderrOption(ois)
if (run) use_sp()
Run the code above in your browser using DataLab