16

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.

David Hammond
  • 161
  • 1
  • 3
  • 2
    have you solved this? I have a similar issue. – Ariel May 13 '20 at 19:12
  • 2
    Also interested and having a similar issue. A shiny app that uses spatial libraries that works locally but not after I deploy the app. The app log tells me it fails with that same error message. – Kmcd39 May 20 '20 at 22:22
  • 1
    I had the same issue with a shapefile when I was trying to plot it interactively with tmaptools::ttm(). This is not a solution, I went around the problem by removing the CRS string from the shapefile. Like this: shp_copy <- shp; raster::crs(shp_copy) <- "". – Shepherd May 21 '20 at 13:07
  • 1
    I alsohave the problem that a shiny app works locally but not remote (on ubuntu shiny server), AND the latest proj-bin is already installed.... – Remko Duursma Jun 07 '20 at 19:56
  • 1
    I have the same issue on shinyapps.io. The application works locally. Any solution? – Ahmed El-Gabbas Jun 15 '20 at 15:44

1 Answers1

28

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
Remko Duursma
  • 2,741
  • 17
  • 24
  • 1
    Wow thanks for finding that. I just started getting this error after running a script that worked yesterday. The difference is I sent a file remotely to my big desktop to crunch, and it does have different versions of a couple packages. A different version of sf between the two computers was also causing all sorts of problems, so now I will look ad GDAL as well! – Dan Slone Sep 03 '20 at 17:50
  • Indeed - thanks for the solution. I had this problem not just with datasets, but with CRS created and stored as package data (eg `crs_longlat <- st_crs(4326)`). Switching to saving them as strings instead was the solution, strings being accepted everywhere I was passing the CRS as parameters (eg `crs_longlat <- "EPSG:4326"`). – Scrope Dec 01 '21 at 06:09