When writing then reading a SpatRaster with certain file formats (ESRI .hdr labeling format, netCDF and ENVI, but not GeoTIFF or Erdas HFA), the raster origin, resolution, and coordinates change slightly. Enough for equality tests for these parameters to return the FALSE value. Here is an example, with GeoTiff then flt.
Nrow=45; Ncol=108
r1 <- rast(nrow=Nrow,ncol=Ncol,vals=1:(Nrow*Ncol),ext=c(-180,180,-60,90))
origin(r1)
# [1] 0 0
writeRaster(r1,"test.tif",overwrite=TRUE)
r2 <- rast("test.tif")
origin(r2)
# origin is OK, same as r1
# [1] 0 0
res(r1)==res(r2)
# [1] TRUE TRUE
writeRaster(r1,"test.flt",overwrite=TRUE)
r3 <- rast("test.flt")
origin(r3)
# origin has been shifted
# [1] 1.705303e-13 5.684342e-14
res(r1)==res(r3)
# the lag is large enough for r3 to be considered as a different raster from r1
# [1] FALSE FALSE
Why do such shifts occur? Is there a way to prevent them, and if not, what can I do to correct them after reading?