Extract in R fails for small polygons and raster

浪子不回头ぞ 提交于 2020-01-05 04:41:07

问题


This example should be reproducible, the first part (with large polygons) works, the second fails:

library(raster)

USA.altitude <- getData('alt', country='USA')
lower48 <- USA.alt[[1]]

Srs1 = Polygons(list(Polygon(cbind(c(-96,-95,-95,-96),c(40,40,41,40)))), "s1")
Srs2 = Polygons(list(Polygon(cbind(c(-97,-96,-96,-97),c(40,40,41,40)))), "s2")

spdf.large <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs1,Srs2)), 
                              data.frame( z=1:2, row.names=c("s1","s2") ) )
plot(spdf.large)
usa.average.elev<-extract(lower48, spdf.large, fun=mean)

Yay! everything seems ok, yes? Now with small polys:

Srs3 = Polygons(list(Polygon(cbind(c(-95.01,-94.99,-94.99,-95.01),c(40,40,40.01,40)))), "s3")
Srs4 = Polygons(list(Polygon(cbind(c(-94.01,-93.99,-93.99,-94.01),c(40,40,40.01,40)))), "s4")


spdf.small <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs3,Srs4)), 
                              data.frame( z=1:2, row.names=c("s3","s4") ) )
plot(spdf.small)
usa.average.elev<-extract(lower48, spdf.small, fun=mean)

Fails with error:

Error in pmax(ceiling(10/dim(r)[1:2]), c(1, 1)) : object 'r' not found

来源:https://stackoverflow.com/questions/20915114/extract-in-r-fails-for-small-polygons-and-raster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!