r-spatial / discuss

a discussion repository: raise issues, or contribute!
54 stars 12 forks source link

Error in .onAttach code when installing rgdal on Ubuntu 20.04 #54

Closed tbradley1013 closed 2 years ago

tbradley1013 commented 2 years ago

Hello,

I recently upgraded my ubuntu server from 16.04 to 20.04. The server is used to host RStudio Connect and as a result has multiple versions of R installed from source.

I am trying to install the rgdal package (v. 1.5-28) for R 4.1.1. However, every time I run install.packages("rgdal"), I get the following error when it tries to test if the package can be loaded:

** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘rgdal’:
 .onAttach failed in attachNamespace() for 'rgdal', details:
  call: get("has_proj_def.dat", envir = .RGDAL_CACHE) || (PROJis6ormore())
  error: invalid 'x' type in 'x || y'
Error: loading failed
Execution halted
ERROR: loading failed

I have dug a little into the source code for the package, and it looks like the error is occurring on line 107 of the AAA.R file in the .onAttach function for the package. I cloned the cran read-only github repo and put a bunch of cat() calls throughout the .onAttach function to see what values were being returned for these different checks. It looks like the value returned by the PROJis6ormore() function is FALSE. Even though during the package installation checks it finds my proj installation which is currently version 6.3.1

checking for gdal-config... /usr/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 3.0.4
checking GDAL version >= 1.11.4... yes
checking GDAL version <= 2.5 or >= 3.0... yes
checking GDAL: linking with --libs only... yes
checking GDAL: gdal-config data directory readable... yes
checking GDAL: /usr/share/gdal/stateplane.csv readable... yes
configure: pkg-config proj exists, will use it
configure: PROJ version: 6.3.1
configure: PROJ CPP flags: -DPROJ_H_API
configure: PROJ LIBS: -lproj
checking PROJ header API:... yes

When this value is FALSE, it leaves the value of "has_proj_def.dat" in the .RGDAL_CACHE as NULL. Then once it reaches line 107, this NULL value is rejected in the || statement, causing the error that I am getting.

I tried to upgrade my proj and gdal installations to the development server by running the following:

sudo apt remove libgdal-dev
sudo apt remove libproj-dev
sudo apt remove gdal-bin
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable

sudo apt update
sudo apt install libgdal-dev
sudo apt install gdal-bin
sudo apt install libproj-dev

This upgraded me to PROJ version 8.2.0, however, I am still getting the same error when installing this package. I have tried to install it from multiple R installations and versions and I am getting the same error everywhere. I am not sure why the .OnAttach function isn't recognizing my PROJ installation when it is recognized during the package installation checks.

Any ideas or suggestions on how to resolve this issue would be greatly appreciated.

rsbivand commented 2 years ago

rgdal is shortly to be retired latest end 2023, no active maintenance other than on systems the maintainer has access to can be expected (Fedora latest, Windows 10, macOS M1). Can you install sf on the same platform? If yes, simply step away from rgdal and rgeos now (if you are a developer).

rgdal is on R-forge, which is where you may access current source (see the CRAN page https://cran.r-project.org/package=rgdal, the canonical information source for the package). Since your system is not one I have access to, any attempt to patch might be by emailing diffs, or rather by checking out the SVN source, trying the patches locally, and emailing a diff when you succeed. I'll then check them, and if they do not break anything else on any other platform, perhaps include them.

One reason for wondering whether the root problem is not multiple installed PROJ or GDAL external software on your system is that rgdal and sf are checked quite regularly on PROJ < 6 and >=6, so this fix, for a broken PROJ distribution which forgot to include the proj_def.dat file seven years ago, defined in R/AAA.R lines 88-92 and src/projectit.cpp lines 97-129. It might be time to drop the test, because PROJ.4 4.9.1 was released some time ago (march 2015, still listed at https://download.osgeo.org/proj/proj-4.9.1.tar.gz); since the release is still listed, maybe it is not yet time to drop it.

tbradley1013 commented 2 years ago

Thanks for the quick reply. For this particular application I am not the developer, but I have instructed the developer that they will need to update their application to use sf rather than rgdal.

To my knowledge, there is only one PROJ installation on the server. I uninstalled all pacakges identified by dpkg -l | grep proj and dpkg -l | grep gdal and then reinstalled them to no avail. At this point, I don't think I have the bandwidth to try to patch the package for Ubuntu 20.04, although I appreciate your openness to PR and helping me.

I will close this issue since upgrading to sf should eliminate the issue. Thanks!

rsbivand commented 2 years ago

Please get back with an sf issue if that package does not install cleanly, or review existing issues concerning installation problems there. The same would apply for the terra package if your need for rgdal was as a package required by raster.