栅格化RasterBrick对象

时间:2018-05-28 14:17:10

标签: r r-raster

我正在尝试通过rasterize对象SpatialPolygon RasterBrick个对象。 raster::rasterize函数的文档明确说明这是允许的。这就是我正在做的事情

# load the raster package
library("raster")

# create a raster brick object using the example from the brick function documentation
b <- brick(system.file("external/rlogo.grd", package="raster"))

# create a SpatialPolygon object using the example from the function documentation
Sr1 = Polygon(10*cbind(c(2,4,4,1,2),10*c(2,3,5,4,2)))
Sr2 = Polygon(10*cbind(c(5,4,2,5),10*c(2,3,2,2)))
Sr3 = Polygon(10*cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(10*cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

# crop
clip1 = crop(b, extent(SpP))

# rasterize returns an error, but documentation says it should return a RasterBrick object
clip2 = rasterize(SpP, b, mask = T)
Error in v[, r] <- rrv : 
     number of items to replace is not a multiple of replacement length

# however, if I used only one layer, all would be fine
clip2 = rasterize(SpP, b[[1]], mask = T)

当然,我可以遍布砖块的层,但据我所知,这会破坏砖块的目的。

我想使用clip2然后获取图层中像素值的直方图。

vals = getValues(clip2)

任何人都可以告诉我为什么我会收到此错误,以及如何有效地解决这个问题?

1 个答案:

答案 0 :(得分:1)

根据@ clbieganek上面的评论,这确实是当前CRAN版本2.6-7中的一个错误。作者Michael Sumner说这应该在下一个版本中修复,请参阅https://www.mail-archive.com/r-help@r-project.org/msg249242.html