Search code examples
rleafletrgdalogr

readOGR cannot open layer error


I'm going through Leaflet tutorial and I have stumbled upon file loading error which reads as follows:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open data source

The code that leads to this:

library(rgdal)
countries <- readOGR("./json/countries.geojson", "OGRGeoJSON")

I have saved countries.geojson file in in json directory and set working directory to the directory that contains json directory.

I've tried to load the file by downloading it from Github:

countries <- readOGR("https://raw.githubusercontent.com/datasets/geoboundaries-world-110m/master/countries.geojson", layer = "OGRGeoJSON")

and I got this error:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = 
use_iconv, : Cannot open layer

Solution

  • Try altering the layer name to the filename without the extension:

    countries <- readOGR("https://raw.githubusercontent.com/datasets/geoboundaries-world-110m/master/countries.geojson", layer = "countries")
    

    For some reason in some environments the layer needs to be called "OGRGeoJSON", in other it needs to be called the filename without the extension.

    For example with the latest R and rgdal versions on my OSX, it requires "OGRGeoJSON", but on our production machine running CentOS, it requires the filename. I suspect this is something to do with different versions of the underlying gdal C libraries.