rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
544 stars 94 forks source link

merge() and mosaic(): R crashes #1650

Open aloboa opened 1 week ago

aloboa commented 1 week ago

Given a list of SpatRaster objects of 512 512 204 that sequentially overlap in a ~25%, I try to make one single object with merge() or mosaic().

While both merge() and mosaic() work with up to ~7 SpatRaster objects, eg

m <- merge(r01, r02, r03)
plotRGB(m,50,100,190,stretch="linear")

(as you can see, this a quick and dirty solution and a better result is being worked out through more sophisticated automatic stitching)

image

either

m <- merge(r01, r02, r03, r04, r05, r06,r07, r08, r09, r10, r11, r12, r13, r14, r15, r16,
           filename=file.path(dirG,"SUM-ZON23-1B-1G_IQ.dat"), 
           wopt=list(filetype="ENVI",overwrite="TRUE"))

or

m <- mosaic(r01, r02, r03, r04, r05, r06,r07, r08, r09, r10, r11, r12, r13, r14, r15, r16, 
            fun="first", filename=file.path(dirG,"SUM-ZON23-1B-1G_IQ.dat"), 
            wopt=list(filetype="ENVI",overwrite="TRUE"))

make R and RStudio crash with a bomb:

All raster 
R Session Aborted
R encountered a fatal error
The session was terminated
Start New Session
...
[18134:18135:20241122,160519.936441ERROR elf_dynamic_array_reader.h:61] tag not found
[18134:18135:20241122,160519.936206:ERROR directroy_reader_postx.cc:42] opendir: No such file or directroy (2)

I understand this is a memory problem (although the process should end with an error message and not a crash). Having stated the output file, I thought the object "m" would not make crash R, as it is not needed to be in memory.

Any way around this problem?

> sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Pop!_OS 22.04 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;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=es_ES.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=es_ES.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=es_ES.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Madrid
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] terra_1.8-1

loaded via a namespace (and not attached):
 [1] compiler_4.4.2    fastmap_1.2.0     cli_3.6.3         htmltools_0.5.8.1 tools_4.4.2       rstudioapi_0.15.0 yaml_2.3.10      
 [8] Rcpp_1.0.13-1     codetools_0.2-19  rmarkdown_2.28    knitr_1.48        xfun_0.49         digest_0.6.36     rlang_1.1.4      
[15] evaluate_0.24.0  
aloboa commented 3 days ago

As an alternative, I am using gdalbuildvrt through system() to build a vrt and then read and write to ENVI format.