Learn R Programming

spatialEco (version 1.3-5)

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

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

distanceFromPoints, distance

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataLab