Does anyone know an equivalent package for the velox
package for fast zonal statistics in R? This package was by far the best performing tool when working with large raster objects but has been recently removed from CRAN...
-
You can still install it from the source code on github - or at least try, assuming its not come off CRAN because it doesn't work.– SpacedmanCommented Jun 1, 2020 at 7:54
2 Answers
I have moved to using the exact_extract
function in the exactextractr
package. One nice thing is that it returns all cell intersections with the fractional proportion of each cells intersection. You could use the proportion of intersection column as weights or to filter out small intersections.
Also, there are numerous statistics available, including some weighted ones, that are part of the C++ code, so very fast. The package takes sf
class polygons and raster
class raster data.
You can use the mmand
package
Example:
#random sampling points:
pts <- readOGR(
dsn = ".",
layer = "random_points",
encoding = "utf8")
pts$id <- as.numeric(pts$id)
pts <- as.data.frame(pts)
library(mmand)
library(raster)
r1 = raster("G:/romain/imperviousness_project/SWI.tif")
#Let's rasterize the points based on their x y coordinates:
z <- rasterize(pts[,2:3], r1, field = pts$id)
#I want to get statistic from a zone of 3*3 pixels around my point (in my example 1 pixel = 5 meters so the total area is 15m2):
kern <- shapeKernel(c(3,3), type="box")
z[,] <- dilate(as.matrix(z), kern)
plot(z)
zonal(r1, z, fun = "mean")
It is also mentionned here: https://stackoverflow.com/questions/50398473/how-to-perform-focal-operation-mean-on-raster-using-3x3-window-in-r-i-have-la
-
1Do you understand what the
mmand
package is actually doing here? TheshapeKernel
anddialate
functions are frommmand
and are just used in creating a 3x3 matrix around each point that is then rasterized and passed to theraster::zonal
function. In this case themmand
package has very little to do with calculating zonal statistics and is completely irrelevant outside of the point example (ie., cannot be expanded to polygons). This also creates a huge amount of unnecessary overhead and would be massively slower than just usingraster::extract
. Commented Jun 1, 2020 at 14:55 -
Ok sorry I didn't know, I only used it once for points ! My bad ! Commented Jun 1, 2020 at 15:15
-