【发布时间】: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