31
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
Taher Ahmed Ghaleb
  • 4,558
  • 5
  • 25
  • 37
Ritesh Jung Thapa
  • 967
  • 2
  • 11
  • 18

7 Answers7

57

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")
Robert Hijmans
  • 24,233
  • 3
  • 36
  • 43
  • 2
    thanks! path.expand() worked. Sorry that I can't vote up the answer. – Ritesh Jung Thapa May 31 '15 at 19:59
  • @RiteshJungThapa you can accept this as the correct answer (even if you can't upvote) – geneorama Jan 08 '16 at 18:52
  • Why is the code posted in the question not working? I have the same issue but multiple files with same name and different extension in the folder, thus `path.expand` won't work. – Dambo Apr 21 '16 at 23:58
  • It does not work because readOGR does not use `path.expand` because it does not know if the first argument actually is a file path (it could be a database). There is no reason why this should not work for you. – Robert Hijmans Apr 22 '16 at 16:11
  • In my case the codes above work only if I add the absolute path . with relative path the same error occurs – JessicaRjes Mar 09 '20 at 14:40
8

For me, the command returned the Cannot open layer error when I included the dsn and layer tags.

So when I included it all just as readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp') it worked.

Note that my file was a gjson, so I've only seen this with readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.gjson')

Tom Logan
  • 301
  • 3
  • 12
7

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.

E. Vr
  • 71
  • 1
  • 1
1

Here's what worked for me (with a real example)

require(rgdal)
shape <- readOGR(dsn = "1259030001_ste11aaust_shape/STE11aAust.shp", layer = "STE11aAust")

The exact data is available here (download the .zip file called 'State and Territory ASGC Ed 2011 Digital Boundaries in MapInfo Interchange Format')

stevec
  • 15,490
  • 6
  • 67
  • 110
1

the syntax: library(raster) s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp") worked perfectly! todah rabah!

con
  • 4,303
  • 5
  • 19
  • 42
0

As I commented in other post (Error when opening shapefile), using file.choose() and selecting manually will help in the case one file selection is needed. Apparently is related with NaturalEarth shapefiles

0

it seems to me that this is the solution, at least before uploading it to the cloud

  ######################################
  #             Server
  ######################################
  #I tell R where to extract the data from
  #Le digo al R donde debe jalar la data

  dirmapas <- "E:/Tu-carpeta/Tu-sub-carpeta/ESRI" #Depende donde tengas tú tus 
                                                  #archivos de cartografía 

  setwd(dirmapas)

  #The raw map
  # El mapa de polígonos en blanco y negro
  departamentos<-readOGR(dsn="BAS_LIM_DEPARTAMENTO.shp", layer="BAS_LIM_DEPARTAMENTO")