Learn R Programming

rdwplus (version 0.1.0)

snap_sites: A function to snap sites in a shapefile to a flow accumulation grid

Description

This function takes a set of survey site locations and makes sure that they are coincident with the point of highest flow accumulation within a specified distance. This is equivalent to snapping sites to a stream network. Note that this function calls r.stream.snap, which is a GRASS GIS add-on. It can be installed through the GRASS GUI.

Usage

snap_sites(
  sites,
  flow_acc,
  max_move,
  out,
  overwrite = FALSE,
  max_memory = 300,
  use_sp = TRUE,
  ...
)

Value

Nothing. Note that a shapefile of snapped survey sites will be written to the location out and a shapefile called basename(out) will be imported into the GRASS mapset.

Arguments

sites

File name for a shapefile containing the locations of the survey sites in the current GRASS mapset.

flow_acc

File name for 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

The output file path.

overwrite

Whether the output should be allowed to overwrite any existing files. Defaults to FALSE.

max_memory

Max memory used in memory swap mode (MB). Defaults to 300.

use_sp

Logical to use 'sf' or 'stars' classes for vector objects. Defaults to TRUE to use 'stars' class.

...

Additional arguments to r.stream.snap.

Examples

Run this code
# 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 = c(dem), as_integer = c(FALSE))
vector_to_mapset(vectors = c(sites, 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_fill.tif", size = 1, overwrite = TRUE)

# Derive flow accumulation and direction grids
derive_flow(dem = "dem_fill.tif", flow_dir = "fdir.tif", 
flow_acc = "facc.tif", overwrite = TRUE)

# Snap sites to pour points (based on flow accumulation)
out_snap <- paste0(tempdir(), "/snapsite.shp")
snap_sites(sites = "site", flow_acc = "facc.tif", max_move = 2, 
out = out_snap, overwrite = TRUE)

}

Run the code above in your browser using DataLab