'Error in CPL_transform(x, crs, aoi, pipeline, reverse): OGRCreateCoordinateTransformation() returned NULL: PROJ available?

I am using ubuntu 18.04 and the following code is generating an error

library(sf)
library(tmap)
library(dplyr)
library(raster)
#sudo apt install libproj-dev
#devtools::install_github("robinlovelace/geocompr")
library(spDataLarge)
if(!file.exists("e.tif"))
  download.file("https://github.com/geocompr/geocompkg/releases/download/0.1/e.tif",
                "e.tif")
elev = raster("e.tif")
urban = spData::urban_agglomerations %>% 
  filter(year == 2030) %>% 
  dplyr::select(population_millions) 
summary(urban)

tm_shape(elev) +
  tm_raster(breaks = c(-10000, 0, 10, 50, 100, 10000)) +
  tm_shape(urban) +
  tm_dots(size = "population_millions", scale = 0.5)

I get the following error

Error in CPL_transform(x, crs, aoi, pipeline, reverse) : 
  OGRCreateCoordinateTransformation() returned NULL: PROJ available?
In addition: Warning message:
In CPL_transform(x, crs, aoi, pipeline, reverse) :
  GDAL Error 1: No PROJ.4 translation for source SRS, coordinate transformation initialization has failed.

If I update PROJ using

sudo apt-get install proj-bin

It says I have the most recent version

proj-bin is already the newest version (5.2.0-1~bionic0).

Any help would be appreciated.



Solution 1:[1]

Thanks to the developers of sf I now know the solution (and the problem):

See the github issue here.

Recap:

  • If you save an sf-dataframe with a newer version of GDAL, and then try st_transform on a system with an older version of GDAL, the projection info cannot be read properly (at least in the version that ships on Ubuntu 18 - which is also where I ran into this problem).

  • The solution is to re-set the projection:

st_crs(data) <- 4326  # or whatever projection your data is in

If you have multiple geometry columns you may need to set it separately:

st_crs(data$areacolumn) <- 4326

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remko Duursma