No time to read the docs? Here’s the basics. See the Usage section in the menu for more detailed documentation of velox functionality.
VeloxRasterCreate a VeloxRaster object from a raster* object:
require(raster)
require(velox)
## Create RasterStack
set.seed(0)
ras.ls <- lapply(1:2, function(x) raster(matrix(rnorm(100), 10, 10), xmn=0, xmx=10, ymn=0, ymx=10))
stk <- stack(ras.ls)
## Cast as VeloxRaster
vx <- velox(stk)
Or load a VeloxRaster object directly from disk:
## Temporary path
temp.path <- file.path(tempdir(), 'test.tif')
## Save as GeoTiff
vx$write(path = temp.path)
## Read from disk
vx <- velox(temp.path)
## Clean up
unlink(temp.path)
Cast a VeloxRaster object as a RasterStack:
## As RasterStack
stk <- vx$as.RasterStack()
VeloxRasterCrop a VeloxRaster; note that VeloxRaster objects are mutable:
## Copy the VeloxRaster and check its extent
vx_copy <- vx$copy()
as.vector(vx_copy$extent)
## [1]  0 10  0 10
## Crop and check extent again
vx_copy$crop(c(0,5,0,5)) # Crop by extent; format c(xmin, xmax, ymin, ymax)
as.vector(vx_copy$extent)
## [1] 0 5 0 5
Aggregate a VeloxRaster:
## Copy the VeloxRaster and aggregate by factor 2
vx_copy <- vx$copy()
vx_copy$aggregate(factor = 2, aggtype = 'sum')
Apply a moving window filter:
## Copy the VeloxRaster and apply a moving window filter to the first raster band
vx_copy <- vx$copy()
vx_copy$meanFocal(weights = matrix(1, 3, 3), bands = 1)
Extract raster values given a set of polygons:
## Make SpatialPolygonsDataFrame
library(sp)
library(rgeos)
set.seed(0)
extent <- as.vector(vx$extent)
coords <- cbind(runif(5, extent[1], extent[2]), runif(10, extent[3], extent[4]))
sp <- SpatialPoints(coords)
spol <- gBuffer(sp, width=2, byid=TRUE)
spdf <- SpatialPolygonsDataFrame(spol, data.frame(id=1:length(spol)), FALSE)
## Extract values and calculate mean
ex.mat <- vx$extract(spdf, fun=mean)
print(ex.mat)
##           [,1]        [,2]
## 1   0.39934581 -0.23877402
## 2   0.01910321  0.02676854
## 3   0.19075114  0.52410344
## 4  -0.29341496  0.35725346
## 5  -0.05015737  0.27699692
## 6   0.24485156 -0.33300533
## 7   0.08754135 -0.25519960
## 8  -0.20048248 -0.51151030
## 9  -0.29904770  0.24206046
## 10  0.30119502  0.06412411
## Also works with sf objects
library(sf)
poly.sf <- st_as_sf(spdf)
ex.mat2 <- vx$extract(poly.sf, fun=mean)
all(ex.mat == ex.mat2)
## [1] TRUE
Rasterize some polygons:
## Copy VeloxRaster
vx_copy <- vx$copy() 
## Rasterize polygons using "id" column
vx_copy$rasterize(spdf, field="id", band=1, background = 0)
Transform the content of a raster into a matrix of ‘flat’ patches:
## Get flattened raster image
flat.mat <- vx$im2col(wrow = 3, wcol = 3, band = 1, rowframe = 0, colframe = 0)
print(dim(flat.mat))
## [1] 16  9
## Replace 'center' pixels with zeros
flat.mat[,5] <- 0
## Copy flattened image to 2nd band of raster
vx$col2im(mat = flat.mat, wrow = 3, wcol = 3, band = 2, rowframe = 0, colframe = 0)
Get help for e.g. the extract method:
?VeloxRaster_extract