简体   繁体   English

使用 rast(..., type=xyz) 将 data.frame 转换为 terra 中的 SpatRaster 时指定图层 package (R)

[英]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我可以想到一种使用循环的方法

  1. 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"))

  2. creates seperate SpatRasters for each individual date为每个单独的日期创建单独的 SpatRaster

  3. 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以下是如何使用splitreshape

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.

相关问题 如何使用 terra 包将栅格堆栈中提取的值添加到 Spatial 对象的 data.frame 中? - How to add the extracted values from raster stack to the data.frame of the Spatial object using terra package? 使用 R 将字符串转换为 data.frame - Convert Strings into data.frame using R 是否有 R function 将“flowCore” package 的“flowFrame”结构转换为“data.frame”? - Is there an R function to convert 'flowFrame' structure of 'flowCore' package to a 'data.frame'? 如何将具有相同类型字段的列表转换为R中的data.frame - How to convert a list with same type of field to a data.frame in R 将列表类型转换为R,data.frame和as.data.frame中的数据帧类型不起作用 - convert list type to data frame type in R, data.frame and as.data.frame does not wok 使用R Package dplyr替换data.frame的子集的问题 - Issues with replacing a subset of a data.frame using the R Package dplyr R:将data.frame转换为具有正确尺寸的data.frame - R: convert data.frame to data.frame with correct dimensions 使用R中的XLSX包在Excel中打印data.frame时出错 - Error in printing data.frame in excel using XLSX package in R R:使用ggplot2将data.frame转换为颜色矩阵吗? - R: Convert data.frame to color matrix using ggplot2? 使用 R 将 data.frame 转换为简单向量 - Using R convert data.frame to simple vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM