Package 'snakedist'

Title: Compute Distances along a Linear Shape
Description: Computes distance between pair of points along a linear shape (e.g. a river, a road, etc.). This function uses parallel computing to speed up the distance computation.
Authors: Nicolas Casajus [aut, cre, cph]
Maintainer: Nicolas Casajus <[email protected]>
License: GPL (>= 2)
Version: 1.0
Built: 2024-10-29 03:15:55 UTC
Source: https://github.com/FRBCesab/snakedist

Help Index


Convert a data.frame to a distance matrix

Description

Converts a data.frame to a distance matrix.

Usage

df_to_matrix(data, lower = TRUE, upper = TRUE, diag = TRUE, na_to_zero = TRUE)

Arguments

data

a data.frame with the following three columns: from (the first site), to (the second site) and distance (the value of the link between two sites). The output of the function distance_along().

lower

a logical value. If TRUE (default), keep values in the lower triangle of the matrix. Otherwise they will be replaced by NA (or 0).

upper

a logical value. If TRUE (default), keep values in the upper triangle of the matrix. Otherwise they will be replaced by NA (or 0).

diag

a logical value. If TRUE (default), keep values in the diagonal of the matrix. Otherwise they will be replaced by NA (or 0).

na_to_zero

a logical value. If TRUE (default), missing edges are coded as 0. Otherwise they will be coded as NA.

Value

A matrix of dimensions ⁠m x n⁠, where m is the number of sites (from) and n is the number of sites (to).

Examples

# Add an example ----

Compute the distance between two locations along a linear shape

Description

Computes distance between two sites along a linear shape (e.g. a river, a road, etc.). This function uses parallel computing to speed up the distance computation.

Usage

distance_along(
  sites,
  along,
  density = 0.01,
  type = "regular",
  mc.cores = parallel::detectCores() - 1,
  ...
)

Arguments

sites

an sf object of type POINT. A spatial object containing coordinates of sites. Note that the first column must be the site identifier.

along

an sf object of type LINESTRING. A spatial object containing coordinates of the linear shape (e.g. a river, a road, etc.) to follow while computing distances between the two sites.

density

a numeric of length 1. The density of points to sample on the linear structure. See sf::st_line_sample() for further detail. Default is 1/100.

type

a character of length 1. The method to sample points on the linear shape. Either regular (default) or random.

mc.cores

an integer of length 1. The number of cores to use (must be lesser than the number of cores available on the machine obtained with parallel::detectCores()).

...

other argument to pass to sf::st_line_sample().

Value

A three-column data.frame with:

  • from, the first site

  • to, the second site

  • weight, the distance between the two sites along the linear shape

Examples

## Add an example ----

Convert a distance matrix to a data.frame

Description

Converts a (distance) matrix to a data.frame.

Usage

matrix_to_df(x, all = FALSE)

Arguments

x

a matrix.

all

a logical value. If TRUE (default), keep cells with NA.

Value

A data.frame with the following three columns: from (the first site), to (the second site) and distance (the value of the link between two sites).

Examples

# Add an example ----