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.
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 )
routes | Routes, the gradients of which are to be calculated.
The object must be of class |
---|---|
dem | Raster overlapping with |
lonlat | Are the routes provided in longitude/latitude coordinates?
By default, value is from the CRS of the routes ( |
method | The method of estimating elevation at points,
passed to the |
fun | The slope function to calculate per route,
|
terra | Should the |
directed | Should the value be directed? |
A vector of slopes equal in length to the number simple features (rows representing linestrings) in the input object.
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 LINESTRING
s as follows:
r_linestring = sf::st_cast(routes, "LINESTRING")
.
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#> [1] 0.9975107slope_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