2
votes

I'm using the new velox extract function to speed up raster extraction by shapefiles.

The old raster package's extract function by default returned a list of cell values, for example when you use the below format:

val.list <- raster::extract(raster, shapefile)

The new velox package requires a fun= argument and I can't for the life of me get it to return the values:

vx.raster <- velox(raster)
vx.vals <- vx.raster$extract(shapefile, fun=??????)

I have tried:
fun=values (returns error Error during wrapup: unable to find an inherited method for function 'values' for signature 'numeric'
fun=function(x){values(x)} (same error as above)

I get fun=sum, fun=mean to work just fine. Whats up with values? Am I just missing something obvious about a numeric vector and returning a values list (which I feel is the most likely case)?

Thank you!

2
From ?extract: "fun: (…) The function should take a single numeric vector as argument and return a single value". From ?value: "Value: vector or matrix of raster values". Could that be the issue? - AkselA
How about fun=function(x){x} or fun=as.numeric? - Kristoffer Winther Balling
Neither fun=function(x){x} nor fun=as.numeric works. It does have a different error though, "Error in out[p, k] <- fun(valmat[, k]) ". Seems to me that the numeric vector output of velox extract is a single vector of all the values, and it may be trying to merge this onto the velox raster matrix of only the number of polygons in the shapefile. Thus the values would be far longer of a vector than a column of the velox matrix which may cause this error. I'm unsure of how to force a new vector of a different length since the extract documentation for velox only has the $ method of extracting. - Neal Barsch
If you look at the code of the function velox_extract: github.com/cran/velox/blob/master/R/velox_extract.R It seems that the output is a dataframe with only one value for each polygon (p) and one value for each column of the polygon associated dataframe. The case for a list of values has apparently not been implemented here. - Sébastien Rochette
I came to the same conclusion. Thanks StatnMap. I also emailed the developer of the package to see if there was any values solution, but agreed, looking at that code it seems as though the output is a single value per poly... too bad since raster::extract is incredibly slow with large sets of polygons. Using a foreach loop with a crop and getvalues as here gis.stackexchange.com/questions/130522/… does help a bit, but there are issues if you have very small crop sizes relative to pixels in the polys. - Neal Barsch

2 Answers

1
votes

The development version of velox (on github) now allows returning 'raw' raster values from a VeloxRaster_extract query. Just set the fun argument to NULL.

Here's an example:

library(devtools)
install_github('hunzikp/velox')
library(velox)

## Make VeloxRaster with two bands
set.seed(0)
mat1 <- matrix(rnorm(100), 10, 10)
mat2 <- matrix(rnorm(100), 10, 10)
vx <- velox(list(mat1, mat2), extent=c(0,1,0,1), res=c(0.1,0.1),
        crs="+proj=longlat +datum=WGS84 +no_defs")

## Make SpatialPolygons
library(sp)
library(rgeos)
coord <- cbind(0.5, 0.5)
spoint <- SpatialPoints(coords=coord)
spols <- gBuffer(spgeom=spoint, width=0.5)

## Extract
vx$extract(sp=spols, fun=NULL)$buffer
#             [,1]        [,2]
# [1,]  1.27242932  0.04658030
# [2,]  0.41464143 -1.13038578
# [3,] -1.53995004  0.57671878
#  etc....
0
votes

Simply try this snippet

vx.raster$crop(shapefile).