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!