【问题标题】:How to cut rasters with a shapefile object in loop by folder in R?如何在R中按文件夹循环切割带有shapefile对象的栅格?
【发布时间】:2020-10-06 17:47:58
【问题描述】:

伙计们。你好吗?

很高兴见到你。

我正在尝试从 shapefile 对象中剪切栅格 (tif)

我的数据结构如下:

(种子文件夹)

物种 1

物种 2

物种 3

我有来自不同物种的不同文件夹。在每个文件夹中,我都有不同的 tiff 模型和不同的 shapefile。我需要的是一个脚本,它使用同一文件夹中的 shapefile 文件剪切该物种文件夹内的所有栅格。然后去下一个文件夹做同样的事情。我不需要剪切不在同一个文件夹中的文件。我正在尝试下一个代码但没有成功。我是 R 中的一个相对较新的用户,所以我将不胜感激。谢谢你们。祝你有美好的一天。

setwd ("H:/Tesis_maestria/1_Eliposides/1_Cut_models")
library(purrr)
library(kuenm)
library(raster)
library(maptools)
library(rgeos)
library(rgdal)
rm(list=ls())

path_general <- "H:/Tesis_maestria/1_Eliposides/1_Cut_models"


dirs_especies_path <- list.dirs(full.names = T,
                                recursive = F)

datum <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")

x=1
y=1
resultados_all <- seq_along(dirs_especies_path) %>% purrr::map_df(function(x){
   sp_mods <- list.files(dirs_especies_path[x], pattern = ".tif", full.names = TRUE)
   M <-list.files(dirs_especies_path[x], pattern=".shp", full.names = TRUE)
   r1 <- raster(sp_mods[y])
   cut_all <- seq_along(sp_mods) %>%  purrr::map_df(function(y){
   cor <-crop(r1,M)
   mas<-mask(cor,M)
   return(cut_all)
    })
     nwdf <- data.frame(cut_all,
                     sp_name=dirs_especies_names[x])
                         setwd("H:/Tesis_maestria/1_Eliposides/1_Cut_models/cut")
    writeRaster(cut_all,filename=paste(M[[i]]),bylayer=T,suffix=names(stac),format="GTiff")
  return(nwdf)
})
print(resultados_all)

【问题讨论】:

    标签: r loops raster shapefile cut


    【解决方案1】:

    经过一天的工作,我可以成功运行此代码。如下。我希望这可以帮助有同样问题的人。问候。

    rm(list=ls())
    library(rgdal)
    library(raster)
    library(maptools)
    library(rgeos)
    library(purrr)
    rm(list=ls())
    
    setwd("H:/Tesis_maestria/1_Eliposides/1_Cut_models1")
    datum <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
    
    path_general <- "H:/Tesis_maestria/1_Eliposides/1_Cut_models1"
    
    dirs_especies_path <- list.dirs(full.names = T,
                                    recursive = F)
                                    
    dirs_especies_names <- list.dirs(full.names = F,
                                     recursive = F)
                                     
    #paste(path_general,dirs_especies_names,sep="")
    
    ##Presente##
    for (i in 1:length(dirs_especies_path)){
        M <- list.files(dirs_especies_path[i],pattern = "csv.shp$",full.names = TRUE)
        mods <- list.files(dirs_especies_path[i],pattern = ".tif$",full.names = TRUE)
        stack <- raster::stack(mods)
        Ms <- shapefile(M)
        Ms@proj4string <- datum
        cor <- crop(stack,Ms)
        mas <- mask(cor,Ms)
        writeRaster(mas,filename=paste(M),bylayer=T,suffix=names(stack),format="GTiff")
        next}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多