# Run example if in active GRASS nc_basic_spm_grass7 location
run <- FALSE
if (nchar(Sys.getenv("GISRC")) > 0 &&
read.dcf(Sys.getenv("GISRC"))[1, "LOCATION_NAME"] == "nc_basic_spm_grass7") {
run <- require(terra, quietly = TRUE)
}
# Store original environment variable settings
GV <- Sys.getenv("GRASS_VERBOSE")
Sys.setenv("GRASS_VERBOSE" = 0)
ois <- get.ignore.stderrOption()
set.ignore.stderrOption(TRUE)
if (run) {
# Create a new mapset
meta <- gmeta()
location_path <- file.path(meta$GISDBASE, meta$LOCATION_NAME)
previous_mapset <- meta$MAPSET
example_mapset <- "RGRASS_EXAMPLES"
execGRASS("g.mapset", "c", mapset = example_mapset)
}
if (run) {
# Report basic metadata about the schools dataset
execGRASS("v.info", map = "schools", layer = "1")
print(vInfo("schools"))
}
if (run) {
# Read/write as a SpatVector
schs <- read_VECT("schools")
print(summary(schs))
}
if (run) {
try({
write_VECT(schs, "newsch", flags = c("o", "overwrite"))
})
schs <- read_VECT("schools", use_gdal_grass_driver = FALSE)
}
if (run) {
write_VECT(schs, "newsch", flags = c("o", "overwrite"))
execGRASS("v.info", map = "newsch", layer = "1")
}
if (run) {
nschs <- read_VECT("newsch")
print(summary(nschs))
}
if (run) {
print(all.equal(names(nschs), as.character(vColumns("newsch")[, 2])))
}
if (run) {
# Show metadata for the roadsmajor dataset and read as spatVector
print(vInfo("roadsmajor"))
}
if (run) {
roads <- read_VECT("roadsmajor")
print(summary(roads))
}
# not run: vect2neigh() currently writes 3 new data sources in the PERMANENT
# mapset, despite this mapset not being the active one.
if (FALSE) {
cen_neig <- vect2neigh("census")
str(cen_neig)
}
# Cleanup the previously created datasets
if (run) {
execGRASS(
"g.remove",
flags = "f",
name = c("newsch", "newsch1"),
type = "vector"
)
execGRASS("g.mapset", mapset = previous_mapset)
if (example_mapset != previous_mapset) {
unlink(file.path(location_path, example_mapset), recursive = TRUE)
}
}
# Restore environment variable settings
Sys.setenv("GRASS_VERBOSE" = GV)
set.ignore.stderrOption(ois)
Run the code above in your browser using DataLab