Skip to content

Commit

Permalink
Close #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Sep 3, 2021
1 parent 846cd86 commit 0481715
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
23 changes: 19 additions & 4 deletions R/slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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
}

Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion man/slope_raster.Rd

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

12 changes: 11 additions & 1 deletion man/slope_xyz.Rd

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

0 comments on commit 0481715

Please sign in to comment.