Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add type="xylz" to complement rast(type="xyz") #1318

Closed
aloboa opened this issue Oct 19, 2023 · 1 comment
Closed

Please add type="xylz" to complement rast(type="xyz") #1318

aloboa opened this issue Oct 19, 2023 · 1 comment

Comments

@aloboa
Copy link

aloboa commented Oct 19, 2023

In reference to
https://stackoverflow.com/questions/74934891/specifying-layers-when-using-rast-type-xyz-to-convert-data-frame-to-spatra
and facing a similar need, I think that, considering terra supports multilayer SpatRaster objects, it would be most natural
having the option
rast(x,type="xylz")
where "l" would stand for layer, thus letting the user simply run, eg,

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))
x <- rast(df, type="xylz")

And the corresponding option
xyl=TRUE
would be appreciated in as.data.frame() to include a layer column within a "long" df format.

@rhijmans
Copy link
Member

Thanks, I have implemented both.

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))
x <- rast(df, type="xylz")

x
#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       :   vals.1,    vals.2,    vals.3 
#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 
 
as.data.frame(x, wide=F, time=T) |> head()
#   layer       time    values
#1 vals.1 2012-01-01 22.023267
#2 vals.1 2012-01-01  9.971164
#3 vals.1 2012-01-01  8.526398
#4 vals.1 2012-01-01  5.357165
#5 vals.1 2012-01-01  2.300250
#6 vals.1 2012-01-01 12.073207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants