5

I try to build a SpatRast with 2 layers, using the "xyz" reading style. It works with 3 columns as input to the rast function, but I get a warning message with 4 columns:

> 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

Any idea why?

1 Answers1

8

That is a problem, I have just fixed it; and I now get:

library(terra)
#terra version 1.0.0
m <- cbind(x=c(1,1,2,2), y=c(1,2,1,2), z1=1:4, z2=5:8)
r <- rast(m, type="xyz")
values(r)
#     z1 z2
#[1,]  2  6
#[2,]  4  8
#[3,]  1  5
#[4,]  3  7

And

as.data.frame(r, xy=TRUE)
#  x y z1 z2
#1 1 2  2  6
#2 2 2  4  8
#3 1 1  1  5
#4 2 1  3  7

Thank you for reporting this. (if you are pretty sure something is a bug, the better place to report it is here)

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks a lot. I will try to better adapt the destination of my posts. I often hesitate between a Github or a Stackoverflow report, because it is difficult to be sure where the problem comes from. – Jean-Luc Dupouey Dec 15 '20 at 13:31
  • I understand. When in doubt I think it is better to come here – Robert Hijmans Dec 15 '20 at 17:34