I have raster layers in a few different projections that I've loaded into R. I eventually plan on using the "merge" function of the raster package to bring these together as one surface, but to do that, all of the rasters need to have the same crs, resolution, and origin. Using projectRaster with "alignOnly=TRUE, says it will use "to or other parameters only to align the output (i.e. same origin and resolution), but use the projected extent from from," which sounds like what I need. However, this fails and gives me the warning "Warning in projectRaster(x, crs = projection(y)) : 'from' has no cell values." Does anyone have any suggestions on how to accomplish this? Is this a bug, or am I interpreting the documentation incorrectly?
Below is a small reproducible example of trying to reproject one raster with the GRS80 ellipsoid and 1.5m resolution to align with another raster of a different extent that uses the WGS84 ellipsoid and has 4m resolution.
#Create a raster with GRS80 crs and 1.5m resolution
r1<- raster(xmn=731458, xmx = 731458+100*1.5, ymn=3116234, ymx= 3116234+100*1.5,
nrow=100, ncol=100, vals=sample(1:50, size = 100*100, replace = TRUE), res=1.5,
crs= "+proj=utm +zone=16 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs ")
#Create a raster with WGS84 crs and 4m resolution
r2<- raster(xmn=740000, xmx = 740000+100*4, ymn=3000000, ymx= 3000000+100*4,
nrow=100, ncol=100, vals=sample(1:50, size = 100*100, replace = TRUE),
res=4, crs= "+proj=utm +zone=16 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
#Reproject r1 to WGS with 4m resolution
r1_res4_WGS84<- projectRaster(from = r1, crs = crs(r2), res= res(r2))
#Origins do not match after reprojecting
origin(r1_res4_WGS84) #1.999999e+00 9.094551e-05
origin(r2) #0 0
#Try reprojecting with alignOnly=TRUE
r1_res4_WGS84_align<- projectRaster(from = r1, to = r2, alignOnly = TRUE)
# Warning in projectRaster(x, crs = projection(y)) :
# 'from' has no cell values