I'm working with two rasters, both the same projection, but one is at a 4x higher resolution and with a different origin. I need to resample the high resolution raster to match the resolution of the low resolution raster. It doesn't matter which origin they end up using as long as they align with each other. The easiest way to do this is with resample, as shown with some sample data below.
I've seen advice suggesting that aggregation be used over resampling when possible. In this case, the lower resolution is a multiple of the higher resolution, so aggregation is possible, but it would leave the data misaligned and would still need to be resampled.
My instinct is that resample() is the best approach here, but is there a circumstance where it would make more sense to aggregate first? Or to resample to match the origin, and then aggregate? Based on my test sample, it looks the two methods produce similar results except maybe along the edge of the sample.
rast_low <- rast(nrows=10, ncols=10, xmin=0, xmax=10, ymin=0, ymax=10)
values(rast_low) <- 1:ncell(rast_low)
origin(rast_low) <- 0.5
rast_high <- rast(nrows=48, ncols=40, xmin=1, xmax=11, ymin=-1, ymax=11)
values(rast_high) <- 1:ncell(rast_high)
#resample
rast_resamp <- resample(rast_high, rast_low, method="bilinear")
#aggregate
rast_agg <- aggregate(rast_high, fact=2, fun="mean")
rast_aggresamp <- resample(rast_agg, rast_low, method="bilinear")
#check difference between methods
diff <- rast_aggresamp - rast_resamp
I'm working with two rasters, both the same projection, but one is at a 4x higher resolution and with a different origin. I need to resample the high resolution raster to match the resolution of the low resolution raster. It doesn't matter which origin they end up using as long as they align with each other. The easiest way to do this is with resample, as shown with some sample data below.
I've seen advice suggesting that aggregation be used over resampling when possible. In this case, the lower resolution is a multiple of the higher resolution, so aggregation is possible, but it would leave the data misaligned and would still need to be resampled.
My instinct is that resample() is the best approach here, but is there a circumstance where it would make more sense to aggregate first? Or to resample to match the origin, and then aggregate? Based on my test sample, it looks the two methods produce similar results except maybe along the edge of the sample.
rast_low <- rast(nrows=10, ncols=10, xmin=0, xmax=10, ymin=0, ymax=10)
values(rast_low) <- 1:ncell(rast_low)
origin(rast_low) <- 0.5
rast_high <- rast(nrows=48, ncols=40, xmin=1, xmax=11, ymin=-1, ymax=11)
values(rast_high) <- 1:ncell(rast_high)
#resample
rast_resamp <- resample(rast_high, rast_low, method="bilinear")
#aggregate
rast_agg <- aggregate(rast_high, fact=2, fun="mean")
rast_aggresamp <- resample(rast_agg, rast_low, method="bilinear")
#check difference between methods
diff <- rast_aggresamp - rast_resamp
Share
Improve this question
asked Jan 20 at 18:54
JakenJaken
3351 silver badge8 bronze badges
1 Answer
Reset to default 3If you are going from a higher resolution to a lower resolution, using resample(method="bilinear")
directly is not great because you are only using the values of the four cells directly around the center of the new cell. Instead you can use resample(method="average")
.
You can see that "average" is very similar to "aggregate" with this example:
library(terra)
set.seed(1)
x <- rast(nrows=100, ncols=100, xmin=0, xmax=25, ymin=0, ymax=25)
values(x) <- sample(ncell(x))
a <- aggregate(x, 10, mean)
r1 <- resample(x, a, "bilinear", names="bilinear")
r2 <- resample(x, a, "average", names="average")
boxplot(c(r1-a, r2-a))
For a categorical raster you could consider the "mode" method.