skip to Main Content

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!

2

Answers


  1. 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).

    Login or Signup to reply.
  2. Seconding @robert-hijamns recommendation that newer versions can be easier. You are having an existential conflict here: you desire and use the current R version 4.3.0 but then constrain it into an OS system from five years ago: Ubuntu 18.04 LTS.

    Consider an alternative with fewer constraints: Ubuntu 22.04 LTS. I am using a Rocker container here based on 22.04 and offering all as CRAN as binaries thanks to r2u.

    Then, after a quick launch of the rocker/r2u:22.04 container and a quick apt update -qq; apt upgrade we can just do install.r terra. That is where the magic happens: r2u knows that terra equatess to r-cran-terra and it install that package and all its binary dependencies. And all it takes is 59 seconds to install the 56 packages needed.

    And then I can run your script, without a segfault:

    > source("question.R")  # the code you posted
    terra 1.7.29
    > spat_vect.calc
     class       : SpatVector 
     geometry    : points 
     dimensions  : 4, 2  (geometries, attributes)
     extent      : -293639.2, -99657.76, 123683.6, 471931.6  (xmin, xmax, ymin, ymax)
     coord. ref. : +proj=aea +lat_0=24 +lon_0=-105 +lat_1=14.5 +lat_2=32.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs 
     names       :        ids           spp
     type        :      <chr>         <chr>
     values      : id00041130 Quercus_aerea
                   id00043728 Quercus_aerea
                   id00032757 Quercus_aerea
    > 
    

    Give r2u a shot. It is a repo, so it works with server, desktop, laptop, cloud, CI, … use. It has been coming in handy for a few of us for over a year now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search