Package 'slopes'

Title: Calculate Slopes of Roads, Rivers and Trajectories
Description: Calculates the slope (longitudinal gradient or steepness) of linear geographic features such as roads (for more details, see Ariza-López et al. (2019) <doi:10.1038/s41597-019-0147-x>) and rivers (for more details, see Cohen et al. (2018) <doi:10.1016/j.jhydrol.2018.06.066>). It can use local Digital Elevation Model (DEM) data or download DEM data via the 'ceramic' package. The package also provides functions to add elevation data to linestrings and visualize elevation profiles.
Authors: Robin Lovelace [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-5679-6536>), Rosa Félix [aut] (ORCID: <https://orcid.org/0000-0002-5642-6006>), Joey Talbot [aut] (ORCID: <https://orcid.org/0000-0002-6520-4560>), Gonçalo Matos [aut] (ORCID: <https://orcid.org/0009-0001-3489-1732>), Dan Olner [rev] (Dan reviewed the package for rOpenSci, see https://github.com/ropensci/software-review/issues/420#issuecomment-857662657), Andy Teucher [rev] (Andy reviewed the package for rOpenSci, see https://github.com/ropensci/software-review/issues/420#issuecomment-858231647)
Maintainer: Robin Lovelace <[email protected]>
License: GPL-3
Version: 1.0.1.9000
Built: 2025-09-29 12:45:57 UTC
Source: https://github.com/ropensci/slopes

Help Index


Cycle route data

Description

Cycle route data

Usage

cyclestreets_route

Format

An sf object


Lisbon elevation raster

Description

Lisbon elevation raster

Usage

dem_lisbon_raster

Format

A raster object


Extract distance and elevation data from route

Description

Extracts cumulative distance and elevation vectors from route XYZ coordinates.

Usage

distance_z(route_xyz, lonlat)

Arguments

route_xyz

An sf object with XYZ coordinates

lonlat

Logical, whether coordinates are longitude/latitude

Value

List with components d (distances) and z (elevations)


Add elevation data to route linestrings

Description

Adds elevation (Z) coordinates to linestring geometries using DEM data.

Usage

elevation_add(
  routes,
  dem = NULL,
  method = "bilinear",
  terra = has_terra() && methods::is(dem, "SpatRaster")
)

Arguments

routes

An sf object containing linestring geometries

dem

A raster object containing elevation data (default: NULL for automatic download)

method

Method for raster extraction (default: "bilinear")

terra

Logical, whether to use terra package (default: auto-detected)

Value

An sf object with XYZ linestring geometries


Extract elevation values from coordinates

Description

Extracts elevation values from a DEM raster at specified coordinate locations.

Usage

elevation_extract(
  m,
  dem,
  method = "bilinear",
  terra = has_terra() && methods::is(dem, "SpatRaster")
)

Arguments

m

Matrix or sf object with coordinates

dem

A raster object containing elevation data

method

Method for raster extraction (default: "bilinear")

terra

Logical, whether to use terra package (default: auto-detected)

Value

Numeric vector of elevation values


Get elevation data for routes

Description

Downloads elevation data using the ceramic package for given routes.

Usage

elevation_get(routes, ..., output_format = "raster")

Arguments

routes

An sf object containing linestring geometries

...

Additional arguments passed to ceramic::cc_elevation

output_format

Character string specifying output format ("raster" or "terra")

Value

Elevation raster data covering the routes


Lisbon road network

Description

Lisbon road network

Usage

lisbon_road_network

Format

An sf object


Lisbon road segment

Description

Lisbon road segment

Usage

lisbon_road_segment

Format

An sf object


Lisbon road segment 3D

Description

Lisbon road segment 3D

Usage

lisbon_road_segment_3d

Format

An sf object


Lisbon road segment XYZ

Description

Lisbon road segment XYZ

Usage

lisbon_road_segment_xyz_mapbox

Format

An sf object


Lisbon route data

Description

Lisbon route data

Usage

lisbon_route

Format

An sf object


Lisbon route 3D

Description

Lisbon route 3D

Usage

lisbon_route_3d

Format

An sf object


Lisbon route XYZ

Description

Lisbon route XYZ

Usage

lisbon_route_xyz_mapbox

Format

An sf object


Magnolia coordinates

Description

Magnolia coordinates

Usage

magnolia_xy

Format

A data frame


Create slope breaks for color mapping

Description

Creates symmetric slope breaks around zero for color classification.

Usage

make_breaks(brks)

Arguments

brks

Vector of positive slope break values (as percentages)

Value

Vector of slope breaks including negative values and zero


Assign colors to slope values

Description

Maps slope gradient values to colors based on break points.

Usage

make_colz(g, b, pal)

Arguments

g

Vector of slope gradient values

b

Vector of break points

pal

Vector of colors corresponding to breaks

Value

Character vector of colors for each slope value


Create color palette for slope visualization

Description

Creates or processes color palettes for slope gradient visualization.

Usage

make_pal(pal, b)

Arguments

pal

Color palette (function or character vector)

b

Vector of breaks for color mapping

Value

Character vector of colors


Plot distance-elevation profile with slope coloring

Description

Creates a distance-elevation plot with segments colored by slope gradient.

Usage

plot_dz(
  d,
  z,
  fill = TRUE,
  horiz = FALSE,
  pal = NULL,
  ...,
  legend_position = "top",
  col = "black",
  cex = 0.9,
  bg = grDevices::rgb(1, 1, 1, 0.8),
  title = "Slope colors (percentage gradient)",
  brks = c(3, 6, 10, 20, 40, 100),
  seq_brks = NULL,
  ncol = 4
)

Arguments

d

Vector of cumulative distances

z

Vector of elevation values

fill

Logical, whether to fill segments with slope colors (default: TRUE)

horiz

Logical, whether legend should be horizontal (default: FALSE)

pal

Color palette for slope visualization (default: NULL, uses slopes_palette)

...

Additional arguments passed to graphics functions

legend_position

Position of legend (default: "top")

col

Color of the elevation profile line (default: "black")

cex

Character expansion factor for legend text (default: 0.9)

bg

Background color for legend (default: semi-transparent white)

title

Title for the legend (default: "Slope colors (percentage gradient)")

brks

Vector of slope break points for coloring (default: c(3, 6, 10, 20, 40, 100))

seq_brks

Sequence of breaks to show in legend (default: NULL, auto-generated)

ncol

Number of columns in legend (default: 4)

Value

NULL (creates plot as side effect)


Plot elevation profile with slope coloring

Description

Creates an elevation profile plot from route geometries with XYZ coordinates, with segments colored according to slope gradient.

Usage

plot_slope(
  route_xyz,
  lonlat = sf::st_is_longlat(route_xyz),
  fill = TRUE,
  horiz = FALSE,
  pal = NULL,
  legend_position = "top",
  col = "black",
  cex = 0.9,
  bg = grDevices::rgb(1, 1, 1, 0.8),
  title = "Slope colors (percentage gradient)",
  brks = c(3, 6, 10, 20, 40, 100),
  seq_brks = seq(from = 3, to = length(brks) * 2 - 2),
  ncol = 4,
  ...
)

Arguments

route_xyz

An sf object containing linestring geometries with XYZ coordinates

lonlat

Logical, whether coordinates are longitude/latitude (default: auto-detected)

fill

Logical, whether to fill segments with slope colors (default: TRUE)

horiz

Logical, whether legend should be horizontal (default: FALSE)

pal

Color palette for slope visualization (default: NULL, uses slopes_palette)

legend_position

Position of legend (default: "top")

col

Color of the elevation profile line (default: "black")

cex

Character expansion factor for legend text (default: 0.9)

bg

Background color for legend (default: semi-transparent white)

title

Title for the legend (default: "Slope colors (percentage gradient)")

brks

Vector of slope break points for coloring (default: c(3, 6, 10, 20, 40, 100))

seq_brks

Sequence of breaks to show in legend (default: auto-generated)

ncol

Number of columns in legend (default: 4)

...

Additional arguments passed to plot_dz

Value

NULL (creates plot as side effect)


Calculate sequential distances between points

Description

Calculates distances between consecutive points in a coordinate matrix.

Usage

sequential_dist(m, lonlat = TRUE)

Arguments

m

Matrix of coordinates (x, y)

lonlat

Logical, whether coordinates are longitude/latitude (default: TRUE)

Value

Numeric vector of distances between consecutive points


Extract midpoint and extent from routes in lonlat

Description

Internal helper function to get midpoint and extent of routes in lon/lat coordinates.

Usage

sf_mid_ext_lonlat(routes)

Arguments

routes

An sf object containing linestring geometries

Value

A list with midpoint coordinates and width/height dimensions


Calculate slopes using distance data

Description

Calculates slope gradients from distance and elevation vectors.

Usage

slope_distance(d, elevations)

Arguments

d

Vector of distance values between points

elevations

Vector of elevation values

Value

Numeric vector of slope values


Calculate mean slope using distance weighting

Description

Computes the mean slope across segments using distance-weighted averaging.

Usage

slope_distance_mean(d, elevations, directed = FALSE)

Arguments

d

Vector of distance values between points

elevations

Vector of elevation values

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric value representing the mean slope


Calculate distance-weighted slopes

Description

Applies distance-based weighting to slope calculations for more accurate results.

Usage

slope_distance_weighted(d, elevations, directed = FALSE)

Arguments

d

Vector of distance values between points

elevations

Vector of elevation values

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric value representing the weighted slope


Calculate slopes for multiple coordinate matrices

Description

Applies slope calculation function to a list of coordinate matrices.

Usage

slope_matrices(m_xyz_split, fun = slope_matrix_weighted, ...)

Arguments

m_xyz_split

List of coordinate matrices with elevation data

fun

Function to apply for slope calculation (default: slope_matrix_weighted)

...

Additional arguments passed to the slope function

Value

Numeric vector of slope values for all matrices


Calculate slopes from coordinate matrix

Description

Calculates slope gradients from a matrix of coordinates and elevation data.

Usage

slope_matrix(m, elevations = m[, 3], lonlat = TRUE)

Arguments

m

Matrix of coordinates (x, y, z)

elevations

Vector of elevation values (default: third column of m)

lonlat

Logical, whether coordinates are longitude/latitude (default: TRUE)

Value

Numeric vector of slope values


Calculate mean slope from coordinate matrix

Description

Computes the mean slope from a matrix of coordinates with elevation data.

Usage

slope_matrix_mean(m, elevations = m[, 3], lonlat = TRUE, directed = FALSE)

Arguments

m

Matrix of coordinates (x, y, z)

elevations

Vector of elevation values (default: third column of m)

lonlat

Logical, whether coordinates are longitude/latitude (default: TRUE)

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric value representing the mean slope


Convert slope matrix to raster

Description

Converts a slope matrix or checks if input is already a raster.

Usage

slope_matrix_to_raster(x)

Arguments

x

A matrix or RasterLayer object

Value

A RasterLayer object


Calculate weighted slopes from coordinate matrix

Description

Applies distance-based weighting to slope calculations from coordinate matrix.

Usage

slope_matrix_weighted(m, elevations = m[, 3], lonlat = TRUE, directed = FALSE)

Arguments

m

Matrix of coordinates (x, y, z)

elevations

Vector of elevation values (default: third column of m)

lonlat

Logical, whether coordinates are longitude/latitude (default: TRUE)

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric value representing the weighted slope


Calculate slopes using raster elevation data

Description

Calculates slope gradients for routes using digital elevation model (DEM) raster data.

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

An sf object containing linestring geometries

dem

A raster object containing elevation data

lonlat

Logical, whether coordinates are longitude/latitude (default: auto-detected)

method

Method for raster extraction (default: "bilinear")

fun

Function for slope calculation (default: slope_matrix_weighted)

terra

Logical, whether to use terra package (default: auto-detected)

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric vector of slope values


Calculate slopes from vector data

Description

Calculates slope gradients using vector distance and elevation data.

Usage

slope_vector(x, elevations)

Arguments

x

Vector of distance values

elevations

Vector of elevation values

Value

Numeric vector of slope values


Calculate slopes from XYZ coordinate data

Description

Calculates slope gradients from linestring geometries with XYZ coordinates.

Usage

slope_xyz(
  route_xyz,
  fun = slope_matrix_weighted,
  lonlat = TRUE,
  directed = FALSE
)

Arguments

route_xyz

An sf object or data frame with XYZ coordinates

fun

Function for slope calculation (default: slope_matrix_weighted)

lonlat

Logical, whether coordinates are longitude/latitude (default: TRUE)

directed

Logical, whether to calculate directed slopes (default: FALSE)

Value

Numeric vector of slope values


Extract XYZ coordinates from raster or matrix

Description

Simplifies raster or matrix data to XYZ coordinate format.

Usage

slope_xyz_simple(x)

Arguments

x

A RasterLayer or matrix object

Value

A data frame with x, y, z coordinates


Get color palette for slopes visualization

Description

Returns a color palette suitable for visualizing slope data, with options for different color schemes.

Usage

slopes_palette(n = 6, palette = "Green-Brown")

Arguments

n

Number of colors to return (default: 6)

palette

Name of the color palette to use (default: "Green-Brown")

Value

A character vector of color codes

Examples

# Get default Green-Brown palette with 6 colors
slopes_palette()

# Get 4 colors from Green-Brown palette
slopes_palette(n = 4)

# Use a different palette
slopes_palette(n = 5, palette = "Blue-Red")