I am trying to understand how the R package terra
decides on the properties of the new raster it creates when projecting. I know best practice when projecting is to use a template raster in the new crs, e.g. project(my_raster, my_template_raster)
. However, if the user does try to project just using a crs e.g. project(my_raster,"ESRI:54009")
, it is not clear how terra
decides on the dimensions, extent and resolution for the template raster in the new projection. For example, in the reprex below, the projected raster does not cover the extent of the projected version of the polygonized raster which seems strange.
library(terra)
#> terra 1.7.71
r <- rast(xmin = 0, xmax = 5, ymin = 0, ymax = 5, resolution = 1)
values(r) <- 1:ncell(r)
#Mollweide
proj_moll <- "ESRI:54009"
#create vector grid of raster
v_moll <- r |>
as.polygons(dissolve = FALSE) |>
project(proj_moll)
#project raster to Molleweide
r_moll <- project(r, proj_moll)
#plot: setting limits manually since ext = argument doesn't seem to function correctly at the moment - see https://github.com/rspatial/terra/issues/1495
plot(r_moll, xlim = c(ext(v_moll)$xmin, ext(v_moll)$xmax), ylim = c(ext(v_moll)$ymin, ext(v_moll)$ymax))
lines(v_moll, col = "red", lwd = 2)
Created on 2024-05-07 with reprex v2.1.0