Title: | An Implementation of IDW-PLUS |
---|---|
Description: | Compute spatially explicit land-use metrics for stream survey sites in GRASS GIS and R as an open-source implementation of IDW-PLUS (Inverse Distance Weighted Percent Land Use for Streams). The package includes functions for preprocessing digital elevation and streams data, and one function to compute all the spatially explicit land use metrics described in Peterson et al. (2011) <doi:10.1111/j.1365-2427.2010.02507.x> and previously implemented by Peterson and Pearse (2017) <doi:10.1111/1752-1688.12558> in ArcGIS-Python as IDW-PLUS. |
Authors: | Alan Pearse [aut, cre], Grace Heron [aut], Erin Peterson [aut] |
Maintainer: | Alan Pearse <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-11-23 03:31:45 UTC |
Source: | https://github.com/apear9/rdwplus |
Burning-in streams (also called 'drainage reinforcement') ensures flow direction and accumulation grids based on the digital elevation model will correctly identify the stream path.
burn_in(dem, stream, out, burn = 10, overwrite = FALSE)
burn_in(dem, stream, out, burn = 10, overwrite = FALSE)
dem |
Digital elevation model raster in the GRASS mapset. |
stream |
Binary stream raster in the GRASS mapset. |
out |
Name of output to be created in the GRASS mapset. |
burn |
The magnitude of the drainage reinforcement in elevation units. Defaults to |
overwrite |
A logical indicating whether the file |
Nothing. A raster with the name out
will be written to the current GRASS mapset.
# Will only run if a GRASS session is initialised if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") set_envir(dem) raster_to_mapset(dem) vector_to_mapset(stream_shp) # Create binary stream rasterise_stream("streams", "streams_rast") reclassify_streams("streams_rast", "streams_binary", out_type = "binary") # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary", out = "dem_burn", burn = 10, overwrite = FALSE) # Plot plot_GRASS("dem_burn", col = topo.colors(10)) }
# Will only run if a GRASS session is initialised if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") set_envir(dem) raster_to_mapset(dem) vector_to_mapset(stream_shp) # Create binary stream rasterise_stream("streams", "streams_rast") reclassify_streams("streams_rast", "streams_binary", out_type = "binary") # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary", out = "dem_burn", burn = 10, overwrite = FALSE) # Plot plot_GRASS("dem_burn", col = topo.colors(10)) }
This function is mostly used internally by other functions in the package. However, users may call this function to check whether they have correctly established a GRASS session prior to using the other functions in the package.
check_running()
check_running()
A logical. The logical indicates whether a valid GRASS session is currently running.
check_running()
check_running()
This function has no parameters. It can be used to clear an existing raster mask.
clear_mask()
clear_mask()
if(check_running()) clear_mask()
if(check_running()) clear_mask()
Compute an iFLO weight raster outside of the compute_metrics()
function.
compute_iFLO_weights( pour_point, watershed, null_streams, flow_dir, out_flow_length, out_iFLO, out_iFLO_no_stream, idwp = -1, remove_streams = FALSE, ... )
compute_iFLO_weights( pour_point, watershed, null_streams, flow_dir, out_flow_length, out_iFLO, out_iFLO_no_stream, idwp = -1, remove_streams = FALSE, ... )
pour_point |
Pour point raster containing a single pour point (i.e., the outlet). |
watershed |
Watershed raster to use as a mask for the flow-path calculations. |
null_streams |
A streams raster with NoData for the stream cells and 1s everywhere else |
flow_dir |
A flow direction raster. |
out_flow_length |
Name of the output flow length raster. |
out_iFLO |
Name of the output weights raster. |
out_iFLO_no_stream |
Name of the output weights raster excluding cells on the stream line (ignored inf |
idwp |
An inverse distance weighting power. This should be negative. The value |
remove_streams |
A logical indicating whether cells corresponding to the stream line should be removed from the weights raster. Defaults to |
... |
Optional extra arguments to |
Nothing.
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get pour points coord_to_raster("snapsite", which = 1, out = "pour_point") # Get iFLO weights compute_iFLO_weights( "pour_point", "wshed.tif", "null_stm.tif", "fd.tif", "fl_outlet.tif", "iFLO_weights.tif", idwp = -1, remove_streams = FALSE ) plot_GRASS("iFLO_weights.tif", col = topo.colors(12)) }
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get pour points coord_to_raster("snapsite", which = 1, out = "pour_point") # Get iFLO weights compute_iFLO_weights( "pour_point", "wshed.tif", "null_stm.tif", "fd.tif", "fl_outlet.tif", "iFLO_weights.tif", idwp = -1, remove_streams = FALSE ) plot_GRASS("iFLO_weights.tif", col = topo.colors(12)) }
Compute an iFLO weight raster outside of the compute_metrics()
function.
compute_iFLS_weights( streams, null_streams, flow_dir, out_flow_length, out_iFLS, out_iFLS_no_stream, watershed, idwp, remove_streams, ... )
compute_iFLS_weights( streams, null_streams, flow_dir, out_flow_length, out_iFLS, out_iFLS_no_stream, watershed, idwp, remove_streams, ... )
streams |
Pour point raster containing a single pour point (i.e., the outlet). |
null_streams |
A streams raster with NoData for the stream cells and 1s everywhere else |
flow_dir |
A flow direction raster. |
out_flow_length |
Name of the output flow length raster. |
out_iFLS |
Name of the output weights raster. |
out_iFLS_no_stream |
Name of the output weights raster excluding cells on the stream line (ignored inf |
watershed |
Watershed raster to use as a mask for the flow-path calculations. This is optional for the iFLS weight calculations. |
idwp |
An inverse distance weighting power. This should be negative. The value |
remove_streams |
A logical indicating whether cells corresponding to the stream line should be removed from the weights raster. Defaults to |
... |
Optional extra arguments to |
Nothing.
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get iFLS weights compute_iFLS_weights( "new_stm.tif", "null_stm.tif", "fd.tif", "fl_streams.tif", "iFLS_weights.tif", idwp = -1, watershed = "wshed.tif", remove_streams = FALSE, overwrite = T ) plot_GRASS("iFLS_weights.tif", col = topo.colors(12)) }
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get iFLS weights compute_iFLS_weights( "new_stm.tif", "null_stm.tif", "fd.tif", "fl_streams.tif", "iFLS_weights.tif", idwp = -1, watershed = "wshed.tif", remove_streams = FALSE, overwrite = T ) plot_GRASS("iFLS_weights.tif", col = topo.colors(12)) }
Workhorse function for rdwplus
. This function computes the spatially explicit landuse metrics in IDW-Plus (Peterson and Pearse, 2017).
compute_metrics( metrics = c("lumped", "iFLO", "iFLS", "HAiFLO", "HAiFLS"), landuse, sites, out_fields, watersheds, flow_dir, flow_acc, streams, idwp = -1, percentage = TRUE, remove_streams = TRUE, max_memory = 300 )
compute_metrics( metrics = c("lumped", "iFLO", "iFLS", "HAiFLO", "HAiFLS"), landuse, sites, out_fields, watersheds, flow_dir, flow_acc, streams, idwp = -1, percentage = TRUE, remove_streams = TRUE, max_memory = 300 )
metrics |
A character vector. This vector specifies which metric(s) should be calculated. Your options are lumped, iFLO, iFLS, iEDO, iEDS, HAiFLO and HAiFLS. The default is to calculate the lumped, iFLO, iFLS, HAiFLO, and HAiFLS metrics. |
landuse |
Names of landuse or landcover rasters in the current GRASS mapset. These can be continuous (e.g., percentage cover or NDVI) or binary, with a value of 1 for cells with a particular land use category and a value of 0 otherwise. |
sites |
A set of survey sites in the current GRASS mapset. |
out_fields |
A character vector of output field names to store the metrics. Note that |
watersheds |
A vector of watershed raster names in the current GRASS mapset. |
flow_dir |
Name of a flow direction raster produced by |
flow_acc |
Name of a flow accumulation raster produced by |
streams |
Name of a streams raster in the current GRASS mapset. The stream raster must have NoData values in cells that do not fall along the stream line. Optional if you are not asking for the iFLS, iEDS, and/or HAiFLS metrics. |
idwp |
The inverse distance weighting parameter. Default is |
percentage |
A logical indicating whether the result should be expressed as a percentage. Defaults to |
remove_streams |
A logical indicating whether cells falling on the stream line should be removed from iEDS, iFLS, and HAiFLS metrics. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
A sf
object of the snapped survey sites that also contains the computed landscape metrics.
Peterson, E.E. & Pearse, A.R. (2017). IDW-Plus: An ArcGIS toolset for calculating spatially explicit watershed attributes for survey sites. JAWRA, 53(5), 1241-1249.
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) compute_metrics( metrics = c("lumped", "iFLO", "iEDO", "HAiFLO", "iFLS", "iEDS", "HAiFLS"), landuse = "landuse.tif", sites = "snapsite", out_fields = c("lumped", "iFLO", "iEDO", "HAiFLO", "iFLS", "iEDS", "HAiFLS"), watersheds = "wshed.tif", flow_dir = "fd.tif", flow_acc = "fa.tif", streams = "new_stm.tif", idwp = -1 ) }
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) compute_metrics( metrics = c("lumped", "iFLO", "iEDO", "HAiFLO", "iFLS", "iEDS", "HAiFLS"), landuse = "landuse.tif", sites = "snapsite", out_fields = c("lumped", "iFLO", "iEDO", "HAiFLO", "iFLS", "iEDS", "HAiFLS"), watersheds = "wshed.tif", flow_dir = "fd.tif", flow_acc = "fa.tif", streams = "new_stm.tif", idwp = -1 ) }
Workhorse function for rdwplus
. This function computes the spatially explicit landuse metrics in IDW-Plus (Peterson and Pearse, 2017). In contrast to compute_metrics()
, this version of the function assumes most of the intermediate data layers (i.e., flow path distance and inverse-distance weight rasters) have been precomputed.
compute_metrics_precomputed( metrics = c("lumped", "iFLO", "iFLS", "HAiFLO", "HAiFLS"), landuse, sites, out_fields, watersheds, flow_dir, flow_acc, iEDO_weights, iFLO_weights, HAiFLO_weights, iEDS_weights, iFLS_weights, HAiFLS_weights, percentage = TRUE, max_memory = 300 )
compute_metrics_precomputed( metrics = c("lumped", "iFLO", "iFLS", "HAiFLO", "HAiFLS"), landuse, sites, out_fields, watersheds, flow_dir, flow_acc, iEDO_weights, iFLO_weights, HAiFLO_weights, iEDS_weights, iFLS_weights, HAiFLS_weights, percentage = TRUE, max_memory = 300 )
metrics |
A character vector. This vector specifies which metric(s) should be calculated. Your options are lumped, iFLO, iFLS, iEDO, iEDS, HAiFLO and HAiFLS. The default is to calculate the lumped, iFLO, iFLS, HAiFLO, and HAiFLS metrics. |
landuse |
Names of landuse or landcover rasters in the current GRASS mapset. These can be continuous (e.g., percentage cover or NDVI) or binary, with a value of 1 for cells with a particular land use category and a value of 0 otherwise. |
sites |
A set of survey sites in the current GRASS mapset. |
out_fields |
A character vector of output field names to store the metrics. Note that |
watersheds |
A vector of watershed raster names in the current GRASS mapset. |
flow_dir |
Name of a flow direction raster produced by |
flow_acc |
Name of a flow accumulation raster produced by |
iEDO_weights |
A vector of names of iEDO weight rasters in the GRASS mapset. |
iFLO_weights |
A vector of names of iFLO weight rasters in the GRASS mapset. |
HAiFLO_weights |
A vector of names of HAiFLO weight rasters in the GRASS mapset. |
iEDS_weights |
A vector of names of iEDS weight rasters in the GRASS mapset. |
iFLS_weights |
A vector of names of iFLS weight rasters in the GRASS mapset. |
HAiFLS_weights |
A vector of names of HAiFLS weight rasters in the GRASS mapset. |
percentage |
A logical indicating whether the result should be expressed as a percentage. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
A sf
object of the snapped survey sites that also contains the computed landscape metrics.
Peterson, E.E. & Pearse, A.R. (2017). IDW-Plus: An ArcGIS toolset for calculating spatially explicit watershed attributes for survey sites. JAWRA, 53(5), 1241-1249.
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get pour points coord_to_raster("snapsite", which = 1, out = "pour_point") # Get iFLO weights compute_iFLO_weights( "pour_point", "wshed.tif", "null_stm.tif", "fd.tif", "fl_outlet.tif", "iFLO_weights.tif", idwp = -1, remove_streams = FALSE ) # Get iFLS weights compute_iFLS_weights( "new_stm.tif", "null_stm.tif", "fd.tif", "fl_streams.tif", "iFLS_weights.tif", idwp = -1, watershed = "wshed.tif", remove_streams = FALSE, overwrite = T ) # Compute metrics for this site compute_metrics_precomputed( metrics = c("iFLO", "iFLS"), landuse = "landuse.tif", sites = "snapsite", out_fields = c("iFLO", "iFLS"), watersheds = "wshed.tif", iFLO_weights = "iFLO_weights.tif", iFLS_weights = "iFLS_weights.tif", flow_dir = "fd.tif", flow_acc = "fa.tif" ) }
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) # Get pour points coord_to_raster("snapsite", which = 1, out = "pour_point") # Get iFLO weights compute_iFLO_weights( "pour_point", "wshed.tif", "null_stm.tif", "fd.tif", "fl_outlet.tif", "iFLO_weights.tif", idwp = -1, remove_streams = FALSE ) # Get iFLS weights compute_iFLS_weights( "new_stm.tif", "null_stm.tif", "fd.tif", "fl_streams.tif", "iFLS_weights.tif", idwp = -1, watershed = "wshed.tif", remove_streams = FALSE, overwrite = T ) # Compute metrics for this site compute_metrics_precomputed( metrics = c("iFLO", "iFLS"), landuse = "landuse.tif", sites = "snapsite", out_fields = c("iFLO", "iFLS"), watersheds = "wshed.tif", iFLO_weights = "iFLO_weights.tif", iFLS_weights = "iFLS_weights.tif", flow_dir = "fd.tif", flow_acc = "fa.tif" ) }
Given a raster in float, double or any other format, this function will convert it to integer format. This can be important because it is often an unstated requirement of GRASS modules such as the one for zonal statistics.
convert_to_integer(x, out)
convert_to_integer(x, out)
x |
A raster layer in the current GRASS mapset. |
out |
Name of the output raster. Avoid names with hyphens. |
Nothing. A raster with the name out
will be added to the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) # Make an integer-valued version of 'dem.tif' convert_to_integer("dem.tif", "int_dem.tif") # Compare plot_GRASS("dem.tif") plot_GRASS("int_dem.tif") }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) # Make an integer-valued version of 'dem.tif' convert_to_integer("dem.tif", "int_dem.tif") # Compare plot_GRASS("dem.tif") plot_GRASS("int_dem.tif") }
Given a set of x-y coordinates, this function will return a raster with a single cell at those coordinates.
coord_to_raster(outlets, which, out, overwrite = FALSE)
coord_to_raster(outlets, which, out, overwrite = FALSE)
outlets |
The name of a set of sites in the current GRASS mapset. |
which |
A numeric identifier for the site to convert to raster. |
out |
The file name of the output outlet raster in the current GRASS mapset. |
overwrite |
Whether the output files should be allowed to overwrite existing files. Defaults to |
This function is exposed to the user, and users are welcome to use if convenient for them, this function is intended for internal use in other functions.
Nothing.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sts <- system.file("extdata", "sites.shp", packages = "rdwplus") # Set environment parameters set_envir(dem) # Read in sites vector_to_mapset(sts) # Convert first site to raster coord_to_raster("site", 1, "coords", overwrite = TRUE) }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sts <- system.file("extdata", "sites.shp", packages = "rdwplus") # Set environment parameters set_envir(dem) # Read in sites vector_to_mapset(sts) # Convert first site to raster coord_to_raster("site", 1, "coords", overwrite = TRUE) }
This function computes flow direction and accumulation (among other things) from a DEM. This is done using the r.watershed
tool in GRASS.
derive_flow( dem, flow_dir, flow_acc, d8 = TRUE, overwrite = FALSE, max_memory = 300, ... )
derive_flow( dem, flow_dir, flow_acc, d8 = TRUE, overwrite = FALSE, max_memory = 300, ... )
dem |
A digital elevation model that has been hydrologically corrected. |
flow_dir |
The name of the output flow direction file in the current GRASS mapset. |
flow_acc |
The name of the output flow accumulation file in the current GRASS mapset. |
d8 |
A logical indicating whether D8 flow direction should be used. If |
overwrite |
A logical indicating whether any of the outputs should be allowed to overwrite existing files. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
... |
Additional arguments to |
Nothing. Files are written in the current GRASS mapset.
if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = c(stream_shp)) # Create binary stream out_name <- paste0(tempdir(), "/streams_rast.tif") rasterise_stream("streams", out_name, overwrite = TRUE) reclassify_streams("streams_rast.tif", "streams_binary.tif", out_type = "binary", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Plot plot_GRASS("fdir.tif", col = topo.colors(6)) plot_GRASS("facc.tif", col = topo.colors(6)) }
if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = c(stream_shp)) # Create binary stream out_name <- paste0(tempdir(), "/streams_rast.tif") rasterise_stream("streams", out_name, overwrite = TRUE) reclassify_streams("streams_rast.tif", "streams_binary.tif", out_type = "binary", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Plot plot_GRASS("fdir.tif", col = topo.colors(6)) plot_GRASS("facc.tif", col = topo.colors(6)) }
Derive a raster and a vector layer of stream lines from a flow accumulation raster.
derive_streams( dem, flow_acc, out_rast, out_vect, min_acc = 1000, min_length = 0, overwrite = FALSE, ... )
derive_streams( dem, flow_acc, out_rast, out_vect, min_acc = 1000, min_length = 0, overwrite = FALSE, ... )
dem |
Name of an elevation raster in the current GRASS mapset. |
flow_acc |
Name of a flow accumulation raster in the current GRASS mapset. |
out_rast |
Name of the output raster dataset of stream lines. File extensions should not matter. |
out_vect |
Name of the output vector dataset of stream lines. Should be WITHOUT .shp extension. |
min_acc |
The minimum accumulation value that a cell needs to be classified as a stream. Defaults to |
min_length |
The minimum length of a stream segment in cells. Defaults to |
overwrite |
A logical indicating whether the output should be allowed to overwrite existing files. Defaults to |
... |
Additional arguments to |
Nothing. A vector dataset with the name basename(out)
will appear in the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) }
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) }
Remove sinks in a DEM (see the 'Details' section)
fill_sinks(dem, out_dem, out_fd, out_sinks, overwrite = FALSE, ...)
fill_sinks(dem, out_dem, out_fd, out_sinks, overwrite = FALSE, ...)
dem |
The name of a DEM in the current GRASS mapset. |
out_dem |
Name of the output DEM, which is a hydrologically corrected (sink-filled) DEM. |
out_fd |
Name of the output flow direction map for the sink-filled DEM. |
out_sinks |
Optional argument giving the name of the output sinks raster. Leave this missing to skip the output. |
overwrite |
A logical indicating whether the output should be allowed to overwrite existing files. Defaults to |
... |
Optional additional parameters to |
A sink is a depression in a DEM. Water flows into these depressions but does not flow out of them. These depressions, although often real features of landscapes, are problematic for flow direction and accumulation algorithms. Therefore, it is common practice to remove these depressions.
Nothing. A file with the name out
will be created in the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) }
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) }
This function is needed to compute Euclidean distance from a feature of interest in a watershed raster.
get_distance(target, out, overwrite = FALSE)
get_distance(target, out, overwrite = FALSE)
target |
File name of the watershed outlet or streams (as a raster) in the current GRASS mapset. |
out |
File path for the result to be written. |
overwrite |
A logical indicating whether the outputs of this function should be allowed to overwrite existing files. |
Nothing. A file with the name basename(out)
will be created in the current GRASS mapset.
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Get distances get_distance("new_stm.tif", "dist_from_stream.tif", T) }
if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Get distances get_distance("new_stm.tif", "dist_from_stream.tif", T) }
Given a (hydrologically corrected, see fill_sinks
) DEM, this function produces a flow accumulation grid which shows the upstream area that flows into each cell in the DEM. Note that this function calls r.stream.distance
, which is a GRASS GIS add-on. It can be installed through the GRASS GUI.
get_flow_length( str_rast, flow_dir, out, to_outlet = FALSE, overwrite = FALSE, max_memory = 300 )
get_flow_length( str_rast, flow_dir, out, to_outlet = FALSE, overwrite = FALSE, max_memory = 300 )
str_rast |
Rasterized unary streams file. |
flow_dir |
Flow direction raster. |
out |
A file name for the output raster of flow lengths. |
to_outlet |
Calculate parameters for outlets flag. Defaults to |
overwrite |
Overwrite flag. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
Nothing. A file with the name out
will be written to GRASS's current workspace.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) vector_to_mapset(vectors = c(sites, stream_shp)) # Create binary stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Derive watershed get_watersheds(sites = "site", flow_dir = "fdir.tif", out = "wshed.tif", overwrite = T) # Set mask set_mask("wshed.tif") # Get flow length get_flow_length( str_rast = "streams_rast.tif", flow_dir = "fdir.tif", out = "flowlength.tif", to_outlet = TRUE, overwrite = TRUE ) # Plot plot_GRASS("flowlength.tif", col = topo.colors(15)) }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) vector_to_mapset(vectors = c(sites, stream_shp)) # Create binary stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Derive watershed get_watersheds(sites = "site", flow_dir = "fdir.tif", out = "wshed.tif", overwrite = T) # Set mask set_mask("wshed.tif") # Get flow length get_flow_length( str_rast = "streams_rast.tif", flow_dir = "fdir.tif", out = "flowlength.tif", to_outlet = TRUE, overwrite = TRUE ) # Plot plot_GRASS("flowlength.tif", col = topo.colors(15)) }
This function delineates watersheds around a set of survey sites.
get_watersheds(sites, flow_dir, out, overwrite = FALSE, lessmem = FALSE)
get_watersheds(sites, flow_dir, out, overwrite = FALSE, lessmem = FALSE)
sites |
A file path to a shapefile of points. |
flow_dir |
The name of a flow direction grid in the current GRASS mapset. |
out |
The names of the output watershed rasters. |
overwrite |
A logical indicating whether the output should be allowed to overwrite existing files. Defaults to |
lessmem |
A logical indicating whether to use the less memory modified watershed module. Defaults to |
Nothing. A raster file with the name out
may be written to file if you have set the write_file
argument accordingly. A raster with the name basename(out)
will be imported into the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) }
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Recode streams reclassify_streams("new_stm.tif", "null_stm.tif", "none") # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) # Get watersheds get_watersheds("snapsite", "fd.tif", "wshed.tif", T) }
Some functions in the rdwplus
package rely on GRASS extensions that need to be installed prior to use. This function installs those extensions.
install_extensions()
install_extensions()
This function has no arguments. Simply run it and it will install a pre-set list of GRASS extensions.
Currently, the GRASS extension required are r.stream.snap
, r.stream.distance
, and r.wateroutlet.lessmem
.
Nothing.
# Will only run if GRASS is running if(check_running()){ install_extensions() }
# Will only run if GRASS is running if(check_running()){ install_extensions() }
Given the name of a raster in the current GRASS mapset, this function will plot it as a stars
object.
plot_GRASS(x, colours, out_x, ...)
plot_GRASS(x, colours, out_x, ...)
x |
The name of an object in the current GRASS mapset. |
colours |
Optional. A colour scale. If not supplied, the default settings in |
out_x |
Optional. If supplied, the function makes a call to |
... |
Additional arguments to |
Nothing.
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) # Plot plot_GRASS("dem.tif") # argument must match name of data set in the mapset plot_GRASS("dem.tif", heat.colors(10)) # with different colour scale }
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) # Plot plot_GRASS("dem.tif") # argument must match name of data set in the mapset plot_GRASS("dem.tif", heat.colors(10)) # with different colour scale }
Given a shapefile of outlet(s), this function will convert its contents into a raster.
point_to_raster(outlets, out, overwrite = FALSE, max_memory = 300)
point_to_raster(outlets, out, overwrite = FALSE, max_memory = 300)
outlets |
A shapefile of outlets in the current GRASS mapset. |
out |
The name of the output raster. |
overwrite |
A logical indicating whether the output should be allowed to overwrite existing files. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
Nothing. A file called out
will be created in the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = sites) # Point to raster point_to_raster(outlets = "site", out = "sites_rast.tif", overwrite = TRUE) # Check conversion success vibe_check() }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = sites) # Point to raster point_to_raster(outlets = "site", out = "sites_rast.tif", overwrite = TRUE) # Check conversion success vibe_check() }
Raster calculator (wrapper for "r.mapcalc")
rast_calc(ex, overwrite = TRUE)
rast_calc(ex, overwrite = TRUE)
ex |
A valid raster calculator expression for GRASS. |
overwrite |
Defaults to |
Nothing.
GRASS can only deal with raster and vector data in a GRASS mapset. This function takes external rasters and imports them into the current GRASS mapset.
raster_to_mapset( rasters, as_integer = rep(FALSE, length(rasters)), overwrite = FALSE, max_memory = 300, ... )
raster_to_mapset( rasters, as_integer = rep(FALSE, length(rasters)), overwrite = FALSE, max_memory = 300, ... )
rasters |
A character vector of filenames of rasters to import. |
as_integer |
A logical vector indicating whether each raster should be imported strictly in integer format. Defaults to |
overwrite |
A logical indicating whether the overwrite flag should be used. If |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
... |
Additional arguments to |
A vector of raster layer names in the GRASS mapset.
# Will only run if a GRASS session is initialised if(check_running()){ dem <- system.file("extdata", "dem.tif", package = "rdwplus") raster_to_mapset(dem) }
# Will only run if a GRASS session is initialised if(check_running()){ dem <- system.file("extdata", "dem.tif", package = "rdwplus") raster_to_mapset(dem) }
Given a shapefile of lines representing the channels of a stream network, this function will return a rasterised version of the shapefile. The raster will have the parameters of the current GRASS mapset.
rasterise_stream(streams, out, overwrite = FALSE, max_memory = 300, ...)
rasterise_stream(streams, out, overwrite = FALSE, max_memory = 300, ...)
streams |
A file name for a shapefile of stream edges in the current GRASS mapset. |
out |
The filename of the output. |
overwrite |
A logical indicating whether the output is allowed to overwrite existing files. Defaults to |
max_memory |
Max memory used in memory swap mode (MB). Defaults to |
... |
Additional arguments to |
Nothing. A file will be written to out
. Note that out
can be a full file path to any location in your file system. A raster with the name basename(out)
will be written to the current GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = stream_shp) # Create rasterised stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Plot plot_GRASS("streams_rast.tif") }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = stream_shp) # Create rasterised stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Plot plot_GRASS("streams_rast.tif") }
Re-format a stream raster.
reclassify_streams(stream, out, out_type = "binary", overwrite = FALSE)
reclassify_streams(stream, out, out_type = "binary", overwrite = FALSE)
stream |
Name of a streams raster in the current GRASS mapset. This can be the output from |
out |
The output file. |
out_type |
Either 'zeros_to_nodata', 'binary', 'unary', or 'none'. See Details below |
overwrite |
A logical indicating whether the output should be allowed to overwrite any existing files. Defaults to |
Given a streams raster, this function will either create a binary streams raster (0 for non-stream cells and 1 for stream cells) or a unary streams raster (1 for stream cells and NoData for all other cells). Another option is to reclassify the streams raster such that stream cells are given the value NoData and non-stream cells are given the value 1.
Do not use raster names containing dashes/hyphens. The underlying call to r.calc
will crash if the raster name contains these symbols because they are misinterpreted as math symbols.
Nothing. A file with the name out
will be written to the current GRASS mapset. This raster will be in unsigned integer format.
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) }
# Will only run if GRASS is running if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) }
GRASS GIS uses a system of mapsets.
report_mapset(which = "current")
report_mapset(which = "current")
which |
One of either 'current' (the default), which causes the function to return the current mapset, or 'possible', which causes the function to list all possible mapsets. |
Nothing.
This function writes a GRASS mapset raster to file.
retrieve_raster(layer, out_layer, overwrite = FALSE, ...)
retrieve_raster(layer, out_layer, overwrite = FALSE, ...)
layer |
The name of the raster in the GRASS mapset that is to be written out. |
out_layer |
The name of the file to be created, with the relevant file extension. |
overwrite |
A logical indicating whether the output from this function should be allowed to overwrite any existing files. Defaults to |
... |
Additional arguments to |
Nothing.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) # Retrieve raster out_name <- paste0(tempdir(), "/retrieved_dem.tif") retrieve_raster("dem.tif", out_layer = out_name, overwrite = TRUE) }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) # Retrieve raster out_name <- paste0(tempdir(), "/retrieved_dem.tif") retrieve_raster("dem.tif", out_layer = out_name, overwrite = TRUE) }
This function writes a GRASS mapset vector layer (like a shapefile) to file.
retrieve_vector(layer, out_layer, overwrite = FALSE, ...)
retrieve_vector(layer, out_layer, overwrite = FALSE, ...)
layer |
The name of the vector layer in the GRASS mapset that is to be written out. |
out_layer |
The name of the shapefile to be created (with .shp file extension). |
overwrite |
A logical indicating whether the output from this function should be allowed to overwrite any existing files. Defaults to |
... |
Additional arguments to |
Nothing.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = stream_shp) # Retrieve raster out_name <- paste0(tempdir(), "/", "retrieved_streams.shp") retrieve_vector("streams", out_layer = out_name, overwrite = TRUE) }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) vector_to_mapset(vectors = stream_shp) # Retrieve raster out_name <- paste0(tempdir(), "/", "retrieved_streams.shp") retrieve_vector("streams", out_layer = out_name, overwrite = TRUE) }
This function finds the path to potential GRASS installations. It does so in a very crude way; that is, by searching for directories that match the string 'GRASS'
.
Warning: this function works by brute force, so it may take a few minutes to find potential GRASS installations.
Note: This is not guaranteed to work. It is not hard to find the path to your computer's GRASS installation yourself. This is the preferred course of action.
search_for_grass(guide)
search_for_grass(guide)
guide |
Optional. A specific folder to search in for the GRASS installation. |
A vector of file paths to potential GRASS installations.
my_grass <- search_for_grass() my_grass
my_grass <- search_for_grass() my_grass
This function simplifies the process of setting up a GRASS environment with parameters such as cell snapping, size and mapset extent.
set_envir(file, ...)
set_envir(file, ...)
file |
The file path to a raster that should be used to set environment parameters such as the projection, cell size, extent, etc. The |
... |
Optional arguments for |
Nothing. Displays current environment settings.
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS with initGRASS if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) }
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS with initGRASS if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") # Set environment set_envir(dem) }
Set a raster mask
set_mask(x, inverse = FALSE, overwrite = TRUE, ...)
set_mask(x, inverse = FALSE, overwrite = TRUE, ...)
x |
Raster to use as a mask |
inverse |
Whether the inverse of the raster should be used as the mask. Defaults to |
overwrite |
Whether the existing mask should be overwritten. Defaults to |
... |
Optional. Additional parameters for r.mask. |
Nothing.
if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) vector_to_mapset(vectors = c(sites, stream_shp)) # Create binary stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Derive watershed get_watersheds(sites = "site", flow_dir = "fdir.tif", out = "wshed.tif", overwrite = T) # Set mask set_mask("wshed.tif") # Get flow length get_flow_length( str_rast = "streams_rast.tif", flow_dir = "fdir.tif", out = "flowlength.tif", to_outlet = TRUE, overwrite = TRUE ) # Plot plot_GRASS("flowlength.tif", col = topo.colors(15)) }
if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") sites <- system.file("extdata", "site.shp", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters and import data to GRASS set_envir(dem) raster_to_mapset(rasters = dem, as_integer = FALSE) vector_to_mapset(vectors = c(sites, stream_shp)) # Create binary stream rasterise_stream("streams", "streams_rast.tif", overwrite = TRUE) # Burn dem burn_in(dem = "dem.tif", stream = "streams_binary.tif", out = "dem_burn.tif", burn = 10, overwrite = TRUE) # Fill sinks fill_sinks(dem = "dem_burn.tif", out_dem = "dem_fill.tif", out_fd = "fd1.tif", overwrite = TRUE) # Derive flow accumulation and direction grids derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", flow_acc = "facc.tif", overwrite = TRUE) # Derive watershed get_watersheds(sites = "site", flow_dir = "fdir.tif", out = "wshed.tif", overwrite = T) # Set mask set_mask("wshed.tif") # Get flow length get_flow_length( str_rast = "streams_rast.tif", flow_dir = "fdir.tif", out = "flowlength.tif", to_outlet = TRUE, overwrite = TRUE ) # Plot plot_GRASS("flowlength.tif", col = topo.colors(15)) }
Prevents the printing GRASS warnings, etc. Use with extreme caution. This is only helpful IF AND ONLY IF you are SURE that any printed messages, warnings, and errors are spurious.
silence(value)
silence(value)
value |
A logical indicating whether GRASS messages, warnings, errors should be suppressed. Can be missing, and it is missing by default. Choose "TRUE" or "FALSE". |
A logical indicating the current status of the option.
silence(TRUE) silence(FALSE)
silence(TRUE) silence(FALSE)
This function takes a set of survey site locations and snaps them to the highest-value cell within a flow accumulation raster, within a specified distance. Note that this function calls r.stream.snap
, which is a GRASS GIS add-on. It can be installed through the GRASS GUI.
snap_sites( sites, stream, flow_acc, max_move, out, overwrite = FALSE, max_memory = 300, ... )
snap_sites( sites, stream, flow_acc, max_move, out, overwrite = FALSE, max_memory = 300, ... )
sites |
File name for a shapefile containing the locations of the survey sites in the current GRASS mapset. |
stream |
Name of a stream raster in the current GRASS mapset. This can either be formatted to have NoData in non-stream cells or 0s in non-stream cells. |
flow_acc |
Name of a flow accumulation raster in the current GRASS mapset. |
max_move |
The maximum distance in cells that any site can be moved to snap it to the flow accumulation grid. |
out |
Name of the output in the current GRASS mapset. Note that this function will add a column called |
overwrite |
Whether the output should be allowed to overwrite any existing files. Defaults to |
max_memory |
Max memory (in) used in memory swap mode. Defaults to |
... |
Additional arguments to |
Nothing.
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) }
# Will only run if GRASS is running # You should load rdwplus and initialise GRASS via the initGRASS function if(check_running()){ # Retrieve paths to data sets dem <- system.file("extdata", "dem.tif", package = "rdwplus") lus <- system.file("extdata", "landuse.tif", package = "rdwplus") sts <- system.file("extdata", "site.shp", package = "rdwplus") stm <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment set_envir(dem) # Get other data sets (stream layer, sites, land use, etc.) raster_to_mapset(lus) vector_to_mapset(c(stm, sts)) # Reclassify streams out_stream <- paste0(tempdir(), "/streams.tif") rasterise_stream("streams", out_stream, TRUE) reclassify_streams("streams.tif", "streams01.tif", overwrite = TRUE) # Burn in the streams to the DEM burn_in("dem.tif", "streams01.tif", "burndem.tif", overwrite = TRUE) # Fill dem fill_sinks("burndem.tif", "filldem.tif", "fd1.tif", "sinks.tif", overwrite = TRUE) # Derive flow direction and accumulation grids derive_flow("dem.tif", "fd.tif", "fa.tif", overwrite = T) # Derive a new stream raster from the FA grid derive_streams("dem.tif", "fa.tif", "new_stm.tif", "new_stm", min_acc = 200, overwrite = T) # Snap sites to streams and flow accumulation snap_sites("site", "new_stm.tif", "fa.tif", 2, "snapsite", T) }
This function detects whether output suppression is on or off, and switches it to its opposite state. Under one setting, this function can be used as an off-switch for the GRASS message/warning/error suppression enforced via the use of silence(value = TRUE)
.
toggle_silence(stay_off = TRUE)
toggle_silence(stay_off = TRUE)
stay_off |
A logical indicating whether output suppression should be kept off once it is turned off. That is, if this function is called but output suppression is already off, then for |
A logical indicating whether output suppression is active.
# Even if silence is currently off, silence will stay off toggle_silence(TRUE) # If silence is currently off, silence will be turned on. toggle_silence(FALSE)
# Even if silence is currently off, silence will stay off toggle_silence(TRUE) # If silence is currently off, silence will be turned on. toggle_silence(FALSE)
GRASS can only deal with raster and vector data in a GRASS mapset. This function takes external vectors and imports them into the current GRASS mapset.
vector_to_mapset(vectors, overwrite = FALSE, ...)
vector_to_mapset(vectors, overwrite = FALSE, ...)
vectors |
A character vector of filenames of shapefiles to import. |
overwrite |
A logical indicating whether the overwrite flag should be used. Defaults to |
... |
Additional arguments to |
A vector of vector layer names in the GRASS mapset.
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters set_envir(dem) # Import vector data to mapset vector_to_mapset(vectors = stream_shp) }
# Will only run if GRASS is running if(check_running()){ # Load data set dem <- system.file("extdata", "dem.tif", package = "rdwplus") stream_shp <- system.file("extdata", "streams.shp", package = "rdwplus") # Set environment parameters set_envir(dem) # Import vector data to mapset vector_to_mapset(vectors = stream_shp) }
This function takes no inputs. It prints a list of data sets in the current GRASS mapset, as well as the parameters of the current computation region.
vibe_check()
vibe_check()
Nothing.
if(check_running()) vibe_check()
if(check_running()) vibe_check()