[英]Specifying layers when using rast(..., type=xyz) to convert data.frame to SpatRaster in the terra package (R)
I have a simple csv file with lon/lat/time/values columns:我有一个简单的 csv 文件,其中包含 lon/lat/time/values 列:
df <- data.frame(longitude=rep(c(10.5,11,12),10),
latitude=rep(c(10.5,11,12),10),
date= as.Date(rep(c("2012-01-01", "2012-02-01", "2012-03-01"), 10)),
vals=rnorm(30,10,5))
I'd like to convert this to a SpatRaster using the S4 method for signature 'data.frame' rast(x, type="xyz", crs="", digits=6, extent=NULL)
, where each "date" would be a seperate layer.我想使用签名 'data.frame' rast(x, type="xyz", crs="", digits=6, extent=NULL)
的 S4 方法将其转换为 SpatRaster,其中每个“日期”将是一个单独的层。
Importing without the date works fine:没有日期的导入工作正常:
df.subset <- select(df,-date)
tmp <- terra::rast(df.subset, type="xyz")
I've tried to split the SpatRaster by date but get a Error in.local(x, f, ...): length(f) == nlyr(x) is not TRUE
error:我试图按日期拆分 SpatRaster 但出现Error in.local(x, f, ...): length(f) == nlyr(x) is not TRUE
错误:
split(tmp, as.factor(df$date))
I can think of an approach using a loop that我可以想到一种使用循环的方法
splits the df by date: split(df, c("2012-01-01", "2012-02-01", "2012-03-01"))
按日期拆分 df: split(df, c("2012-01-01", "2012-02-01", "2012-03-01"))
creates seperate SpatRasters for each individual date为每个单独的日期创建单独的 SpatRaster
Use merge
to combine individual SpatRasters with named layers使用merge
将单个 SpatRaster 与命名层组合
Is there a tidier way of doing this in terra?在 terra 中有更简洁的方法吗?
Here is how you can use split
or reshape
以下是如何使用split
或reshape
Example data示例数据
library(terra)
set.seed(0)
df <- data.frame(longitude=rep(seq(10,19,1), 3),
latitude=rep(seq(10, 19,1), 3),
date= as.Date(rep(c("2012-01-01", "2012-02-01", "2012-03-01"), each=10)),
vals=rnorm(30,10,5))
reshape重塑
w <- reshape(df, timevar="date", idvar=c("longitude", "latitude"), direction="wide")
x <- rast(w, type="xyz")
split分裂
r <- split(df[,-3], df$date)
r <- lapply(r, \(i) rast(i, type="xyz"))
r <- rast(r)
# and perhaps
time(r) <- as.Date(names(r))
r
#class : SpatRaster
#dimensions : 10, 10, 3 (nrow, ncol, nlyr)
#resolution : 1, 1 (x, y)
#extent : 9.5, 19.5, 9.5, 19.5 (xmin, xmax, ymin, ymax)
#coord. ref. :
#source(s) : memory
#names : 2012-01-01, 2012-02-01, 2012-03-01
#min values : 2.30025, 3.812308, 3.577003
#max values : 22.02327, 13.817967, 15.428847
time (days) : 2012-01-01 to 2012-03-01
You could also do the splitting "manually", inside the lapply
loop您也可以在lapply
循环内“手动”进行拆分
ud <- unique(df$date)
x <- lapply(ud, \(d) rast(df[df$date == d, -3], type="xyz"))
x <- rast(x)
names(x) <- ud
You can use one of the apply
functions to make a list of rasters and then use rast
again on this list.您可以使用其中一个apply
函数来制作栅格列表,然后在此列表上再次使用rast
。
library(terra)
library(dplyr)
# I changed the data so that different dates have many locations
# rast complains otherwise
df <- data.frame(longitude=rep(c(10.5,11,12),10),
latitude=rep(c(10.5,11,12),10),
date= as.Date(c(rep("2012-01-01", 10), rep("2012-02-01", 10), rep("2012-03-01", 10))),
vals=rnorm(30,10,5))
# A function to filter for dates
# and remove the date column (change this if you want to keep it)
myrast <- function(df, date_to_filter) {
df1 <- df %>% filter(date == date_to_filter) %>% select(-date)
raster_return <- rast(x = df1, type='xyz')
raster_return
}
# Iterate over all the unique dates
rr <- sapply(unique(df$date), function(d) myrast(df, d))
# Combine the list of raster objects
rr_all <- rast(rr)
plot(rr_all)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.