weighted sum in terra::project: what are weights?

66 views Asked by At

I project a terra raster into another terra raster, in a different coordinate system, using the project function. The project documentation states that, for method="sum", it calculates the weighted sum of all non-NA contributing grid cells. I don't quite understand what weights are used here: is it the area of the pixels of the input raster calculated in the projected, template raster? Or something else?

1

There are 1 answers

0
Jason On

The documentation is not very clear and I had the same question formethod = "average". terra uses gdal_warp under the hood in these cases, and although the GDAL documentation doesn't give anymore info than the terra documentation, I found this Github pull request with comments that does help clarify that the weights are the areas of the contributing cells.

The following reprex also confirms that it is area weighted sums:

library(terra)
#> terra 1.7.55
r <- rast(nrows = 1, ncols = 3)
values(r) <- c(3,5,7)
r2 <- rast(nrows = 1, ncols = 2)

plot(r)
text(r, digits = 1)
lines(as.polygons(r2), col = "red")

r_resample <- resample(r, r2, method = "sum")
plot(r_resample)
text(r_resample, digits = 1)

#this agrees with manual area weighted sum calculation:
#left hand pixel
(3*1 + 5*0.5)
#> [1] 5.5
#right hand pixel
(5*0.5 + 7*1)
#> [1] 9.5

Created on 2023-11-16 with reprex v2.0.2