我尝试使用“xyz”阅读风格构建一个 2 层的 SpatRast。它使用 3 列作为 rast 函数的输入,但我收到一条包含 4 列的警告消息:
> rast(as.matrix(data.frame(x=c(1,1,2,2),y=c(1,2,1,2),z1=1:4)),type="xyz")
class : SpatRaster
dimensions : 2, 2, 1 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : 0.5, 2.5, 0.5, 2.5 (xmin, xmax, ymin, ymax)
coord. ref. :
data source : memory
names : z1
min values : 1
max values : 4
> r=rast(as.matrix(data.frame(x=c(1,1,2,2),y=c(1,2,1,2),z1=1:4,z2=5:8)),type="xyz")
Warning message:
In v[cells] <- xyz[, 3:d[2]] :
number of items to replace is not a multiple of replacement length
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
我尝试使用 getData (来自栅格包)读取 DEM 栅格,然后将 RasterLayer 转换为 SpatRaster (terra 包)。第一步成功了,但第二步失败了。
library(raster)
library(terra)
(alt <- getData('alt', country='DEU', mask=T))
#class : RasterLayer
#dimensions : 960, 1116, 1071360 (nrow, ncol, ncell)
#resolution : 0.008333333, 0.008333333 (x, y)
#extent : 5.8, 15.1, 47.2, 55.2 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : D:/dummy/DEU_msk_alt.grd
#names : DEU_msk_alt
#values : -179, 2701 (min, max)
altT <- rast(alt)
# rast is supposed to be able to read RasterLayer, but it fails. Why?
# Error …Run Code Online (Sandbox Code Playgroud)