terra icon indicating copy to clipboard operation
terra copied to clipboard

terra::resample gives different results from raster::resample with method bilinear

Open see24 opened this issue 2 years ago • 0 comments

I am migrating a package from using raster to using terra. And I am comparing the results with the old version to the new version to make sure I haven't changed much. I traced the difference I was seeing to a resampling step. It seems like terra and raster resample have different results. Is this expected? If so it would be helpful if this was added to the changed behaviour section here .

library(terra)

r <- rast(nrows=25, ncols=30, xmin=1, xmax=11, ymin=-1, ymax=11)
values(r) <- lapply(1:5, function(x)(rep(x, 150))) |> unlist()
r <- segregate(r)
s <- rast(nrows=10, ncols=10, xmin=0, xmax=10, ymin=0, ymax=10)
x <- resample(r, s, method="bilinear")

x_rast <- raster::resample(as(r, "Raster"), as(s, "Raster"), method="bilinear")

plot(terra::rast(x_rast) - x)

The above is similar to my use case where the differences are along the edges of "clumps" but there are also differences if we just look at the example from the terra resample docs.

r <- rast(nrows=3, ncols=3, xmin=0, xmax=10, ymin=0, ymax=10)
values(r) <- 1:ncell(r)
s <- rast(nrows=25, ncols=30, xmin=1, xmax=11, ymin=-1, ymax=11)
x <- resample(r, s, method="bilinear")

x_rast <- raster::resample(as(r, "Raster"), as(s, "Raster"), method="bilinear")

see24 avatar Jun 28 '23 18:06 see24