I'm having trouble to convert a SpatRaster back to im. Here's an example:
library(spatstat)
library(gstat)
library(terra)
size=100
env <- im(matrix(0, size, size), xrange=c(0,1), yrange=c(0,1))
xy <- expand.grid(x = env$xcol, y = env$yrow)
temp.dummy <- gstat(formula=z~1, locations=~x+y, dummy=T, beta=0,
model=vgm(psill=0.1, range=50, model='Exp'), nmax=20)
temp <- predict(temp.dummy, newdata=xy, nsim=1)
temp <- as.im(temp)
temp_im <- as.im(raster::raster(temp))
temp_im <- as.im(terra::rast(temp))
Error in as.im.default(rast(temp)) : Can't convert X to a pixel image
It works with raster::raster() but not with terra::rast().
Since maptool is bieng retired, this Convert raster to im object is no longer a solution.
Thanks!
I think the function below works. Perhaps you can ask the maintainer of "spatstat.geom" to include this or something similar in their package.
And here is an alternative that does not use terra-specific function names. For this one to work, you need
as.list(X, geom=TRUE)which was introduced in terra 1.7-72 (currently the development version). This version also handles factors/characters.Illustration