distance_to: Distance to In distanceto: Calculate Distance to Features

Description

Measures the distance from points x to features in layer y.

Usage

 1 distance_to(x, y, measure = NULL)

Arguments

 x points to measure distances from, to layer y. Expecting an sf point compatible with sf::st_coordinates such as an sf, sfc or sfg object with geometry type 'POINT' or 'MULTIPOINT'. CRS of x should match CRS of y. y feature layer to measure distance to. Expecting an sf point, line or polygon compatible with sf::st_coordinates such as an sf, sfc or sfg object. CRS of y should match CRS of x. measure method used to measure geographic distances between longlat x and y objects. See geodist::geodist for more information. Ignored if CRS of x and y indicated projected coordinates.

Details

Uses the function nabor::knn to determine the distance from each point in x to the nearest feature in layer y. If the input CRS is longlat, eg. EPSG 4326, the distance is returned as measured by geodist::geodist. Otherwise, if the input CRS indicates projected coordinates, the distance returned is the euclidean distance. Both x and y are expected to be sf objects and the distances are returned as vector, easily added to input x with \$<- or other methods. If y is a 'POLYGON' or 'MULTIPOLYGON' object, the distance returned for points in x within features in y are set to 0.

Value

A vector of distances from points in x to features in layer y.

Examples

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # Load sf library(sf) # Load nc data nc <- st_read(system.file("shapes/sids.shp", package="spData")) st_crs(nc) <- "+proj=longlat +datum=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, printing result distance_to(ncpts, ncsub, measure = 'geodesic') # or add to ncpts ncpts\$dist <- distance_to(ncpts, ncsub, measure = 'geodesic')

distanceto documentation built on Nov. 12, 2021, 1:07 a.m.