Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break up run-conditioned example chunks to place output after each co…
Browse files Browse the repository at this point in the history
…de section
rsbivand committed Jan 3, 2025

Verified

This commit was signed with the committer’s verified signature.
nickzelei Nick Zelei
1 parent 75a2ebb commit 46b5560
Showing 18 changed files with 564 additions and 321 deletions.
6 changes: 6 additions & 0 deletions R/gmeta.R
Original file line number Diff line number Diff line change
@@ -33,13 +33,19 @@
#' if (run) {
#' G <- gmeta()
#' print(G)
#' }
#'
#' if (run) {
#' cat(getLocationProj(), "\n")
#' 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)
8 changes: 8 additions & 0 deletions R/initGRASS.R
Original file line number Diff line number Diff line change
@@ -108,19 +108,25 @@
#' f <- system.file("ex/elev.tif", package="terra")
#' r <- rast(f)
#' plot(r, col = terrain.colors(50))
#' }
#'
#' if (run) {
#' # Initialize a temporary GRASS project using the example data
#' loc <- initGRASS(
#' GRASS_INSTALLATION,
#' home = tempdir(),
#' SG = r,
#' override = TRUE
#' )
#' }
#'
#' if (run) {
#' # Write the example data to the GRASS database
#' write_RAST(r, "elev", flags = "overwrite")
#' execGRASS("r.info", map = "elev")
#' }
#'
#' if (run) {
#' # Calculate slope and aspect raster
#' execGRASS(
#' "r.slope.aspect",
@@ -129,7 +135,9 @@
#' slope = "slope",
#' aspect = "aspect"
#' )
#' }
#'
#' if (run) {
#' # Read the results back into R and plot
#' u1 <- read_RAST(c("elev", "slope", "aspect"), return_format = "terra")
#' plot(u1[["elev"]], col = terrain.colors(50))
130 changes: 82 additions & 48 deletions R/rast_link.R
Original file line number Diff line number Diff line change
@@ -77,79 +77,113 @@
#' previous_mapset <- meta$MAPSET
#' example_mapset <- "RGRASS_EXAMPLES"
#' execGRASS("g.mapset", flags = "c", mapset = example_mapset)
#' }
#'
#' if (run) {
#' # List available mapsets and raster maps
#' mapsets <- unlist(
#' strsplit(execGRASS("g.mapsets", flags = "p", intern = TRUE), " ")
#' )
#' print(mapsets)
#' }
#'
#' if (run) {
#' execGRASS("g.list", type = "raster", pattern = "soils", flags = "m",
#' intern = TRUE)
#' }
#'
#' if (run) {
#' execGRASS("g.list", type = "raster", pattern = "soils@PERMANENT",
#' mapset = ".", flags = "m", intern = TRUE)
#' }
#'
#' if (run) {
#' execGRASS("g.list", type = "raster", pattern = "soils",
#' mapset = "PERMANENT", flags = "m", intern = TRUE)
#' }
#' # Read/write the GRASS "landuse" dataset as a SpatRaster
#' if (require("terra", quietly = TRUE)) {
#' if (run) {
#' v1 <- read_RAST("landuse", cat = TRUE, return_format = "terra")
#' print(v1)
#' print(inMemory(v1))
#' }
#'
#' # Read/write the GRASS "landuse" dataset as a SpatRaster
#' if (require("terra", quietly = TRUE)) {
#' v1 <- read_RAST("landuse", cat = TRUE, return_format = "terra")
#' print(v1)
#' print(inMemory(v1))
#'
#' write_RAST(v1, "landuse1", flags = c("o", "overwrite"))
#' execGRASS("r.stats", flags = "c", input = "landuse1")
#' execGRASS("g.remove", flags = "f", name = "landuse1", type = "raster")
#' }
#' 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("_SP_EVOLUTION_STATUS_" = "2")
#' # read the geology and elevation GRASS datasets as SpatialGridDataFrames
#' if (require("sp", quietly = TRUE)) {
#' if (run) {
#' nc_basic <- read_RAST(c("geology", "elevation"), cat = c(TRUE, FALSE),
#' return_format = "SGDF")
#' print(table(nc_basic$geology))
#' }
#'
#' # read the geology and elevation GRASS datasets as SpatialGridDataFrames
#' if (require("sp", quietly = TRUE)) {
#' nc_basic <- read_RAST(c("geology", "elevation"), cat = c(TRUE, FALSE),
#' return_format = "SGDF")
#' print(table(nc_basic$geology))
#' if (run) {
#' execGRASS("r.stats", flags = c("c", "l", "quiet"), input = "geology")
#' boxplot(nc_basic$elevation ~ nc_basic$geology)
#' }
#' if (run) {
#' # Compute square root of elevation and write back to GRASS
#' nc_basic$sqdem <- sqrt(nc_basic$elevation)
#' write_RAST(nc_basic, "sqdemSP", zcol = "sqdem",
#' flags = c("quiet", "overwrite"))
#' execGRASS("r.info", map = "sqdemSP")
#' }
#'
#' execGRASS("r.stats", flags = c("c", "l", "quiet"), input = "geology")
#' boxplot(nc_basic$elevation ~ nc_basic$geology)
#' if (run) {
#' # Read the new raster data and measure read times
#' print(system.time(
#' sqdemSP <- read_RAST(c("sqdemSP", "elevation"), return_format = "SGDF")
#' ))
#' }
#'
#' # Compute square root of elevation and write back to GRASS
#' nc_basic$sqdem <- sqrt(nc_basic$elevation)
#' write_RAST(nc_basic, "sqdemSP", zcol = "sqdem",
#' flags = c("quiet", "overwrite"))
#' execGRASS("r.info", map = "sqdemSP")
#' if (run) {
#' print(system.time(
#' sqdem <- read_RAST(c("sqdemSP", "elevation"), return_format = "terra"))
#' )
#' }
#'
#' # Read the new raster data and measure read times
#' print(system.time(
#' sqdemSP <- read_RAST(c("sqdemSP", "elevation"), return_format = "SGDF")
#' ))
#' print(system.time(
#' sqdem <- read_RAST(c("sqdemSP", "elevation"), return_format = "terra"))
#' )
#' print(names(sqdem))
#' if (run) {
#' print(names(sqdem))
#' }
#'
#' sqdem1 <- read_RAST(c("sqdemSP@RGRASS_EXAMPLES", "elevation@PERMANENT"))
#' print(names(sqdem1))
#' if (run) {
#' sqdem1 <- read_RAST(c("sqdemSP@RGRASS_EXAMPLES", "elevation@PERMANENT"))
#' print(names(sqdem1))
#' }
#'
#' execGRASS("g.remove", flags = "f", name = "sqdemSP", type = "raster")
#' if (run) {
#' execGRASS("g.remove", flags = "f", name = "sqdemSP", type = "raster")
#'
#' # GRASS r.mapcalc example
#' execGRASS("r.mapcalc", expression = "basins0 = basins - 1",
#' flags = "overwrite")
#' execGRASS("r.stats", flags = "c", input = "basins0")
#' # GRASS r.mapcalc example
#' execGRASS("r.mapcalc", expression = "basins0 = basins - 1",
#' flags = "overwrite")
#' execGRASS("r.stats", flags = "c", input = "basins0")
#' }
#'
#' basins0 <- read_RAST("basins0", return_format = "SGDF")
#' print(table(basins0$basins0))
#' execGRASS("g.remove", flags = "f", name = "basins0", type = "raster")
#' if (run) {
#' basins0 <- read_RAST("basins0", return_format = "SGDF")
#' print(table(basins0$basins0))
#' execGRASS("g.remove", flags = "f", name = "basins0", type = "raster")
#' }
#'
#' # Create and read a test raster
#' execGRASS("r.mapcalc", expression = "test_t = 66000",
#' flags = "overwrite")
#' execGRASS("r.info", flags = "r", map = "test_t", intern = TRUE)
#' tt <- read_RAST("test_t")
#' execGRASS("g.remove", flags = "f", name = "test_t", type = "raster")
#' }
#' if (run) {
#' # Create and read a test raster
#' execGRASS("r.mapcalc", expression = "test_t = 66000",
#' flags = "overwrite")
#' execGRASS("r.info", flags = "r", map = "test_t", intern = TRUE)
#' tt <- read_RAST("test_t")
#' execGRASS("g.remove", flags = "f", name = "test_t", type = "raster")
#' }
#' }
#'
#' if (run) {
#' # Restore the previous mapset and clean up
#' execGRASS("g.mapset", mapset = previous_mapset)
#' if (example_mapset != previous_mapset) {
19 changes: 18 additions & 1 deletion R/vect_link_ng.R
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@
#' run <- FALSE
#' if (nchar(Sys.getenv("GISRC")) > 0 &&
#' read.dcf(Sys.getenv("GISRC"))[1, "LOCATION_NAME"] == "nc_basic_spm_grass7") {
#' run <- TRUE
#' run <- require(terra, quietly = TRUE)
#' }
#'
#' # Store original environment variable settings
@@ -87,30 +87,47 @@
#' 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))
#' }
29 changes: 20 additions & 9 deletions R/xml1.R
Original file line number Diff line number Diff line change
@@ -569,7 +569,8 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' if (run) {
#' # Read and print GRASS interface description for 'r.slope.aspect'
#' print(parseGRASS("r.slope.aspect"))
#'
#' }
#' if (run) {
#' # Assemble the 'r.slope.aspect' command with specified parameters as a string
#' doGRASS(
#' "r.slope.aspect",
@@ -578,18 +579,21 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' slope = "slope",
#' aspect = "aspect"
#' )
#'
#' }
#' if (run) {
#' # Alternatively, specify parameters as a list
#' params <- list(elevation = "elevation",
#' slope = "slope",
#' aspect = "aspect")
#' doGRASS("r.slope.aspect",
#' flags = c("overwrite"),
#' parameters = params)
#'
#' }
#' if (run) {
#' # Read and print GRASS interface description for 'r.buffer'
#' print(parseGRASS("r.buffer"))
#'
#' }
#' if (run) {
#' # Assemble the 'r.buffer' with specified parameters as as string
#' doGRASS(
#' "r.buffer",
@@ -598,23 +602,26 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' output = "bmap",
#' distances = seq(1000, 15000, 1000)
#' )
#'
#' }
#' if (run) {
#' # Alternatively, specify parameters as a list
#' params <- list(
#' input = "schools",
#' output = "bmap",
#' distances = seq(1000, 15000, 1000)
#' )
#' doGRASS("r.buffer", flags = c("overwrite"), parameters = params)
#'
#' }
#' if (run) {
#' # Restore original echo command option
#' set.echoCmdOption(echoCmdOption)
#'
#' # Try executing 'r.stats' command which will fail because "fire_blocksgg"
#' # does not exist in the mapset
#' try(res <- execGRASS("r.stats", input = "fire_blocksgg", flags = c("C", "n")),
#' silent = FALSE)
#'
#' }
#' if (run) {
#' # Execute 'r.stats' with legacyExec and print the result
#' res <- execGRASS(
#' "r.stats",
@@ -623,7 +630,8 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' legacyExec = TRUE
#' )
#' print(res)
#'
#' }
#' if (run) {
#' # If the command failed, retrieve error message
#' if (res != 0) {
#' resERR <- execGRASS(
@@ -635,6 +643,8 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' )
#' print(resERR)
#' }
#' }
#' if (run) {
#'
#' # Use 'stringexecGRASS' to run a command and print the result
#' res <- stringexecGRASS("r.stats -p -l input=geology", intern = TRUE)
@@ -643,7 +653,8 @@ insert_required <- function(pcmd, parameters, pt, req, suppress_required) {
#' stringexecGRASS(
#' "r.random.cells --overwrite --quiet output=samples distance=1000 ncells=100 seed=1"
#' )
#'
#' }
#' if (run) {
#' # Alternatively, run the same command using 'execGRASS'
#' execGRASS(
#' "r.random.cells",
2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ pkgdown_sha: ~
articles:
coerce: coerce.html
use: use.html
last_built: 2025-01-02T10:52Z
last_built: 2025-01-03T14:04Z
urls:
reference: https://osgeo.github.io/rgrass/reference
article: https://osgeo.github.io/rgrass/articles
191 changes: 107 additions & 84 deletions docs/reference/execGRASS.html

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions docs/reference/gmeta.html
8 changes: 8 additions & 0 deletions docs/reference/initGRASS.html
181 changes: 109 additions & 72 deletions docs/reference/read_RAST.html

Large diffs are not rendered by default.

67 changes: 42 additions & 25 deletions docs/reference/read_VECT.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions man/execGRASS.Rd
6 changes: 6 additions & 0 deletions man/gmeta.Rd
8 changes: 8 additions & 0 deletions man/initGRASS.Rd
150 changes: 92 additions & 58 deletions man/read_RAST.Rd
19 changes: 18 additions & 1 deletion man/read_VECT.Rd

0 comments on commit 46b5560

Please sign in to comment.