Search code examples
rgeospatialubuntu-18.04coredumpterra

R terra::project function causing core dump (Ubuntu)


I'm consistently having the project function in the terra R package (v1.7-29) crash in R (4.3.0). This happens via Terminal and in RStudio, and despite the terra package being installed without errors.

Reprex:

library(terra)

# SPECIFY PROJECTIONS 
pt.proj <- "+proj=longlat +datum=WGS84"
calc.proj <- 
  "+proj=aea + lat_1=14.5 + lat_2=32.5 + lat_0=24.0 + lon_0=-105 +x_0=0 +y_0=0 + ellps =GRS80 +datum=NAD83 + units=m +no_defs"

# BUILDING SPATIAL VECTOR
# Fields
ids <- c("id00041130", "id00043728", "id00032757", "id00035604")
spp <- rep("Quercus_aerea", 4)
lats <- c(27.4, 25.1, 26.9, 28.2)
longs <- c(-108, -106, -107, -107)
spat_points <- cbind.data.frame(ids, spp, lats, longs)
# Convert to spatial vector
spat_vect <- vect(spat_points, geom=c("longs", "lats"), crs=pt.proj, keepgeom=FALSE)

# PROJECT FUNCTION (CRASHING)
spat_vect.calc <- project(spat_vect,calc.proj)

# Generates the message:
# "free(): double free detected in tcache 2"
# "Aborted (core dumped)"

Session info:

> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3 
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3;  LAPACK version 3.7.1

Because terra v1.7-29 has certain system requirements, I checked these using sf::sf_extSoftVersion; all appear to be met.

> sf::sf_extSoftVersion()
          GEOS           GDAL         proj.4 
       "3.6.2"        "2.2.3"        "4.9.3" 
GDAL_with_GEOS     USE_PROJ_H           PROJ 
        "true"        "false"        "4.9.3"

Any hints on other things I can check with this package, or suggestions on how I could more clearly diagnose the problem, would be greatly appreciated. Thanks!


Solution

  • I see the same with old GDAL/PROJ

    library(terra)
    #terra 1.7.33
    gdal(lib="")
    #   gdal    proj    geos
    #"2.2.3" "4.9.3" "3.6.2"
    

    But it works for me if you correctly specify the PROJ4 string (no spaces between the "+" and the keyword.)

    prj <- "+proj=aea +lat_1=14.5 +lat_2=32.5 +lat_0=24.0 +lon_0=-105 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m"
    v <- project(spat_vect, prj)
    

    This is not an issue with newer versions such as

    library(terra)
    terra 1.7.33
    gdal(lib="")
    #    gdal     proj     geos
    # "3.4.3"  "8.2.0" "3.10.2"
    

    Or

    #    gdal     proj     geos 
    # "3.6.2"  "9.2.0" "3.11.2" 
    

    So this appears to be an issue with older GDAL or PROJ. I would recommend getting newer versions. I would start with upgrading Ubuntu as you are still using version 18.04, which has been deprecated today (31 May 2023).