diff --git a/R/slopes.R b/R/slopes.R index 0c95f1a..d781eb5 100644 --- a/R/slopes.R +++ b/R/slopes.R @@ -215,6 +215,7 @@ slope_matrices = function(m_xyz_split, fun = slope_matrix_weighted, ...) { #' datasets. Default: `"bilinear"`. #' @param fun The slope function to calculate per route, #' `slope_matrix_weighted` by default. +#' @inheritParams slope_vector #' @importFrom methods is #' @return A vector of slopes equal in length to the number simple features #' (rows representing linestrings) in the input object. @@ -224,13 +225,16 @@ slope_matrices = function(m_xyz_split, fun = slope_matrix_weighted, ...) { #' dem = dem_lisbon_raster #' (s = slope_raster(routes, dem)) #' cor(routes$Avg_Slope, s) +#' slope_raster(routes, dem, directed = TRUE) +#' slope_raster(sf::st_reverse(routes), dem, directed = TRUE) slope_raster = function( routes, dem, lonlat = sf::st_is_longlat(routes), method = "bilinear", fun = slope_matrix_weighted, - terra = has_terra() && methods::is(dem, "SpatRaster") + terra = has_terra() && methods::is(dem, "SpatRaster"), + directed = FALSE ) { if(is.na(lonlat)) { stop( @@ -245,7 +249,7 @@ slope_raster = function( # colnames(m) z = elevation_extract(m, dem, method = method, terra = terra) m_xyz_df = data.frame(x = m[, "X"], y = m[, "Y"], z = z, L1 = m[, "L1"]) - res = slope_xyz(m_xyz_df, fun = fun, lonlat = lonlat) + res = slope_xyz(m_xyz_df, fun = fun, lonlat = lonlat, directed = directed) res } @@ -260,14 +264,25 @@ slope_raster = function( #' @examples #' route_xyz = lisbon_road_segment_3d #' slope_xyz(route_xyz, lonlat = FALSE) -slope_xyz = function(route_xyz, fun = slope_matrix_weighted, lonlat = TRUE) { +#' slope_xyz(route_xyz, lonlat = FALSE, directed = TRUE) +slope_xyz = function( + route_xyz, + fun = slope_matrix_weighted, + lonlat = TRUE, + directed = FALSE + ) { if(inherits(route_xyz, "sf") | inherits(route_xyz, "sfc")) { lonlat = sf::st_is_longlat(route_xyz) route_xyz = as.data.frame(sf::st_coordinates(route_xyz)) } if("L1" %in% colnames(route_xyz)) { m_xyz_split = split(x = route_xyz, f = route_xyz[, "L1"]) - res = slope_matrices(m_xyz_split, lonlat = lonlat, fun = fun) + res = slope_matrices( + m_xyz_split, + lonlat = lonlat, + fun = fun, + directed = directed + ) } else { # todo: add content here if data frame was generated by sfheaders # or another package that does not call id colums 'L1' by default diff --git a/man/slope_raster.Rd b/man/slope_raster.Rd index ecf277f..da90a35 100644 --- a/man/slope_raster.Rd +++ b/man/slope_raster.Rd @@ -10,7 +10,8 @@ slope_raster( lonlat = sf::st_is_longlat(routes), method = "bilinear", fun = slope_matrix_weighted, - terra = has_terra() && methods::is(dem, "SpatRaster") + terra = has_terra() && methods::is(dem, "SpatRaster"), + directed = FALSE ) } \arguments{ @@ -32,6 +33,10 @@ datasets. Default: \code{"bilinear"}.} \item{terra}{Should the \code{terra} package be used? \code{TRUE} by default if the package is installed \emph{and} if \code{dem} is of class \code{SpatRast}} + +\item{directed}{Should the value be directed? \code{FALSE} by default. +If \code{TRUE} the result will be negative when it represents a downslope +(when the end point is lower than the start point).} } \value{ A vector of slopes equal in length to the number simple features diff --git a/man/slope_xyz.Rd b/man/slope_xyz.Rd index ce5f10b..31327a4 100644 --- a/man/slope_xyz.Rd +++ b/man/slope_xyz.Rd @@ -4,7 +4,12 @@ \alias{slope_xyz} \title{Extract slopes from xyz data frame or sf objects} \usage{ -slope_xyz(route_xyz, fun = slope_matrix_weighted, lonlat = TRUE) +slope_xyz( + route_xyz, + fun = slope_matrix_weighted, + lonlat = TRUE, + directed = FALSE +) } \arguments{ \item{route_xyz}{An sf object with x, y, z dimensions} @@ -13,6 +18,10 @@ slope_xyz(route_xyz, fun = slope_matrix_weighted, lonlat = TRUE) \code{slope_matrix_weighted} by default.} \item{lonlat}{Are the coordinates in lon/lat order? TRUE by default} + +\item{directed}{Should the value be directed? \code{FALSE} by default. +If \code{TRUE} the result will be negative when it represents a downslope +(when the end point is lower than the start point).} } \value{ A vector of slopes equal in length to the number simple features @@ -24,4 +33,5 @@ Extract slopes from xyz data frame or sf objects \examples{ route_xyz = lisbon_road_segment_3d slope_xyz(route_xyz, lonlat = FALSE) +slope_xyz(route_xyz, lonlat = FALSE, directed = TRUE) }