# Calculate the gradient of line segments from distance and elevation vectors

Source:`R/slopes.R`

`slope_vector.Rd`

`slope_vector()`

calculates the slopes associated with consecutive elements
in one dimensional distance and associated elevations (see examples).

`slope_distance()`

calculates the slopes associated with consecutive
distances and elevations.

`slope_distance_mean()`

calculates the mean average slopes associated with
consecutive distances and elevations.

`slope_distance_weighted()`

calculates the slopes associated with
consecutive distances and elevations,
with the mean value associated with each set of distance/elevation
vectors weighted in proportion to the distance between each elevation
measurement, so longer sections have proportionally more influence
on the resulting gradient estimate (see examples).

## Usage

```
slope_vector(x, elevations)
slope_distance(d, elevations)
slope_distance_mean(d, elevations, directed = FALSE)
slope_distance_weighted(d, elevations, directed = FALSE)
```

## Arguments

- x
Vector of locations

- elevations
Elevations in same units as x (assumed to be metres)

- d
Vector of distances between points

- 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 slope gradients associated with each linear element
(each line between consecutive vertices) associated with linear features.
Returned values for `slope_distance_mean()`

and
`slope_distance_mean_weighted()`

are summary statistics for all
linear elements in the linestring.
The output value is a proportion representing the change in elevation
for a given change in horizontal movement along the linestring.
0.02, for example, represents a low gradient of 2% while 0.08 represents
a steep gradient of 8%.

## Examples

```
x = c(0, 2, 3, 4, 5, 9)
elevations = c(1, 2, 2, 4, 3, 0) / 10 # downward slope overall
slope_vector(x, elevations)
#> [1] 0.050 0.000 0.200 -0.100 -0.075
library(sf)
m = st_coordinates(lisbon_road_segment)
d = sequential_dist(m, lonlat = FALSE)
elevations = elevation_extract(m, dem_lisbon_raster)
slope_distance(d, elevations)
#> [1] -0.047226259 -0.040883072 -0.025032918 -0.061124557 -0.017447060
#> [6] -0.062426272 -0.123580541 0.033705378 0.004292243 -0.040360003
#> [11] -0.151671893 -0.182367906 0.409246854 -0.034463974 -0.098406640
#> [16] -0.161798173 0.076261379 0.100654228
slope_distance_mean(d, elevations)
#> [1] 0.09283052
slope_distance_mean(d, elevations, directed = TRUE)
#> [1] -0.09283052
slope_distance_mean(rev(d), rev(elevations), directed = TRUE)
#> [1] 0.09283052
slope_distance_weighted(d, elevations)
#> [1] 0.09501323
slope_distance_weighted(d, elevations, directed = TRUE)
#> [1] -0.09501323
```