Search code examples
rterra

Rasterize SpatVect (points) with buffer around SpatRaster


I have a SpatVect consisting of points and I want to rasterize them into a SpatRaster with a given resolution. Is there a way of specifying a function taking in the points that are within a buffer of each raster cell?

Many thanks Joao

-- Update -- Maybe a figure would help understand what I'm after with my question. The red square will have to be run over the center of each pixel to calculate some statistics using the ovelaying points. Apologies for the clumsy question, but I hope the figure is clear enough...

enter image description here


Solution

  • terra version 1.6-28 supports rasterization of points with a rectangular moving window.

    Example data

    library(terra)
    #terra 1.6.33
    r <- rast(ncol=100, nrow=100, crs="local", xmin=0, xmax=50, ymin=0, ymax=50)
    set.seed(100)
    x <- runif(50, 5, 45)
    y <- runif(50, 5, 45)
    z <- sample(50)
    v <- vect(data.frame(x,y,z), geom=c("x", "y"))
    

    Solution

    r1 <- rasterizeWin(v, r, field="z", fun="count", pars=10, win="rectangle")
    
    plot(r1)
    points(x, y)
    

    enter image description here

    You can change fun to another function that works for you, and you can change the size of the moving window with pars.

    Instead of a rectangle, you can also use a circle or an ellipse. The border of a circular window is equidistant from the center of the cells. In contrast, the border of rectangles are at a constant distance from the border of the grid cells in most directions (not at the corners). Here is an example.

    r2 <- rasterizeWin(v, r, field="z", fun="count", pars=5.25, win="circle")
    plot(r2)
    

    enter image description here

    You can also use buffers around each cell to get a window that is truly equidistant from each cell border.

    r3 <- rasterizeWin(v, r, field="z", fun=length, pars=5, win="buf")
    plot(r3)
    

    enter image description here

    In this case, because the buffer size is large relative to the cell size, the result is very similar to what you get when using a circular window. Using "circle" should be the fastest, and using "buffer" should be the slowest in most cases. The function should now in all cases be memory-safe, except, perhaps when using very large buffers (more could be done if need be).

    Version 1.6-28 is currently the development version. You can install it with install.packages('terra', repos='https://rspatial.r-universe.dev')