Skip to contents

This function takes an sf representing routes over geographical space and a raster dataset representing the terrain as inputs. It returns the average gradient of each route feature.

Usage

slope_raster(
  routes,
  dem,
  lonlat = sf::st_is_longlat(routes),
  method = "bilinear",
  fun = slope_matrix_weighted,
  terra = has_terra() && methods::is(dem, "SpatRaster"),
  directed = FALSE
)

Arguments

routes

Routes, the gradients of which are to be calculated. The object must be of class sf or sfc with LINESTRING geometries.

dem

Raster overlapping with routes and values representing elevations

lonlat

Are the routes provided in longitude/latitude coordinates? By default, value is from the CRS of the routes (sf::st_is_longlat(routes)).

method

The method of estimating elevation at points, passed to the extract function for extracting values from raster datasets. Default: "bilinear".

fun

The slope function to calculate per route, slope_matrix_weighted by default.

terra

Should the terra package be used? TRUE by default if the package is installed and if dem is of class SpatRast

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).

Value

A vector of slopes equal in length to the number simple features (rows representing linestrings) in the input object.

Details

If calculating slopes associated with OSM data, the results may be better if the network is first split-up, e.g. using the function stplanr::rnet_breakup_vertices() from the stplanr package. Note: The routes object must have a geometry type of LINESTRING. The sf::st_cast() function can convert from MULTILINESTRING (and other) geometries to LINESTRINGs as follows: r_linestring = sf::st_cast(routes, "LINESTRING").

Examples

library(sf)
routes = lisbon_road_network[1:3, ]
dem = dem_lisbon_raster
(s = slope_raster(routes, dem))
#>           1           2           3 
#> 0.003074005 0.010870459 0.004998315 
cor(routes$Avg_Slope, s)
#> [1] 0.9975107
slope_raster(routes, dem, directed = TRUE)
#>            1            2            3 
#>  0.003074005 -0.010870459  0.004998315 
# Demonstrate that reverse routes have the opposite directed slope
slope_raster(st_reverse(routes), dem, directed = TRUE)
#>            1            2            3 
#> -0.003074005  0.010870459 -0.004998315