生锈砖的快速子集化

问题描述

我有一个多边形列表,我想用它来对 terra::rast 砖块进行子集化。为此,我将 terra::croplapply 一起使用,如下所示,但速度相当慢。是否有一种矢量化的方式来对多边形进行子集化,而不是通过多边形lapply

示例

首先,我加载库并创建一个 rast 对象。

# Load library                                                                         
library(terra)                                                                         
library(geohashTools)                                                                  
                                                                                       
# Create raster                                                                        
r <- rast(matrix(runif(360 * 2 * 180 *2),ncol = 360 * 2))                             
                                                                                       
# Set extent                                                                           
ext(r)  <- c(-180,180,-90,90)                                                       
                                                                                       
# examine raster                                                                       
r                                                                                      
## class       : SpatRaster                                                            
## dimensions  : 360,720,1  (nrow,ncol,nlyr)                                       
## resolution  : 0.5,0.5  (x,y)                                                      
## extent      : -180,90  (xmin,xmax,ymin,ymax)                          
## coord. ref. :                                                                       
## source      : memory                                                                
## name        :        lyr.1                                                          
## min value   : 7.853378e-07                                                          
## max value   :    0.9999981                                                          
        

接下来,我创建了这些对象的砖块。

# Create a brick                                                                       
b <- c(r,r,r)                                                                  
                                                                                       
## class       : SpatRaster                                                            
## dimensions  : 360,5  (nrow,ymax)                          
## coord. ref. :                                                                       
## sources     : memory                                                                
##               memory                                                                
##               memory                                                                
##               ... and 2 more source(s)                                              
## names       :        lyr.1,lyr.1,lyr.1  
## min values  : 7.853378e-07,7.853378e-07,7.853378e-07  
## max values  :    0.9999981,0.9999981,0.9999981  

在这里,我只是在创建一个空间多边形负载

# All possible coordinates                                                             
coords <- expand.grid(-180:180,-90:89)                                                
                                                                                       
# Get all unique geohashes for raster                                                  
geohashes <- unique(gh_encode(coords$Var2,coords$Var1,precision = 4L))               
                                                                                       
# Convert to spatial polygons                                                          
sp <- geohashTools::gh_to_sp(geohashes)                                                
         

最后,我遍历每个多边形并使用它来裁剪我的砖块。

# Crop raster using geohash polygon                                                    
b_cropped <- lapply(seq_along(sp),function(x) terra::crop(b,sp[x]))                  
                                                                                       

问:有没有更快的方法来完成最后一步?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)