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
I see the same with old GDAL/PROJ
But it works for me if you correctly specify the PROJ4 string (no spaces between the "+" and the keyword.)
This is not an issue with newer versions such as
Or
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).
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 quickapt update -qq; apt upgrade
we can just doinstall.r terra
. That is where the magic happens:r2u
knows thatterra
equatess tor-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:
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.