通过R中的空间多边形数据框对象列表提取光栅

通过R中的空间多边形数据框对象列表提取光栅,r,polygon,spatial,raster,R,Polygon,Spatial,Raster,我试图从一个大文件中提取存储在列表中的R中的各种空间多边形数据帧(SPDF)对象的汇总光栅单元值,然后将提取的值添加到SPDF对象属性表中。我想重复这个过程,但不知道怎么做。我已经找到了一个有效的解决方案,用于存储在单个SPDF对象中的多个多边形(请参见:),但不知道如何将裁剪遮罩提取过程应用于SPDF对象列表,每个对象包含多个多边形。以下是一个可复制的示例: library(maptools) ## For wrld_simpl library(raster) ## Example Spa

我试图从一个大文件中提取存储在列表中的R中的各种
空间多边形数据帧
(SPDF)对象的汇总光栅单元值,然后将提取的值添加到SPDF对象属性表中。我想重复这个过程,但不知道怎么做。我已经找到了一个有效的解决方案,用于存储在单个SPDF对象中的多个多边形(请参见:),但不知道如何将
裁剪
遮罩
提取
过程应用于SPDF对象列表,每个对象包含多个多边形。以下是一个可复制的示例:

library(maptools)  ## For wrld_simpl
library(raster)

## Example SpatialPolygonsDataFrame
data(wrld_simpl) #polygon of world countries
bound <- wrld_simpl[1:25,] #country subset 1  
bound2 <- wrld_simpl[26:36,] #subset 2

## Example RasterLayer
c <- raster(nrow=2e3, ncol=2e3, crs=proj4string(wrld_simpl), xmn=-180, 
xmx=180, ymn=-90, ymx=90)
c[] <- 1:length(c)

#plot, so you can see it
plot(c)    
plot(bound, add=TRUE) 
plot(bound2, add=TRUE, col=3) 

#make list of two SPDF objects
boundl<-list()
boundl[[1]]<-bound1
boundl[[2]]<-bound2

#confirm creation of SPDF list
boundl
当我尝试隔离SPDF列表的掩码函数(
raster::mask
)时,它返回一个错误:

maskl <- lapply(boundl, mask, c) 
#Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘mask’ for signature ‘"SpatialPolygonsDataFrame", "RasterLayer"’

maskl
mask
extract
>将提取的值添加到SPDF属性表中。我对R真的很陌生,不知道从这里该去哪里。请帮忙

一种方法是采用有效的方法,简单地将所需的“裁剪->遮罩->提取->添加”放入for循环:

for(i in seq_along(boundl)) {
    clip1 <- crop(c, extent(boundl[[i]])) 
    clip2 <- mask(clip1, boundl[[i]])
    extract_clip <- extract(clip2, boundl[[i]], fun=sum)  
    boundl[[i]]@data["newcolumn"] <- extract_clip
}
将函数
crop()
应用于列表的每个元素
boundl
。执行的操作是

tmp <- crop(boundl[[1]], c)
## test if equal to first element
all.equal(cropl[[1]], tmp) 
[1] TRUE

tmp一种方法是采用有效的方法,只需将所需的“裁剪->遮罩->提取->添加”放入for循环:

for(i in seq_along(boundl)) {
    clip1 <- crop(c, extent(boundl[[i]])) 
    clip2 <- mask(clip1, boundl[[i]])
    extract_clip <- extract(clip2, boundl[[i]], fun=sum)  
    boundl[[i]]@data["newcolumn"] <- extract_clip
}
将函数
crop()
应用于列表的每个元素
boundl
。执行的操作是

tmp <- crop(boundl[[1]], c)
## test if equal to first element
all.equal(cropl[[1]], tmp) 
[1] TRUE

tmp
bound1
未定义。
bound1
未定义。
tmp <- crop(boundl[[1]], c)
## test if equal to first element
all.equal(cropl[[1]], tmp) 
[1] TRUE
cropl <- lapply(boundl, function(x, c) crop(c, extent(x)), c=c)
## test if the first element is as expected
all.equal(cropl[[1]], crop(c, extent(boundl[[1]]))) 
[1] TRUE