Skip to content

Commit

Permalink
Add directed, progress on #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Sep 3, 2021
1 parent b274140 commit 846cd86
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Suggests:
stplanr,
dplyr,
rgdal,
tmap
tmap,
leaflet
VignetteBuilder: knitr
Config/testthat/edition: 3
33 changes: 26 additions & 7 deletions R/slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#' @param x Vector of locations
#' @param d Vector of distances between points
#' @param elevations Elevations in same units as x (assumed to be metres)
#' @param directed Should the value be directed? `FALSE` by default.
#' If `TRUE` the result will be negative when it represents a downslope
#' (when the end point is lower than the start point).
#' @return A vector of slope gradients associated with each linear element
#' (each line between consecutive vertices) associated with linear features.
#' Returned values for `slope_distance_mean()` and
Expand Down Expand Up @@ -60,7 +63,8 @@ slope_distance = function(d, elevations) {
slope_distance_mean = function(d, elevations, directed = FALSE) {
e_change = diff(elevations)
if(directed) {
mean(abs(e_change) / d) * sign(tail(elevations, 1) - head(elevations, 1))
mean(abs(e_change) / d) *
sign(utils::tail(elevations, 1) - utils::head(elevations, 1))
} else {
mean(abs(e_change) / d)
}
Expand All @@ -71,7 +75,7 @@ slope_distance_weighted = function(d, elevations, directed = FALSE) {
e_change = diff(elevations)
if(directed) {
stats::weighted.mean(abs(e_change) / d, d) *
sign(tail(elevations, 1) - head(elevations, 1))
sign(utils::tail(elevations, 1) - utils::head(elevations, 1))
} else {
stats::weighted.mean(abs(e_change) / d, d)
}
Expand Down Expand Up @@ -103,10 +107,15 @@ slope_distance_weighted = function(d, elevations, directed = FALSE) {
#' @examples
#' x = c(0, 2, 3, 4, 5, 9)
#' y = c(0, 0, 0, 0, 0, 9)
#' z = c(1, 2, 2, 4, 3, 1) / 10
#' z = c(1, 2, 2, 4, 3, 0) / 10
#' m = cbind(x, y, z)
#' slope_matrix_weighted(m, lonlat = FALSE)
#' slope_matrix_weighted(m, lonlat = FALSE, directed = TRUE)
#' # 0 value returned if no change in elevation:
#' slope_matrix_weighted(m,lonlat = FALSE, directed = TRUE,
#' elevations = c(1, 2, 2, 4, 3, 1))
#' slope_matrix_mean(m, lonlat = FALSE)
#' slope_matrix_mean(m, lonlat = FALSE, directed = TRUE)
#' plot(x, z, ylim = c(-0.5, 0.5), type = "l")
#' (gx = slope_vector(x, z))
#' (gxy = slope_matrix(m, lonlat = FALSE))
Expand All @@ -123,17 +132,27 @@ slope_matrix = function(m, elevations = m[, 3], lonlat = TRUE) {
}
#' @rdname slope_matrix
#' @export
slope_matrix_mean = function(m, elevations = m[, 3], lonlat = TRUE) {
slope_matrix_mean = function(m, elevations = m[, 3], lonlat = TRUE, directed = FALSE) {
g1 = slope_matrix(m, elevations = elevations, lonlat = lonlat)
d = sequential_dist(m = m, lonlat = lonlat)
mean(abs(g1), na.rm = TRUE)
if(directed) {
mean(abs(g1), na.rm = TRUE) *
sign(utils::tail(elevations, 1) - utils::head(elevations, 1))
} else {
mean(abs(g1), na.rm = TRUE)
}
}
#' @rdname slope_matrix
#' @export
slope_matrix_weighted = function(m, elevations = m[, 3], lonlat = TRUE) {
slope_matrix_weighted = function(m, elevations = m[, 3], lonlat = TRUE, directed = FALSE) {
g1 = slope_matrix(m, elevations = elevations, lonlat = lonlat)
d = sequential_dist(m = m, lonlat = lonlat)
stats::weighted.mean(abs(g1), d, na.rm = TRUE)
if(directed) {
stats::weighted.mean(abs(g1), d, na.rm = TRUE) *
sign(utils::tail(elevations, 1) - utils::head(elevations, 1))
} else {
stats::weighted.mean(abs(g1), d, na.rm = TRUE)
}
}

#' Calculate the sequential distances between sequential coordinate pairs
Expand Down
15 changes: 12 additions & 3 deletions man/slope_matrix.Rd

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

13 changes: 10 additions & 3 deletions man/slope_vector.Rd

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

1 change: 1 addition & 0 deletions man/z_value.Rd

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

0 comments on commit 846cd86

Please sign in to comment.