0

I have been using the following to important .shp files into R fine for ages:

require(maptools)
require(rgeos)

shp <- lapply(list.files(pattern="*.shp"), readShapePoly)

If I am loading a handful of files it works fine. However, I am trying to load in ~1000 files and I just get the (common) error message:

Error in getinfo.shape(filen) : Error opening SHP file

I have been searching online but have not been able to find a solution. The working directory is set correctly, the accompanying .dbf .shx and .prj files are present for each shapefile. Also,

list.files(getwd(),pattern="shp")

does return every file. So I'm a bit confused, am I missing something obvious?

Thanks

Nick Crouch
  • 301
  • 3
  • 14

1 Answers1

1

Either change your lapply to a loop or use the options(error=recover) trick to figure out which file is giving you trouble.

R shapefile functions tend to be picky about file extensions, unfortunately. Make sure that all the capitalization on all the different files which collectively make up a shapefile are consistent.

If you think that it's a particular file, you can use try or tryCatch to catch the error.

Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • options(error=recover) is really useful, unfortunately it just lists the first two file sets (1 name with the 4 extensions) as problematic, which work if tested on just their own – Nick Crouch Feb 04 '13 at 15:33
  • 2
    Try it with one file if it works. Try specifying the full path or try specifying the name of the shape file *without the extension* .shp. I remember there were some changes in rgdal and some discussion on the mailing list. – Rainer Feb 04 '13 at 15:45
  • And: which version of R, maptools, all the other packages maptools depends on moght be useful ( sessinInfo() ) Works for me: R version 2.15.2 (2012-10-26) Platform: i686-pc-linux-gnu (32-bit) [1] rgeos_0.2-12 maptools_0.8-22 lattice_0.20-13 sp_1.0-5 [5] foreign_0.8-52 loaded via a namespace (and not attached): [1] tools_2.15.2 – Rainer Feb 04 '13 at 15:51
  • If it's not working for any, maybe it's a path issue. Try `,full.names = TRUE` in `list.files()` . Also try `lapply(list.files(pattern="*.shp")[[1]], readShapePoly)` and see if you can get the same error. If so, compare `list.files(pattern="*.shp")[[1]]` to the character string you know works. – Ari B. Friedman Feb 04 '13 at 16:06
  • 3
    Or stop using `readShapePoly` and use `readOGR` from the `rgdal` package. – Spacedman Feb 04 '13 at 16:30
  • including '[[1]]' works fine and pulls out the first file. 'readOGR' gives me a new error of missing layer. Could this be a memory issue if i'm loading lots of large files, i've seen that people have had issues handling lots of large shapefiles elsewhere. also, the packages are all up to date – Nick Crouch Feb 04 '13 at 17:25
  • Tried converting to loop: `k <- length(list) x <- vector(mode="list",length=k) for (i in 1:k) { x[[i]] <- readShapePoly("*.shp") }` but no luck – Nick Crouch Feb 04 '13 at 19:28
  • `x[[i]] <- readShapePoly("*.shp")` isn't right in your loop. Need a specific filename for each iteration. – Ari B. Friedman Feb 04 '13 at 20:21
  • I have sorted it....a file was corrupted. Annoying. I am ticking the response above because I have noted all of the comments below for future reference – Nick Crouch Feb 04 '13 at 20:35
  • @NickCrouch Retroactively added a suggestion for `try` so that it solves your problem :-) – Ari B. Friedman Feb 04 '13 at 21:07