dieghernan / tidyterra

tidyverse and ggplot2 methods for terra spatial objects

Home Page:https://dieghernan.github.io/tidyterra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error while combined with ggplot: Error: node stack overflow

GuiSPinto opened this issue · comments

Hi!

I keep getting this error since quite recently. I tried to reinstall all the packages but the error persists. However, the spatvectors do work with base plot.

The error also shows up either when downloading a spatvector or creating a new one:

library(terra)
library(tidyterra)
library(ggplot2)

GER <- geodata::gadm(country=c("Germany"),level=0,path=tempdir())
ggplot(GER) + geom_spatvector()

or:

library(terra)
library(tidyterra)
library(ggplot2)

points <- data.frame(GridID = c(1,2,3,4,5,6), Lat = c(53.8900,53.8900,53.8900,53.8900,53.8900,53.9125), Long = c(14.2425,14.2875,14.3325,14.3775,14.4225,11.2275))
grids <- terra::vect(x = points, geom = c("Long","Lat"), crs = "+proj=longlat +datum=WGS84 +no_defs +type=crs")
ggplot(grids) + geom_spatvector()
Error: node stack overflow In addition: There were 50 or more warnings (use warnings() to see the first 50) Error during wrapup: node stack overflow Error: no more error handlers available (recursive errors?); invoking 'abort' restart

If I try to store the ggplot in a variable, I get an additional warning:

g1 <- ggplot(grids) + geom_spatvector()

In CPL_crs_from_input(x) : GDAL Error 1: PROJ: proj_create_from_database: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.

It is working in my local machine (Windows R 4.3.3) and in all the test I put together., that tests this in several platforms with different R versions.

It seems to me that this is realted with GDAL rather than tidyterra. Could you please uninstall sf and terra, re-install them and re-check?

In my machine_

library(terra)
#> terra 1.7.71
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggplot2)

GER <- geodata::gadm(country = c("Germany"), level = 0, path = tempdir())
ggplot(GER) +
  geom_spatvector()

points <- data.frame(
  GridID = c(1, 2, 3, 4, 5, 6),
  Lat = c(53.8900, 53.8900, 53.8900, 53.8900, 53.8900, 53.9125),
  Long = c(14.2425, 14.2875, 14.3325, 14.3775, 14.4225, 11.2275)
)

grids <- terra::vect(
  x = points,
  geom = c("Long", "Lat"),
  crs = "+proj=longlat +datum=WGS84 +no_defs +type=crs"
)


ggplot(grids) +
  geom_spatvector()

Created on 2024-03-12 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29 ucrt)
#>  os       Windows 11 x64 (build 22631)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Spanish_Spain.utf8
#>  ctype    Spanish_Spain.utf8
#>  tz       Europe/Madrid
#>  date     2024-03-12
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  class         7.3-22  2023-05-03 [1] CRAN (R 4.3.1)
#>  classInt      0.4-10  2023-09-05 [1] CRAN (R 4.3.1)
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.2)
#>  codetools     0.2-19  2023-02-01 [2] CRAN (R 4.3.3)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  curl          5.2.1   2024-03-01 [1] CRAN (R 4.3.3)
#>  DBI           1.2.2   2024-02-16 [1] CRAN (R 4.3.2)
#>  digest        0.6.35  2024-03-11 [1] CRAN (R 4.3.3)
#>  dplyr         1.1.4   2023-11-17 [1] CRAN (R 4.3.2)
#>  e1071         1.7-14  2023-12-06 [1] CRAN (R 4.3.2)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.2)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  geodata       0.5-9   2023-10-13 [1] CRAN (R 4.3.1)
#>  ggplot2     * 3.5.0   2024-02-23 [1] CRAN (R 4.3.2)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.2)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.1)
#>  highr         0.10    2022-12-22 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
#>  KernSmooth    2.23-22 2023-07-10 [1] CRAN (R 4.3.1)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.2)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  proxy         0.4-27  2022-06-09 [1] CRAN (R 4.3.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.3.2)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.3.2)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.3.2)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.3.2)
#>  rmarkdown     2.26    2024-03-05 [1] CRAN (R 4.3.3)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.1)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  sf            1.0-15  2023-12-18 [1] CRAN (R 4.3.3)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.1)
#>  terra       * 1.7-71  2024-01-31 [1] CRAN (R 4.3.2)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr         1.3.1   2024-01-24 [1] CRAN (R 4.3.2)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.3.3)
#>  tidyterra   * 0.5.2   2024-01-19 [1] CRAN (R 4.3.3)
#>  units         0.8-5   2023-11-28 [1] CRAN (R 4.3.2)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.2)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.2)
#>  xfun          0.42    2024-02-08 [1] CRAN (R 4.3.2)
#>  xml2          1.3.6   2023-12-04 [1] CRAN (R 4.3.2)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.2)
#> 
#>  [1] C:/Users/diego/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.3/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Hey there!

Thanks for the answer. It still doesn't work (see below). It probably is something related to GDAL, since I keep getting this error/warning when projecting:

GER <- project(GER, "EPSG:3006")
Error: [project] output crs is not valid
In addition: Warning message:
PROJ: proj_create_from_database: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. (GDAL error 1) 

Any ideas on how I could completely remove GDAL and re-install it on Windows?

Here the same errors:

> R.version
               _                                
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          3.1                              
year           2023                             
month          06                               
day            16                               
svn rev        84548                            
language       R                                
version.string R version 4.3.1 (2023-06-16 ucrt)
nickname       Beagle Scouts     

> install.packages(c("sf", "terra"))
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.3/sf_1.0-15.zip'
Content type 'application/zip' length 39200041 bytes (37.4 MB)
downloaded 37.4 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.3/terra_1.7-71.zip'
Content type 'application/zip' length 39567537 bytes (37.7 MB)
downloaded 37.7 MB

package ‘sf’ successfully unpacked and MD5 sums checked
package ‘terra’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\ly60anep\AppData\Local\Temp\RtmpOcZ7ej\downloaded_packages
> library(terra)
terra 1.7.71
Warning message:
package ‘terra’ was built under R version 4.3.3 
> library(tidyterra)

Attaching package: ‘tidyterra’

The following object is masked from ‘package:stats’:

    filter

Warning message:
package ‘tidyterra’ was built under R version 4.3.3 
> library(ggplot2)
Warning message:
package ‘ggplot2’ was built under R version 4.3.3 
> GER <- geodata::gadm(country = c("Germany"), level = 0, path = tempdir())
trying URL 'https://geodata.ucdavis.edu/gadm/gadm4.1/pck/gadm41_DEU_0_pk.rds'
Content type 'unknown' length 2255014 bytes (2.2 MB)
downloaded 2.2 MB

> ggplot(GER) +
+     geom_spatvector()
Error: node stack overflow
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error during wrapup: node stack overflow
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

Hey there!

So, it took a while and some digging, but I solved the problem. Actually, the whole thing happened because of different GDAL installations and where the packages were looking for it.

What I did was just re-set the environmental variable from PROJ_LIB:
Sys.setenv(PROJ_LIB="C:\\Users\\ly60anep\\AppData\\Local\\R\\win-library\\4.3\\terra\\proj")

It was, however, not permanent, what I fixed using:
usethis::edit_r_profile()

It also solved other problems I had related to re-projections, such as:

Error: [project] output crs is not valid
In addition: Warning message:
PROJ: proj_create_from_database: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. (GDAL error 1)