简化R脚本以加载多个SpatVectors、SpatRaster,然后使用SpatVectors对空间栅格进行掩膜

5f0d552i  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个R脚本,通过使用相应的空间向量来掩蔽几个光栅,因为光栅有一个5公里的缓冲区,需要在我可以将它们合并在一起之前进行裁剪。我从文件夹中手动一个接一个地加载数据,这些文件夹看起来有很多重复。
我想知道是否有可能简化我的脚本,以避免加载日期一个接一个,做每个过程一个接一个?
我有很多光栅文件(超过100个. tif文件)需要通过相应的空间矢量进行遮罩,并合并为最终的大Map。

library(terra)

setwd("I:/France_grid_1e5")

##Load data
A10 <- vect("./fr_grid_A10.shp")
A100 <- vect("./fr_grid_A100.shp")
A101 <- vect("./fr_grid_A101.shp")
##more than 80 .shp files need to be loaded here and follow the same procedures below.####

country_shp <- vect("I:/VoCC/Data/France_shapefiles/France.shp")

#A10
raster10 <- rast("I:/VoCC/Data/Output/France_1e5grid_5kmbuffer/10FVoCC_fr_Euclidean_25m_ssp370_1e5grid_5kmBuffer.tif")
plot(raster10)
raster10
plot(A10, add = TRUE)
raster10 <- mask(raster10, A10)
writeRaster(raster10, filename = "I:/VoCC/Data/Output/RemoveBuffer_fr/10FVoCC_fr_Euclidean_25m_ssp370_1e5grid.tif", overwrite=TRUE)

#A100
raster100 <- rast("I:/VoCC/Data/Output/France_1e5grid_5kmbuffer/100FVoCC_fr_Euclidean_25m_ssp370_1e5grid_5kmBuffer.tif")
plot(raster100)
raster100
plot(A100, add = TRUE)
raster100 <- mask(raster100, A100)
writeRaster(raster100, filename = "I:/VoCC/Data/Output/RemoveBuffer_fr/100FVoCC_fr_Euclidean_25m_ssp370_1e5grid.tif", overwrite=TRUE)

#A101
raster101 <- rast("I:/VoCC/Data/Output/France_1e5grid_5kmbuffer/101FVoCC_fr_Euclidean_25m_ssp370_1e5grid_5kmBuffer.tif")
plot(raster101)
raster101
plot(A101, add = TRUE)
raster101 <- mask(raster101, A101)
writeRaster(raster101, filename = "I:/VoCC/Data/Output/RemoveBuffer_fr/101FVoCC_fr_Euclidean_25m_ssp370_1e5grid.tif", overwrite=TRUE)

####Merge the raster tiles####

raster_files <- list.files("I:/VoCC/Data/Output/France_1e5grid_5kmbuffer/", pattern = ".tif$", full.names = TRUE)

rasters <- lapply(raster_files, terra::rast)

raster_sprc <- terra::sprc(rasters)

#raster_mosaic<- terra::mosaic(raster_sprc) 

raster_merge <- terra::merge(raster_sprc)
##In areas where the SpatRasters overlap, the values of the SpatRaster that is 
##first in the sequence of arguments (or in the SpatRasterCollection) will be retained 
plot(raster_merge)
raster_merge
final_raster <- mask(raster_merge, country_shp)
plot(final_raster)

writeRaster(final_raster, filename = "I:/VoCC/Data/Output/RemoveBuffer_BE/Final_FVoCC_BE_Euclidean_25m_ssp370_1e5grid.tif", overwrite=TRUE)

字符串
有没有人知道如何简化这些R脚本?
我想得到一些关于如何避免手动逐行加载数据的想法。分配给我的光栅图块的编号不是按顺序排列的,您可以看到光栅图块的编号可以是“A10”,然后跳到“A100”,由于光栅图块没有数据值,因此删除了一些编号。
根据Till的回答,我修改了我的代码,但在读取数据时遇到了一些问题。在我的输出中,'vect_list'和'rast_list'都是空的,显示为'List of 0'。有人知道为什么会这样吗?

library(purrr)
library(terra)

# read data ---------------------------------------------------------------

# these patterns will be used to select files in your directory you are
# looking to use. the patterns might not be specific enough to
# avoid overselecting in your folders

vect_files_to_keep <- c("A10", "A100", "A101")
rast_files_to_keep <- c("/10", "/100", "/101")

vect_list <-
  dir("I:\\France_grid_1e5", pattern = "\\.shp") |>
  keep(\(x) x %in% vect_files_to_keep) |>
  map(vect)

rast_list <-
  dir("I:/VoCC/Data/Output/France_1e5grid_5kmbuffer/", pattern = "\\.tif") |>
  keep(\(x) x %in% rast_files_to_keep) |>
  map(rast)


我修改了部分脚本,现在它可以为我工作。下面是我的代码的一小部分:

library(terra)

setwd("I:/France_grid_1e5/")
# Define the filenames to keep
vect_files_to_keep <- c("A10.shp", "A100.shp", "A101.shp")

# Get the list of shapefiles
shapefile_list <- dir("I:/France_grid_1e5/", pattern = "\\.shp")

# Filter the list of shapefiles based on vect_files_to_keep
filtered_shapefile_list <- shapefile_list[grepl(paste(vect_files_to_keep, collapse = "|"), shapefile_list)]

# Read the shapefiles using the vect function from terra
vect_list <- lapply(filtered_shapefile_list, vect)

o2gm4chl

o2gm4chl1#

您可以序列化脚本执行的重复任务。purrr包非常适合这一点。
这里有一些代码,它应该给予你一个起点。我不知道你的确切文件夹结构,所以你可能需要做一些调整。

library(purrr)
library(terra)

# read data ---------------------------------------------------------------

# these patterns will be used to select files in your directory you are
# looking to use. the patterns might not be specific enough to
# avoid overselecting in your folders

vect_files_to_keep <- c("A10", "A100", "A101")
rast_files_to_keep <- c("/10", "/100", "/101")

vect_list <-
  dir("path/to/data", pattern = "\\.shp") |>
  keep(\(x) x %in% files_to_keep) |>
  map(vect)

rast_list <-
  dir("path/to/data", pattern = "\\.tif") |>
  keep(\(x) x %in% rast_files_to_keep) |>
  map(rast)

# plot --------------------------------------------------------------------

map(vect_list, plot)
map(rast_list, plot)

# mask --------------------------------------------------------------------

masked_list <-
  map2(vect_list,
       rast_list,
       mask)

# merge -------------------------------------------------------------------

merged <-
  masked_list |>
  raster_sprce() |>
  terra::merge()

plot(merged)

# final -------------------------------------------------------------------

country_shp <- vect("path/to/data/France.shp")
final_raster <- mask(merged, country_shp)
plot(final_raster)

字符串

相关问题