Search code examples
rrasterterra

Why is terra's focal function changing raster location?


I am using the focal function in the terra package to fill holes in a DEM raster. However, after running focal(m, w=19, fun=mean, na.policy="only", na.rm=TRUE) my DEM is moved ~11cm relative to it's original position (see images below). This problem does not occur when I simply read in the raster using rast() and then write it using writeRaster(), but it happens when I use focal() regardless of whether I save the output using writeRaster() or using the filename = argument within focal().

I have also noticed that the larger the window size used, the greater the movement of the output raster. A window size of 9 produced ~5cm error. Raster pixels are ~1.2cm in size so focal() appears to be causing a movement of ~1/2 the width of the window used. As far as I can tell the movement is straight up/north.

Any idea why this might be occuring and how it could be avoided?

This the correctly aligned raster:

enter image description here

And this is after running focal (w = 19):

enter image description here


Solution

  • That is a bug that, I believe, has been fixed in the development version of terra available at https://github.com/rspatial/terra (and that is also the best place to report bugs). Can you please try it?