3

I cannot successfully read a shapefile in R.

I'm using Rstudio version 0.99.902

I have read several articles on readOGR errors that concern reading a shapefile.

My shapefile is error free, as it opens in qGIS. when I check prescence of file, file.exists returns TRUE yet I get a "Cannot open layer" error:

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

Any advice?

  • Does your file path for dsn have a "/" at then end? Remove it – mdsumner Jul 15 '16 at 14:28
  • the dsn does not have a "/" still doesn't work! very frustrating – Ben Jul 15 '16 at 14:58
  • Have you tried with other packages to read a shapefile ? – gene Jul 15 '16 at 15:14
  • 1
    Please click 'edit' and add your code! – Simbamangu Jul 15 '16 at 16:10
  • 1
    Can you provide more documentation, in the form of your actual readOGR() call? And maybe the paths of your files. – the_skua Jul 15 '16 at 19:01
6

It took me a bit to get this too.

# shapefile path example:
# C:/Users/User/GIS/MyShapefile.shp

# OGR command:
library(rgdal)
readOGR("C:/Users/User/GIS", "MyShapefile")

You have to leave off the ".shp" extension as well as the last forward slash on the path.

  • I wonder if the errors output of readOGR could be modified to flag this issue specifically?? – haff Nov 23 '16 at 19:57
  • Or modified to fix the input. I wonder in what proportion users use the various file formats available to ogr. The input schema makes sense but is confusing coming from desktop GIS software. – JMT2080AD Nov 24 '16 at 0:23
  • Looks like the new version of rgdal does what you want. I can call a shapefile by it's full .shp path and it loads fine. I did it by accident and it works! – JMT2080AD Dec 29 '16 at 0:25
  • @JMT_2080AD Can you share your code? Still struggling with readOGR Thanks! :) – Ben Jan 25 '17 at 15:55
  • @Ben send me what you have here and I'll show you what you are doing wrong. As a note, if you update your R instance with the new rgdal library you should be able to load the "xxx.shp" with extension directly. – JMT2080AD Jan 28 '17 at 18:17
1

I also received this error message when reading in a shapefile in QGIS (3.4.3 using the R "Autocor spatiale" tool. Tried running it in R (3.5.2) and had the same issue.

It took me a while to figure this silly one out, but I had my shapefile on a USB (moving between a Mac and PC to make sure a module works for students using both types of OS). Once I migrated the shapefile to my hard disk, it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.