简体   繁体   English

如何在R中使用terra包编写光栅旁路?

[英]How to write raster bylayer using terra package in R?

I want to write rasters by layer using terra package.我想使用terra包逐层编写栅格。 I am using the following code我正在使用以下代码

library(terra)

# first create a raster
r1 <- r2 <- r3 <- rast(nrow=10, ncol=10)
# Assign random cell values
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- c(r1, r2, r3)
s
plot(s)

writeRaster(s, names(s), overwrite=TRUE)

It gives me following error它给了我以下错误

Error: [writeRaster] cannot open file: C:/Users/nn/Desktop/lyr.1 In addition: Warning message: C:/Users/nn/Desktop/lyr.1: No such file or directory (GDAL error 4)错误:[writeRaster] 无法打开文件:C:/Users/nn/Desktop/lyr.1 另外:警告消息:C:/Users/nn/Desktop/lyr.1:没有这样的文件或目录(GDAL 错误 4)

I want to have the same output available in raster package using the following function我想使用以下函数在raster包中获得相同的输出

raster::writeRaster(s, names(s), bylayer=TRUE, format='GTiff', overwrite=TRUE)

You have to do a little more work你必须做更多的工作

dir.create("test")
setwd("test")
f <- paste0("test", 1:nlyr(s), ".tif")
r <- writeRaster(s, f, overwrite=TRUE)
list.files()
# [1] "test1.tif" "test2.tif" "test3.tif"

r
#class       : SpatRaster 
#dimensions  : 10, 10, 3  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#sources     : test1.tif  
#              test2.tif  
#              test3.tif  
#names       :      lyr.1,      lyr.1,      lyr.1 
#min values  : 0.02075680, 0.01058152, 0.02179740 
#max values  :  0.9874134,  0.9990475,  0.9883418 

This also works:这也有效:

names(s) <- c("a", "b", "c")
x <- writeRaster(s, names(s), overwrite=TRUE, filetype="GTiff")

But note the filenames do not get the tif extension但请注意文件名没有获得 tif 扩展名

sources(x)
#    source nlyr
#1 ./test/a    1
#2 ./test/b    1
#3 ./test/c    1

So I would do所以我会做

z <- writeRaster(s, paste0(names(s), ".tif"), overwrite=TRUE)

#class       : SpatRaster 
#dimensions  : 10, 10, 3  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#sources     : a.tif  
#              b.tif  
#              c.tif  
#names       :          a,          b,          c 
#min values  : 0.02075680, 0.01058152, 0.02179740 
#max values  :  0.9874134,  0.9990475,  0.9883418 

The error messages have now been improved (see this issue )错误消息现已得到改进(请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 terra r 包为栅格分配正确的投影和范围? - How to assign correct projection and extent to a raster using terra r package? 如何使用焦点(R raster/terra)通过 IDW 填补 NA 空白? - How to fill NA gaps by IDW using focal (R raster/terra)? 如何使用“terra”通过 R 中的条件语句对栅格进行子集化? - How can I subset a raster by conditional statement in R using `terra`? 如何使用 terra 包将 R 中的多层栅格(457 个波段)与一层栅格(值 0 和 1 )相乘? - How to multiply a multi-layer raster (457 bands) with a one-layer raster (values of 0 and 1 ) in R with terra package? 如何使用 terra 将光栅写入折旧的 crs? - How to write raster to depreciated crs with terra? 如何在R栅格包中编写具有RAT因子的栅格 - How to write a raster with RAT factors in R raster package 如何使用 terra 包将栅格堆栈中提取的值添加到 Spatial 对象的 data.frame 中? - How to add the extracted values from raster stack to the data.frame of the Spatial object using terra package? 如何使用 terra r 包创建最大值复合? - How to create maximum value composite using terra r package? 如何使用 terra 包在 r 中制作等面积网格 - How to make an equal area grid in r using the terra package 如何在 R 中使用 terra package 对部分重叠的栅格进行重采样? - How to resample partially overlapping rasters using terra package in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM