我有大量的栅格(ASCII 文件),我想: 1. 从 Lamberts Equal area 重新投影到 WGS 84 2. 将生成的 WGS 84 栅格裁剪到一定程度 3. 将生成的栅格写入目录
我知道堆叠栅格和重新投影会遇到内存问题。因此,我尝试了一个 for 循环,该循环遍历每个光栅,重新投影作物并将其保存到输出目录。即便如此,我也遇到了内存问题。如果我只是裁剪和掩盖栅格,代码工作得非常快,当我想重新投影时会出现问题。这是我的代码
library(raster)
library(doParallel)
library(rgdal)
#Define how many cores you want to use
UseCores <- detectCores()-1
#Register CoreCluster
cl <- makeCluster (UseCores)
registerDoParallel (cl)
# Start the clock!
ptm <- proc.time ()
# Reading the shapefile (mask to crop later)
Maskshp <- readOGR("G:/PhD BOKU/DATA/GIS Data/austria","AUT0")
# Name the output path and creat a directory to store the final results
outpath <- "C:/Users/chakraborty/Desktop/cropdata/"
dir.create(outpath)
# Reading the raster to crop
setwd("C:/Users/chakraborty/Desktop/MPI_rcp85_2080s_Bioclimatic")
files <- list.files(pattern=".tif")
# add the output directory
outfiles <- paste0(outpath, files)
for(i in 1:length(files)) {
r <-raster(files[i])
projection(r)<- CRS("+proj=aea +lat_1=43 +lat_2=62 +lat_0=30 +lon_0=15 +x_0=0 +y_0=0 +ellps=intl
+units=m +no_defs")
newproj <- CRS("+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs")
rWGS <- projectRaster(r, crs= newproj,res= 0.008333334)
rc <- crop(rWGS, Maskshp)
rc1 <- mask(rc,Maskshp)
rc1 <- writeRaster(rc1, outfiles[i],format="GTiff")
}
#end cluster
stopCluster (cl)
# Stop the clock
proc.time() -ptm
# Takes 102 seconds to crop and write 20 rasters from Europe to Austrian Extent