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.
rasterDistance(x, y, reference = NULL, scale = FALSE)
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 |
a distance raster of class rasterLayer
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.
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.
Jeffrey S. Evans <jeffrey_evans@tnc.org>
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)