问题
Does anyone know why rasterize
would return the following error when used on polygons:
Error in rv[[ii]] : subscript out of bounds
Using other common R functions, such as mean
, also give the same error, but rasterize
options, such as 'last'
, work fine. I haven't encountered this problem in the past on previous versions of raster
(currently using raster_2.3-0). I've attached code I grabbed from an example in ?rasterize
:
library(raster)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- Polygons(list(Polygon(p1), Polygon(hole, hole=TRUE)), 1)
p2 <- Polygons(list(Polygon(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)))), 2)
p3 <- Polygons(list(Polygon(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0)))), 3)
pols <- SpatialPolygons( list( p1 , p2, p3) )
r <- raster(ncol=90, nrow=45)
r <- rasterize(pols, r, fun='sum') # works
r <- rasterize(pols, r, fun=sum) # returns error: "Error in rv[[ii]] : subscript out of bounds"
回答1:
The package maintainer Robert Hijmans has fixed this issue; for the time being you can install the updated package from R-forge (install.packages("raster", repos="http://R-Forge.R-project.org")) and the updated version should be on CRAN next week. For the record, I just emailed Hijmans yesterday and he was very responsive in getting this addressed.
来源:https://stackoverflow.com/questions/25994474/r-error-in-rasterize-when-used-on-polygons