Learn R Programming

spatialEco (version 2.0-1)

rasterDistance: Raster Distance

Description

Calculates the Euclidean distance of a defined raster class and all the other cells in a taster

Usage

rasterDistance(x, y, scale = FALSE)

Value

a distance terra SpatRast raster

Arguments

x

A terra SpatRast or sf class object

y

Value(s) in x to to calculate distance to

scale

(FALSE/TRUE) Perform a row standardization on results

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org>

References

Arya S., Mount D. M., Netanyahu N. S., Silverman R. and Wu A. Y (1998), An optimal algorithm for approximate nearest neighbor searching, Journal of the ACM, 45, 891-923.

See Also

Examples

Run this code
library(sf)
library(terra)

# read, project and subset 10 polygons
nc <- suppressWarnings(st_cast(st_read(system.file("shape/nc.shp", 
         package="sf")), "POLYGON"))
  nc <- st_transform(nc, st_crs("ESRI:102008"))
    nc.sub <- nc[sample(1:nrow(nc),10),]

# create 1000m reference raster, rasterize subset polygons
ref <- rast(ext(nc), resolution=1000)
  rnc <- mask(rasterize(vect(nc.sub), field="CNTY_ID",
              ref, background=9999), vect(nc)) 
    crs(rnc) <- "ESRI:102008"  
  
# Calculate distance to class 1 in rnc raster, plot results
ids <- nc.sub$CNTY_ID 
rd <- rasterDistance(rnc, y=ids) 
  plot(rd)
    plot( st_geometry(nc.sub), add=TRUE)

#### Benchmark rasterDistance and terra::distance
####   at res=90m the differences are quite notable
# ref <- rast(ext(nc), resolution=500)
#   rnc <- mask(rasterize(vect(nc.sub), ref, background=2),
#               vect(nc)) 
#     crs(rnc) <- "ESRI:102008" 
# system.time({ rasterDistance(rnc, y=1) })  
# system.time({ distance(rnc, target=2) }) 

Run the code above in your browser using DataLab