Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

rasterDistance

Raster Distance


Description

Calculates the Euclidean distance between a set of points and the cells in a raster. This is a drop-in replacement for the raster distanceFromPoints function using the RANN algorithm for calculating distance, resulting in a large improvement in processing speed.

Usage

rasterDistance(x, y, reference = NULL, scale = FALSE)

Arguments

x

rasterLayer, sp SpatialPoints or sf POINTS object

y

sp SpatialPoints or sf POINTS object

reference

A raster to use as a reference if x is points object

scale

(FALSE/TRUE) Perform a row standardization on results

Value

a distance raster of class rasterLayer

Note

This replicates the raster distanceFromPoints function but uses the Arya & Mount Approximate Near Neighbor (ANN) C++ library for calculating distances. Where this results in a notable increase in performance it is not memory safe, needing to read in the entire raster and does not use the GeographicLib (Karney, 2013) spheroid distance method for geographic data.

Author(s)

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

library(raster)
r <- raster(ncol=100,nrow=100)
  r[] <- sample(c(0,1), ncell(r), replace = TRUE)
 
majority <- function(x){
 m <- table(x)
 names(m)[which.max(m)][1]
}
r <- focal(r, matrix(1,11,11, byrow=TRUE), majority) 
 
 pts <- rasterToPoints(r, spatial=TRUE)
   cls <- pts[pts$layer == "1",] 
 d <- rasterDistance(pts, cls, reference = r, scale=TRUE)
   dev.new(height=8,width=11)
     plot(d)
       points(cls,pch=19,cex=0.5)

spatialEco

Spatial Analysis and Modelling Utilities

v1.3-6
GPL-3
Authors
Jeffrey S. Evans [aut, cre], Melanie A. Murphy [ctb], Karthik Ram [ctb]
Initial release
2021-03-24

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.