'Why do terra::cellSize() and raster::area() produce different estimates of raster cell area?

I just noticed that terra::cellSize() produces cell area estimates that do not match those produced by raster::area().

First, why do these two methods not provide the same answer? Second, which estimate is most accurate? See example below.

library(raster)
#> Loading required package: sp
library(terra)
#> terra version 1.3.4

# make test raster with raster::raster()
a <- raster::raster(ncols = 100, nrows = 100,
            xmn = -84, xmx = -83, 
            ymn = 42, ymx = 43)

# make test raster with terra::rast()
b <- terra::rast(ncols = 100, nrows = 100, 
          xmin = -84, xmax = -83, 
          ymin = 42, ymax = 43)

# calculate cell areas (km2)
a_area <- raster::area(a) # km by default
b_area <- terra::cellSize(b, unit = "km")

# sum across cells
a_sum <- raster::cellStats(a_area, "sum")
b_sum <- terra::global(b_area, fun = "sum")

a_sum
#> [1] 9088.98
b_sum
#>           sum
#> area 9130.795

# note that this terra workflow yields the same answer as terra::expanse()
terra::expanse(b, unit = "km")
#> [1] 9130.795


sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS  10.16
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] terra_1.3-4  raster_3.4-5 sp_1.4-5    
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.6        codetools_0.2-18  lattice_0.20-41   digest_0.6.27    
#>  [5] grid_4.0.2        magrittr_2.0.1    evaluate_0.14     highr_0.8        
#>  [9] rlang_0.4.10      stringi_1.5.3     rmarkdown_2.6     rgdal_1.5-19     
#> [13] tools_4.0.2       stringr_1.4.0     xfun_0.20         yaml_2.2.1       
#> [17] compiler_4.0.2    htmltools_0.5.1.1 knitr_1.31
packageVersion("raster")
#> [1] '3.4.5'
packageVersion("terra")
#> [1] '1.3.4'

Created on 2021-07-08 by the reprex package (v0.3.0)

I have been using raster (and loving it) for the last few years, and have recently been blown away by the speed and other improvements provided by the newer terra package. I am assuming that the area estimates provided by terra::cellSize() (or terra::expanse(), for that matter) are more accurate than raster::area(), but I'd love to know more about what changed before I update previous area estimates.

Thanks for everything you do, https://github.com/rspatial!



Solution 1:[1]

raster uses the product of the width (longitude) and height (latitude) of a cell. terra is more precise, it computes the spherical area of a cell (as defined by its four corners). This matters most at high latitudes, where the width of a cell changes most, and can be different between the bottom and the top of a cell. So the difference will be largest at high latitudes and with cells with a low vertical resolution.

Illustrated here:

library(raster)
library(terra)
a <- raster::raster()
b <- terra::rast()
values(a) <- 1:ncell(a)
values(b) <- 1:ncell(b)

a_rast <- rast(area(a))
a_terra <- cellSize(b, unit = "km")

dif <- a_terra - a_rast    
reldif <- 100 * dif / a_terra
plot(reldif)

enter image description here

Near the poles the difference, with these cells, is almost 1%, and it is ~0 at the equator.

A related major change is that terra also computes the true cell-size for projected (i.e. not lon-lat) rasters; intstead of assuming that the nominal resolution is constant.

r <- rast(ncols=90, nrows=45, ymin=-80, ymax=80)
m <- project(r, "+proj=merc")

bad <- init(m, prod(res(m)) / 1000000, names="naive")
good <- cellSize(m, unit="km", names="corrected")
plot(c(good, bad), nc=1, mar=c(2,2,1,6))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1