0

I've used the read_shape() function from the tmaptools package to import shapefiles many times successfully, but I've now encountered a strange error.

For some shapefiles the read_shape() command yields message:

"Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Layer not found"

For other shapefiles, I have no problem at all. Here is an example of an open data shapefile for which I am getting this error: https://opendata.arcgis.com/datasets/3605212dc7f0477cacb4cca884487e3d_0.zip?outSR=%7B%22wkid%22%3A32054%2C%22latestWkid%22%3A32054%7D

I believe the error may be related to the tmap package because I am able to open the shapefiles in question with no issues in QGIS. I'm running the current versions of RStudio, tmap, and tmaptools.

Any ideas?

Phil
  • 4,344
  • 2
  • 23
  • 33
John J.
  • 1,450
  • 1
  • 13
  • 28
  • The error is a common one with `rgdal::readOGR()` (which is actually what's loading the shapefile; `tmaptools::read_shape()` is just a wrapper). Often it's because the `dsn` (i.e. the directory) argument is incorrectly specified. [This](http://zevross.com/blog/2016/01/13/tips-for-reading-spatial-files-into-r-with-rgdal/) and [this](http://stackoverflow.com/questions/22289794/read-shape-file-with-readogr-verses-readshapepoly) might help. – Phil Feb 12 '17 at 20:44
  • Thanks to @Phil for pointing me on the right track. `tmaptools::read_shape()` is sometimes confused when the name of the directory storing the .shp file and its companion files is located. By leaving the name of the folder unchanged, this error is avoided. – John J. Apr 07 '17 at 17:16

1 Answers1

0

After experiencing this error in multiple situations, I've found that the only consistent fix is to use a different tool.

Now I use the "shapefile" command from the raster package.

So,

 #install.packages("raster")
 library(raster)
 shapefile("~/YOURFILEPATHHERE")

This will read the shapefile into your session as a SpatialPolygonsDataFrame, which can then be used by the tmap (or similar) package.

John J.
  • 1,450
  • 1
  • 13
  • 28