Introduction to distance-to

Overview

The distanceto package is designed to quickly sample distances from points features to other vector layers. Normally the approach for calculating distance to (something) involves generating distance surfaces using raster based approaches eg. raster::distance or gdal_proximity and subsequently point sampling these surfaces. Since raster based approaches are a costly method that frequently leads to memory issues or long and slow run times with high resolution data or large study sites, we have opted to compute these distances using vector based approaches. As a helper, there’s a decidedly low-res raster based approach for visually inspecting your region’s distance surface. But the workhorse is distance_to.

The distanceto package provides two functions:

  • distance_to
  • distance_raster

Install

# Enable the robitalec universe
options(repos = c(
    robitalec = 'https://robitalec.r-universe.dev',
    CRAN = 'https://cloud.r-project.org'))

# Install distanceto
install.packages('distanceto')

distance_to

library(distanceto)
library(sf)
#> Linking to GEOS 3.14.1, GDAL 3.12.2, PROJ 9.7.1; sf_use_s2() is TRUE

Long-lat / unprojected coordinates

# Load nc data
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/github/workspace/pkglib/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27

# Set number of sampling points
npts <- 1e3

# Sample points in nc
ncpts <- st_sample(nc, npts)

# Select first 5 of nc
ncsub <- nc[1:5,]

# Measure distance from ncpts to first 5 of nc
dist <- distance_to(ncpts, ncsub, measure = 'geodesic')

# or add to ncpts
ncpts$dist <- dist

head(dist, 30)
#>  [1] 124716.133  78117.488  54957.263 122821.737 175423.784  87367.219
#>  [7] 152858.493  54403.285 142679.469  57644.560  65236.291 199965.527
#> [13]  29754.891  81670.902 115809.176   2050.881 221260.793 124094.135
#> [19] 109058.200  81414.855  68415.209  40108.675  81643.910 268071.958
#> [25]  71655.641  53304.080 246614.032 130986.328  36246.312 146290.290
hist(dist)

Projected coordinates

# Transform nc data to local projected coordinates (UTM 18N)
nc_utm <- st_transform(nc, 32618)

# Set number of sampling points
npts <- 1e2

# Sample points within nc data
nc_utm_pts <- st_sample(nc_utm, npts)

# Select one polygon within nc data
nc_utm_select <- nc_utm[1, ]

# Measure distance from seine points to seine
dist <- distance_to(nc_utm_pts, nc_utm_select)

# or add to seine points
nc_utm_pts$dist <- dist

head(dist, 30)
#>  [1] 282251.54 160924.18 236865.67 173786.57 133998.51 302243.92  44247.78
#>  [8] 117397.50 289224.18  51107.04   1614.21 390318.62 270447.06 162955.09
#> [15]  69042.88 151970.42 101681.30 156088.04 143254.13 171012.54 267726.06
#> [22] 492495.71 296724.72 361597.27 117098.03 184987.01 188478.68 351485.85
#> [29] 372504.21  45559.20
hist(dist)

distance_raster

library(raster)
#> Loading required package: sp

rdist <- distance_raster(nc_utm_select, 1e4, extent = st_bbox(nc_utm))

plot(rdist)