I am trying to merge rasters with same crs and resolution but different extent to one bigger raster. For overlapping areas the function mean should be applied. I have thought this would be the perfect use case for mosaic function from terra. However, when I apply it to all my rasters at one the function breaks. (s. picture below). When I am using merge instead, it works.
Further I have encountered, that using a subset of rasters or doing the mosaic-merge in two steps works. Also, when I apply a different function than mean (e.g, first) it works as expected. Here is a minimal reproducable example
library(terra)
x1 <- rast(xmin=3323433, xmax=3668916, ymin=5774887, ymax=6119500, res=1000, vals=1, crs=terra::crs("EPSG:5677"))
x2 <- rast(xmin=3478201, xmax=3882596, ymin=5757906, ymax=6095527, res=1000, vals=2, crs=terra::crs("EPSG:5677"))
x3 <- rast(xmin=3278500, xmax=3625980, ymin=5410296, ymax=5828826, res=1000, vals=3, crs=terra::crs("EPSG:5677"))
x4 <- rast(xmin=3555086, xmax=3941507, ymin=5550139, ymax=5916728, res=1000, vals=4, crs=terra::crs("EPSG:5677"))
x5 <- rast(xmin=3459229, xmax=3875606, ymin=5228500, ymax=5661015, res=1000, vals=5, crs=terra::crs("EPSG:5677"))
x6 <- rast(xmin=3373358, xmax=3664922, ymin=5244482, ymax=5629050, res=1000, vals=6, crs=terra::crs("EPSG:5677"))
x <- list(x1, x2, x3, x4, x5, x6)
x_ <- list(x1, x2, x3, x4)
# all together
collection <- terra::sprc(x)
m1 <- mosaic(collection)
plot(m1)

# stepwise approach
collection <- terra::sprc(x_)
m2 <- mosaic(collection)
plot(m2)

m3 <- mosaic(m2, x5, x6)
plot(m3)

# all merge
collection <- terra::sprc(x)
m4 <- merge(collection)
plot(m4)

I am working on Ubuntu Yammy and this is my SessionInfo
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=de_DE.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.7-71 dplyr_1.1.4 doParallel_1.0.17 iterators_1.0.14 foreach_1.5.2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.12 pillar_1.9.0 compiler_4.1.2 class_7.3-20 tools_4.1.2 xts_0.13.2 gstat_2.1-1 lifecycle_1.0.4 tibble_3.2.1
[10] lattice_0.20-45 pkgconfig_2.0.3 rlang_1.1.3 DBI_1.2.2 cli_3.6.2 e1071_1.7-14 generics_0.1.3 vctrs_0.6.5 hms_1.1.3
[19] classInt_0.4-10 grid_4.1.2 tidyselect_1.2.0 spacetime_1.3-1 glue_1.7.0 sf_1.0-15 R6_2.5.1 fansi_1.0.6 sp_2.1-3
[28] tzdb_0.4.0 readr_2.1.5 magrittr_2.0.3 intervals_0.15.4 codetools_0.2-18 units_0.8-5 utf8_1.2.4 KernSmooth_2.23-20 proxy_0.4-27
[37] FNN_1.1.4 zoo_1.8-12
That looks pretty wild to me... Not only does this affect terra::mosaic(..., fun = "mean"), but almost all alternative functions are failing, measured by the result at least, using your data.
What does help for reasons I do not fully understand, is to call terra::mosaic(..., fun = "first"). After this, in the second attempt, your initial call seems to work fine:
library(terra)
#> terra 1.7.73
rc <- list(x1, x2, x3, x4, x5, x6) |>
terra::sprc()
# first try fails
terra::mosaic(rc, fun = "mean") |> terra::plot()

# applying some magic
terra::mosaic(rc, fun = "first") |> terra::plot()

# second try seems to work as intended
terra::mosaic(rc, fun = "mean") |> terra::plot()

If I had to guess, I'd say this looks like some memory allocation peculiarity and you propably should open an issue at rspatial/terra - but since you have done this already, let's wait.
Update:
Having had a look at rspatial/terra#1262, I wanted to give v1.7-29 a shot:
url <- "https://cran.r-project.org/src/contrib/Archive/terra/terra_1.7-29.tar.gz"
install.packages(url, repos = NULL, type = "source")
library(terra)
#> terra 1.7.29
rc <- list(x1, x2, x3, x4, x5, x6) |>
terra::sprc()
# and suddenly, it seems to work from the beginning
terra::mosaic(rc, fun = "mean") |> terra::plot()

So I'd definitely consider this a bug related to #1262, with some unresolved issues left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With