如何从 R 中的 shapefile 中的裁剪和蒙版(剪切)创建多个栅格的列表

How to create a list of several rasters from a crop and mask (cut) from shapefile in R

我每年都有 14 个土地使用光栅文件。我想使用我感兴趣的 shapefile 区域裁剪这些栅格。使用这些切割栅格,我想创建一个列表,其中每个元素代表 shapefile 切割的每一年。

我尝试使用 forcropmask 命令以一种非常简单和快速的方式,但没有成功。

我的数据示例: 光栅和形状文件:https://drive.google.com/file/d/1IOGWZ3_ckKj3UoZVd7ikR5l9n04xd6He/view?usp=sharing

我的代码

    library(raster)
    library(rgdal)
    library(sf)
     
    setwd('dir')
    
    raster_solo_2005<-raster('utm-50-2005.tif')
    
    raster_solo_2006<-raster('utm-50-2006.tif')
    
    raster_solo_2007<-raster('utm-50-2007.tif')
    
    raster_solo_2008<-raster('utm-50-2008.tif')
    
    raster_solo_2009<-raster('utm-50-2009.tif')
    
    raster_solo_2010<-raster('utm-50-2010.tif')
    
    raster_solo_2011<-raster('utm-50-2011.tif')
    
    raster_solo_2012<-raster('utm-50-2012.tif')
    
    raster_solo_2013<-raster('utm-50-2013.tif')
    
    raster_solo_2014<-raster('utm-50-2014.tif')
    
    raster_solo_2015<-raster('utm-50-2015.tif')
    
    raster_solo_2016<-raster('utm-50-2016.tif')
    
    raster_solo_2017<-raster('utm-50-2017.tif')
    
    raster_solo_2018<-raster('utm-50-2018.tif')
   
 #list raster
    list_raster<-as.list(raster_solo_2005, raster_solo_2006, raster_solo_2007, raster_solo_2008, raster_solo_2009, raster_solo_2010, raster_solo_2011, raster_solo_2012, raster_solo_2013, raster_solo_2014, raster_solo_2015, raster_solo_2016, raster_solo_2017, raster_solo_2018)
      
    #shapefile
    shp_area<-sf::st_read('500m_utm.shp')
    
    #creat list empity
    list_raster_cut<-list()
      
    #loop operation
    for(i in 1:10){
      
      
      # crop e mask
      list_raster_cut[[i]] <- list_raster %>% 
        raster::crop(shp_area)%>%
        raster::mask(shp_area)
      
    }

更新:退出错误

Error in h(simpleError(msg, call)) : error evaluating argument 'x' when selecting method for function 'mask': 'unable to find an inherited method for function 'crop' for signature '"list" , "sf"''

问题是您还需要在 for 循环中对 list_raster 对象进行子集化。

list_raster_cut[[i]] <- list_raster[[i]] %>% 
        raster::crop(shp_area)%>%
        raster::mask(shp_area)