vl_elevation: Get elevation along a route

View source: R/vl_elevation.R

vl_elevationR Documentation

Get elevation along a route

Description

Build and send a Valhalla API query to get the elevation at a set of input locations.
This function interfaces with the height service.
If sampling_dist is provided, the elevation is sampled at regular intervals along the input locations.

Usage

vl_elevation(loc, sampling_dist, server = getOption("valh.server"))

Arguments

loc

one (or multiples) point(s) at which to get elevation. loc can be:

  • a vector of coordinates (longitude and latitude, WGS 84),

  • a data.frame of longitudes and latitudes (WGS 84),

  • a matrix of longitudes and latitudes (WGS 84),

  • an sfc object of type POINT,

  • an sf object of type POINT.

sampling_dist

distance between each point to sample the elevation (in meters). Default is no sampling.

server

URL of the Valhalla server.

Value

An sf POINT object is returned with the following fields: 'distance' (the distance from the first points), 'height' (the sampled height on the DEM) and 'geometry' (the geometry of the sampled point).

Examples

## Not run: 
# Inputs are data frames
apotheke.df <- read.csv(system.file("csv/apotheke.csv", package = "valh"))
# The first 5 points
pts <- apotheke.df[1:5, c("lon", "lat")]
# Ask for the elevation at these points
elev1 <- vl_elevation(loc = pts)

# Inputs are sf points
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "valh"),
  quiet = TRUE
)
# The first 5 points
pts2 <- apotheke.sf[1:5, ]
# Ask for the elevation at these points
elev2 <- vl_elevation(loc = pts2)
# Ask for elevation between the first and the second points,
# sampling every 100 meters
elev3 <- vl_elevation(loc = apotheke.sf[1:2, ], sampling_dist = 100)
# Plot the corresponding elevation profile
plot(as.matrix(st_drop_geometry(elev3)), type = "l")

# Input is a route (sf LINESTRING) from vl_route
# Compute the route between the first and the second points
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "valh"),
  quiet = TRUE
)
src <- apotheke.sf[1, ]
dst <- apotheke.sf[2, ]
route <- vl_route(src = src, dst = dst)

# Split the LINESTRING into its composing points
pts_route <- sf::st_cast(route, "POINT")
# Ask for the elevation at these points
elev4 <- vl_elevation(loc = pts_route)

# Plot the elevation profile
plot(as.matrix(st_drop_geometry(elev4)), type = "l")

## End(Not run)

valh documentation built on April 11, 2025, 6:14 p.m.