openSTARS (version 1.2.2)

calc_attributes_edges: Calculate attributes of the edges.

Description

For each edge (i.e. stream segment) additional attributes (potential predictor variables) are derived based on given raster or vector maps.

Usage

calc_attributes_edges(
  input_raster = NULL,
  stat_rast = NULL,
  attr_name_rast = NULL,
  input_vector = NULL,
  stat_vect = NULL,
  attr_name_vect = NULL,
  round_dig = 2
)

Arguments

input_raster

name(s) of raster map(s) to calculate attributes from.

stat_rast

name(s) giving the statistics to be calculated, from the raster maps, must be one of: "min", "max", "mean", "sum", "percent", "area" for each input_raster.

attr_name_rast

of new column name(s) for the attribute(s) to be calculated. Attribute names must not be longer than 8 characters as ESRI shapefiles cannot have colum names with more than 10 characters. See notes.

input_vector

name(s) of vector map(s) to calculate attributes from.

stat_vect

name(s) giving the statistics to be calculated from the vector maps, must be one of: "count" (for point data), "percent" or "area" (for polygon data) for each input_vector.

attr_name_vect

name(s) of attribute column(s), case sensitive. For polygon data, this is the column to calculate the statistics from; the results column names are created by the content of this column. For point data, a column will be created with this name to hold the counts. See notes.

round_dig

integer; number of digits to round results to. Can be a vector of different values or just one value for all attributes. #@param clean logical; should intermediate files be deleted

Value

Nothing. The function appends new columns to the 'edges' attribute table with column names given in attr_name_rast and derived from the attribute classes for vector data. For each attribute, two columns are appended: one giving the attribute for the rca of the edge ("attribute_name_e") and one for the attribute of the total catchment of the edge ("attribute_name_c").

Details

First, the reach contributing areas (= subcatchments) for all edges are calculated. Then these are intersected with the given raster and/or vector maps and the desired statistics are computed. This function must be run before computing approximate attribute values for sites calc_attributes_sites_approx.

For stat_rast = "percent" or "area" the input_raster can be either coded as 1 and 0 (e.g., cells occupied by the land use under consideration and not) or as different classes. The percentage or area of each class in the catchment is calculated. If the input_raster consists of percentages per cell (e.g., proportional land use of a certain type per cell) stat_rast = "mean" gives the overall proportion of this land use in the catchment.

For stat_vect = "percent" or "area" input_vector must contain polygons of e.g. different land use types. The column attr_name_vect would then give the code for the different land uses. Then, the percentage for each land use type in the catchment of the edge is calculated and given in separate columns with column names resampling the different categories given in column attr_name_vect

For stat_vect = "count" input_vector must contain points of e.g. waste water treatment plants. The column attr_name_vect gives the name of the column to hold the count value, e.g. nWWTP.

Both raster and vector maps to be used must be read in to the GRASS session, either in import_data or using the GRASS function r.in.rast or v.in.ogr (see examples).

Examples

Run this code
# NOT RUN {
# Initiate and setup GRASS
dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS")
if(.Platform$OS.type == "windows"){
  grass_program_path = "c:/Program Files/GRASS GIS 7.6"
  } else {
  grass_program_path = "/usr/lib/grass78/"
  }

setup_grass_environment(dem = dem_path, 
                        gisBase = grass_program_path,      
                        remove_GISRC = TRUE,
                        override = TRUE
                        )
gmeta()
                        
# Load files into GRASS
dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS")
sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS")
streams_path <- system.file("extdata", "nc", "streams.shp", package = "openSTARS")
preds_v_path <- system.file("extdata", "nc", "pointsources.shp", package = "openSTARS")
preds_r_path <- system.file("extdata", "nc", "landuse_r.tif", package = "openSTARS")
                 
import_data(dem = dem_path, sites = sites_path, streams = streams_path,
            predictor_vector = preds_v_path, predictor_raster = preds_r_path)

# Derive streams from DEM
# burn in 'streams' 10 meters
derive_streams(burn = 10, accum_threshold = 700, condition = TRUE, clean = TRUE)

# Check and correct complex confluences (there are no complex confluences in this
# example date set; set accum_threshold in derive_streams to a smaller value
# to create complex confluences)
cj <- check_compl_confluences()
if(cj){
  correct_compl_confluences()
}

# calculate slope as potential predictor
execGRASS("r.slope.aspect", flags = c("overwrite","quiet"),
parameters = list(
  elevation = "dem",
    slope = "slope"
    )) 

# Prepare edges
calc_edges()
calc_attributes_edges(input_raster = c("slope", "landuse_r"), 
                      stat_rast = c("max", "percent"), 
                      attr_name_rast = c("maxSlo", "luse"),
                      input_vector = "pointsources", stat_vect = "count",
                      attr_name_vect = "psource")
                      
# Plot eges with percentage of forest in the catchment (lusep_5) as line width
edges <- readVECT('edges', ignore.stderr = TRUE)
head(edges@data)
lu <- readRAST("landuse_r", ignore.stderr = TRUE, plugin = FALSE)

# plot landuse data
library(raster)
op <- par()
par(xpd = FALSE)
plot(raster(lu), legend = FALSE, xaxt = "n", yaxt = "n", bty = "n",
col = adjustcolor(c("red", "goldenrod", "green", "forestgreen",
"darkgreen", "blue", "lightblue"), alpha.f = 0.7))
par(xpd = TRUE)
legend("bottom", cex = 0.75,
  legend = c("developed", "agriculture", "herbaceous", "shrubland", "forest", "water", "sediment"),
  fill = c("red", "goldenrod", "green", "forestgreen","darkgreen", "blue", "lightblue"),
  horiz = TRUE, inset = -0.175)
  # line width is relative to the area of land use class 5 (forest) in the rca of the edge segment
plot(edges, lwd = edges$lusep_5_c * 10, add = TRUE)    
par <- op
# }

Run the code above in your browser using DataCamp Workspace