hf_hike: Shortest path between points

View source: R/hf_hike.R

hf_hikeR Documentation

Shortest path between points

Description

Calculate the "shortest" or least-cost paths between multiple start and end points. For each from-point, the shortest path to every to-point is calculated.

Usage

hf_hike(x, from, to, add_cost = FALSE)

Arguments

x

a cost terrain generated by hf_terrain().

from

an sf specifying start locations, must be POINT or MULTIPOINT.

to

an sf specifying end locations, must be POINT or MULTIPOINT.

add_cost

logical, whether to include a column of travel costs.

Value

An sf object with least-cost LINE features and two columns: from and to. Values in the from- and to-columns are 1:nrow(from) and 1:nrow(to), respectively. If add_cost = TRUE, the sf object also includes a travel cost column.

Examples

library(sf)
library(terra)

fn <- system.file("extdata/red_butte_dem.tif", package = "hiker")
red_butte_dem <- rast(fn)

from <- st_sf(geometry = st_sfc(st_point(c(432000, 4514000)),
                                st_point(c(434000, 4518000)),
                                crs = 26912))

to <- st_sf(geometry = st_sfc(st_point(c(431000, 4515000)),
                              st_point(c(436500, 4518500)),
                              crs = 26912))

terrain <- hf_terrain(red_butte_dem)

short_paths <- hf_hike(terrain, from, to)

plot(red_butte_dem)
plot(st_geometry(short_paths), lty = 2, add = TRUE)
plot(st_geometry(from), pch = 19, col = "red2", add = TRUE)
plot(st_geometry(to), pch = 19, col = "dodgerblue4", add = TRUE)


kbvernon/hiker documentation built on Dec. 9, 2022, 11:16 p.m.