Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readOGR() cannot open file

Tags:

r

gdal

rgdal

ogr

wmap <- readOGR(dsn="~/R/funwithR/data/ne_110m_land", layer="ne_110m_land")

This code is not loading the shape file and error is generated as

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

I am sure that the directory is correct one. At the end / is also not there and layer name is also correct.

Inside the ne_110m_land directory files I have are:

ne_110m_land.dbf
ne_110m_land.prj
ne_110m_land.shp
ne_110m_land.shx
ne_110m_land.VERSION.txt
ne_110m_land.README.html
like image 506
Ritesh Jung Thapa Avatar asked May 31 '15 19:05

Ritesh Jung Thapa


2 Answers

You could have shown that you have the right path with:

list.files('~/R/funwithR/data/ne_110m_land', pattern='\\.shp$')
file.exists('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp')

perhaps try:

readOGR(dsn=path.expand("~/R/funwithR/data/ne_110m_land"), layer="ne_110m_land")

or a simpler alternative that is wrapped around that:

library(raster)
s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")

Update:

rgdal has changed a bit and you do not need to separate the path and layer anymore (at least for some formats). So you can do

x <- readOGR("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")

(perhaps still using path.expand)

Also, if you are still using readOGR you are a bit behind the times. It is better to use terra::vect or sf::st_read.

like image 84
Robert Hijmans Avatar answered Oct 04 '22 04:10

Robert Hijmans


I had the same error. To read in a shapefile, you need to have three files in your folder: the .shp, .dbf and .shx files.

like image 12
E. Vr Avatar answered Oct 03 '22 04:10

E. Vr