I tried to read a DEM raster using getData (from the raster package), then convert the RasterLayer to a SpatRaster (terra package). First step worked, but the second one failed.
library(raster)
library(terra)
(alt <- getData('alt', country='DEU', mask=T))
#class : RasterLayer
#dimensions : 960, 1116, 1071360 (nrow, ncol, ncell)
#resolution : 0.008333333, 0.008333333 (x, y)
#extent : 5.8, 15.1, 47.2, 55.2 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : D:/dummy/DEU_msk_alt.grd
#names : DEU_msk_alt
#values : -179, 2701 (min, max)
altT <- rast(alt)
# rast is supposed to be able to read RasterLayer, but it fails. Why?
# Error : [rast] cannot read from D:/dummy/DEU_msk_alt.grd
Some hints? :
rast("DEU_msk_alt.grd")
# Error : [rast] cannot read from D:/dummy/DEU_msk_alt.grd
rast("DEU_msk_alt.vrt")
#class : SpatRaster
#dimensions : 960, 1116, 1 (nrow, ncol, nlyr)
#resolution : 0.008333333, 0.008333333 (x, y)
#extent : 5.8, 15.1, 47.2, 55.2 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +ellps=WGS84 +no_defs
#data source : DEU_msk_alt.vrt
#names : DEU_msk_alt
It looks like the rast function is looking for a .vrt file, while getData associated the raster to the grd file. Whatever, rast should work when applied to a RasterLayer, according to the documentation.
Any idea? How to convert such a RasterLayer object to a terra object? What do I miss? Thanks in advance,
JL