Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mosaic causes R crash if SpatRasterCollection elements have multiple layers #1524

Open
johnbaums opened this issue May 30, 2024 · 0 comments

Comments

@johnbaums
Copy link
Contributor

R crashes when using mosaic on a SpatRasterCollection with elements having multiple layers. Reprex provided for one of the working cases, below.

library(terra)
set.seed(1)
e <- ext(113, 154, -44, -10)
r <- replicate(2, {
  x <- round(sort(runif(2, e[1], e[2])), 2)
  y <- round(sort(runif(2, e[3], e[4])), 2)
  init(rast(ext(c(x, y)), res = 0.05, crs = "EPSG:4326", nlyrs = 2), runif)
}) |> sprc()
terra::mosaic(r)
## Floating point exception (core dumped)

Interestingly, the mosaic operation completes successfully if we use merge first:

library(terra)
#> terra 1.7.71
set.seed(1)
e <- ext(113, 154, -44, -10)
r <- replicate(2, {
  x <- round(sort(runif(2, e[1], e[2])), 2)
  y <- round(sort(runif(2, e[3], e[4])), 2)
  init(rast(ext(c(x, y)), res = 0.05, crs = "EPSG:4326", nlyrs = 2), runif)
}) |> sprc()
terra::merge(r)
#> class       : SpatRaster 
#> dimensions  : 260, 451, 2  (nrow, ncol, nlyr)
#> resolution  : 0.05, 0.05  (x, y)
#> extent      : 123.89, 146.44, -24.52, -11.52  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :        lyr.1,        lyr.2 
#> min values  : 0.0001064336, 8.431729e-06 
#> max values  : 0.9999305937, 9.999035e-01
terra::mosaic(r)
#> class       : SpatRaster 
#> dimensions  : 260, 451, 2  (nrow, ncol, nlyr)
#> resolution  : 0.05, 0.05  (x, y)
#> extent      : 123.89, 146.44, -24.52, -11.52  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :        lyr.1,        lyr.2 
#> min values  : 0.0001064336, 8.431729e-06 
#> max values  : 0.9999305937, 9.999035e-01

Created on 2024-05-30 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       Ubuntu 20.04.6 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Melbourne
#>  date     2024-05-30
#>  pandoc   2.5 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version date (UTC) lib source
#>  P cli           3.6.2   2023-12-11 [?] RSPM (R 4.3.0)
#>  P codetools     0.2-19  2023-02-01 [?] CRAN (R 4.2.2)
#>  P digest        0.6.34  2024-01-11 [?] RSPM (R 4.3.0)
#>  P evaluate      0.23    2023-11-01 [?] RSPM (R 4.3.0)
#>  P fastmap       1.1.1   2023-02-24 [?] RSPM (R 4.3.0)
#>    fs            1.6.3   2023-07-20 [1] RSPM (R 4.3.2)
#>  P glue          1.7.0   2024-01-09 [?] RSPM (R 4.3.0)
#>  P htmltools     0.5.8.1 2024-04-04 [?] CRAN (R 4.3.2)
#>  P knitr         1.45    2023-10-30 [?] RSPM (R 4.3.0)
#>  P lifecycle     1.0.4   2023-11-07 [?] RSPM (R 4.3.0)
#>  P magrittr      2.0.3   2022-03-30 [?] RSPM (R 4.3.0)
#>  P purrr         1.0.2   2023-08-10 [?] RSPM (R 4.3.0)
#>    R.cache       0.16.0  2022-07-21 [1] RSPM (R 4.3.2)
#>    R.methodsS3   1.8.2   2022-06-13 [1] RSPM (R 4.3.2)
#>    R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.2)
#>    R.utils       2.12.3  2023-11-18 [1] RSPM (R 4.3.2)
#>  P Rcpp          1.0.12  2024-01-09 [?] CRAN (R 4.3.2)
#>    renv          1.0.3   2023-09-19 [1] CRAN (R 4.3.2)
#>  P reprex        2.1.0   2024-01-11 [?] CRAN (R 4.3.2)
#>  P rlang         1.1.3   2024-01-10 [?] RSPM (R 4.3.0)
#>  P rmarkdown     2.27    2024-05-17 [?] CRAN (R 4.3.2)
#>  P sessioninfo   1.2.2   2021-12-06 [?] CRAN (R 4.3.2)
#>    styler        1.10.2  2023-08-29 [1] RSPM (R 4.3.2)
#>  P terra       * 1.7-71  2024-01-31 [?] RSPM (R 4.3.0)
#>  P vctrs         0.6.5   2023-12-01 [?] RSPM (R 4.3.0)
#>  P withr         3.0.0   2024-01-16 [?] RSPM (R 4.3.0)
#>  P xfun          0.41    2023-11-01 [?] RSPM (R 4.3.0)
#>  P yaml          2.3.8   2023-12-11 [?] RSPM (R 4.3.0)
#> 
#>  [1] /home/unimelb.edu.au/jbau/23a-v2/renv/library/R-4.3/x86_64-pc-linux-gnu
#>  [2] /home/unimelb.edu.au/jbau/.cache/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/9a444a72
#> 
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Similarly, the mosaic works fine if the elements of the collection have a single layer:

library(terra)
set.seed(1)
e <- ext(113, 154, -44, -10)
r <- replicate(2, {
  x <- round(sort(runif(2, e[1], e[2])), 2)
  y <- round(sort(runif(2, e[3], e[4])), 2)
  init(rast(ext(c(x, y)), res = 0.05, crs = "EPSG:4326", nlyrs = 1), runif)
}) |> sprc()

terra::mosaic(r)
## class       : SpatRaster 
## dimensions  : 403, 444, 1  (nrow, ncol, nlyr)
## resolution  : 0.05, 0.05  (x, y)
## extent      : 123.89, 146.09, -33.26, -13.11  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326) 
## source(s)   : memory
## name        :     lyr.1 
## min value   : 0.0000000 
## max value   : 0.9994188 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant