2017-04-16 65 views
2

我正在使用新的velox提取函数来加速shapefile的栅格提取。返回值的函数(velox栅格)

老光栅包的默认提取函数返回的单元格值的列表,例如,当您使用以下格式:

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

新VELOX包需要一个有趣=参数,我不能为我的生活得到它返回的值:

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) 

我得到乐趣=总和,乐趣=意味着工作就好。什么与价值?我只是错过了一些关于数值向量的显而易见的东西,并返回了一个值列表(我认为这是最可能的情况)?

谢谢!

+0

从'extract':“有趣:(...)函数应当采取单一数值向量作为参数,并返回一个值”。来自'?value':“值:栅格值的向量或矩阵”。这可能是问题吗? – AkselA

+0

'fun = function(x){x}'或'fun = as.numeric'怎么样? –

+0

既不fun = function(x){x}也不fun = as.numeric作品。它确实有一个不同的错误,“out [p,k] < - fun(valmat [,k])中的错误”)。在我看来,velox提取的数值向量输出是所有值的单个向量,它可能试图将它合并到只有shapefile中的多边形数的velox栅格矩阵上。因此,这些值将比可能导致这个误差的velox矩阵的列向量长得多。我不确定如何强制一个不同长度的新向量,因为velox的提取文档只有$提取方法。 –

回答

0

简单地尝试这个片段

vx.raster$crop(shapefile). 
1

VELOX的开发版本(在github)现在允许从VeloxRaster_extract查询返回“原始”栅格值。只需将fun参数设置为NULL即可。

下面是一个例子:

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....