Skip to content

spatSample - stratified sampling function generating core dump error #1858

@M3nin0

Description

@M3nin0

Hi,

Thank you for the terra package!

I'd like to report an error I'm encountering when using terra::spatSample with method = 'stratified'. The issue arises when I run the following code:

# Example file available here: https://github.com/e-sensing/sitsdata/raw/refs/heads/main/inst/extdata/world-cover-mato-grosso/WORLD-COVER_2021_S12W057_class_2021-12-31.tif

rast <- terra::rast('https://github.com/e-sensing/sitsdata/raw/refs/heads/main/inst/extdata/world-cover-mato-grosso/WORLD-COVER_2021_S12W057_class_2021-12-31.tif')
points <- terra::spatSample(
    x = rast, 
    size = 500, 
    method = "stratified", 
    as.points = TRUE
)

I receive the following error message:

 *** caught segfault ***
address (nil), cause 'unknown'

Traceback:
 1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x55618601ad00>,     dll = list(name = "Rcpp", path = "/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so",         dynamicLookup = TRUE, handle = <pointer: 0x55618752b580>,         info = <pointer: 0x55618522fbe0>, forceSymbols = FALSE),     numParameters = -1L), <pointer: 0x55618940d770>, <pointer: 0x556187578660>,     .pointer, ...)
 2: x@pntr$sampleStratifiedCells(size, each, replace, .seed(), spatOptions())
 3: .sampleCellsStratified(x, size, each, replace, ext)
 4: sampleStratified2(x, size, replace = replace, as.df = as.df,     as.points = as.points, values = values, cells = cells, xy = xy,     ext = ext, warn = warn, each = each)
 5: .local(x, ...)
 6: terra::spatSample(rast, 500, "stratified", as.points = TRUE)
 7: terra::spatSample(rast, 500, "stratified", as.points = TRUE)
An irrecoverable exception occurred. R is aborting now ...
Segmentation fault (core dumped)

I was able to track this Segmentation fault (core dumped) when running the code in a terminal. When I run it in RStudio, the session crashes and R restarts. While testing, I tried other files - for example, this one - and I was able to reproduce the error.

The interesting point is that it doesn't throw the error every time (maybe related to the random sampling?). Sometimes it runs successfully and returns results, and other times it fails with the error:

> rast <- terra::rast('WORLD-COVER_2021_S12W057_class_2021-12-31.tif')
> points <- terra::spatSample(
    x = rast, 
    size = 500, 
    method = "stratified", 
    as.points = TRUE
)
> points
 class       : SpatVector 
 geometry    : points 
 dimensions  : 4000, 1  (geometries, attributes)
 extent      : -55.80254, -55.19904, -11.80196, -11.49587  (xmin, xmax, ymin, ymax)
 coord. ref. : lon/lat WGS 84 (EPSG:4326) 
 names       : cover
 type        : <num>
 values      :    10
                  10
                  10

Since the error doesn't appear consistently, I created a simple for loop to help reproduce it:

for (i in seq_len(100)) {
  print(paste0('Iteration: ', i))

  rast <- terra::rast('WORLD-COVER_2021_S12W057_class_2021-12-31.tif')
  points <- terra::spatSample(
    x = rast,
    size = 500,
    method = "stratified",
    as.points = TRUE
 )
}

I ran the same code on macOS Sequoia 15.5 and Ubuntu 22.04, using R versions 4.4.1 and 4.5.1, and was able to reproduce the issue.

We initially identified this behavior because we use terra as part of some sits functions for sampling data.

Please let me know if I can provide more information or other details.

Thanks a lot!

@M3nin0 / @OldLipe

session info - R 4.4.1
> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
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;  LAPACK version 3.10.0

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

time zone: Etc/UTC
tzcode source: system (glibc)

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

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

loaded via a namespace (and not attached):
[1] compiler_4.4.1    tools_4.4.1       rstudioapi_0.17.1 Rcpp_1.0.14       codetools_0.2-20 
session info - R 4.5.1
> sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0  LAPACK version 3.10.0

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

time zone: Etc/UTC
tzcode source: system (glibc)

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

loaded via a namespace (and not attached):
[1] compiler_4.5.1   Rcpp_1.0.14      codetools_0.2-20 terra_1.8-54

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions