4

I am trying to open a shapefile in R, but I am getting the following error message:

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

I have checked other responses and most problems seem to have been solved by ensuring that the .dbf and .shx files are in the same folder. I have them all in the same folder (along with some other extensions too) but I still get the error message. I work on a mac. This is my code:

getinfo.shape("/Users/Suz/Desktop/DWH satellite maps/20100517_Composite.shp")

I have tried it without the .shp extension, and with other commands, such as readShapePoints etc. Nothing has worked so far. Please help, I am new to R and making maps, and after extensive Googling and forum-reading I am still stuck.

jbaums
  • 25,349
  • 5
  • 72
  • 114
user3281487
  • 97
  • 1
  • 1
  • 8
  • Does `readOGR("/Users/Suz/Desktop/DWH satellite maps", "20100517_Composite")` work? (`readOGR` is in the `rgdal` package). – jbaums Feb 06 '14 at 21:32
  • Also, can you please include in your post the output of `list.files('/Users/Suz/Desktop/DWH satellite maps')`? Secondly, could you please try with a trusted shapefile, e.g. [here](http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_1_states_provinces.zip). Finally, are you aware that `getinfo.shape` only reads the header of the shapefile, and doesn't actually read in the data - is that what you intended? – jbaums Feb 06 '14 at 22:24
  • I tried the shapefile that you suggested: getinfo.shape("/Users/Suz/Desktop/ne_110m_admin_1_states_provinces.shp") and still got the same error message. I am starting to think that my problem is something stupid and I don't have all the necessary libraries open or something..? I have sp, map tools, rgdal, and rgeos. – user3281487 Feb 07 '14 at 00:42
  • 1
    The list.files command gives all the files in the directory (which there are 174 of) so the whole output is huge to post. But my files are there. – user3281487 Feb 07 '14 at 00:49
  • I got it to work, somehow. Thanks for your advice! – user3281487 Feb 07 '14 at 00:58
  • 1
    Good to hear. If you can work out what you were doing wrong, please add your solution as an answer and accept it for the benefit of future generations. – jbaums Feb 07 '14 at 01:00

6 Answers6

1

I just had the same problem. Often come other files with your SHP-file. If they are missing the file cant be loaded.

So search if there are any other file ext. with "20100517_Composite" at the source you got your file.

Cant comment yet but i wanted the ppl to save time, if this is the problem.

Andre Elrico
  • 8,959
  • 1
  • 37
  • 61
0

You could try getinfo.shape(file.choose()) to select the file via a pop up window. If this works then it is probably an issue with your input string.

Note: I'm on linux, but I think file.choose() should work for mac.

jprockbelly
  • 1,372
  • 12
  • 25
0

After having the same issue, I did some digging and found a nice thread [here].1 Turns out that after checking the list.files() command and found that my files were not there, and even though I had included the file path in my original code, it still produced the error shown in the question. I then moved all the files into the working directory and then the command below worked.

readShapeSpatial()

Also simply changing the wd would work as well.

setwd("directory_path")

I figured I'd put this here as @jbaums suggested because it would have saved me some time in solving this issue.

Community
  • 1
  • 1
Dan Johnson
  • 67
  • 1
  • 7
0

I had the same problem until i removed the .shp extension.

So instead of

readShapeSpatial("/Users/Suz/Desktop/DWH satellite maps/20100517_Composite.shp")

go with

readShapeSpatial("/Users/Suz/Desktop/DWH satellite maps/20100517_Composite")

If you have all the files in the working directory it should work like a charm.

skuntsel
  • 11,346
  • 11
  • 41
  • 64
0

The easy way to read a shapefile in R is

either (to get a Spatial*) object

library(raster)
x <- shapefile("/Users/Suz/Desktop/DWH satellite maps/20100517_Composite.shp")

or (to get a sf object)

library(sf)
st_read("/Users/Suz/Desktop/DWH satellite maps/20100517_Composite.shp")

(but do not use the deprecated (incomplete and obsolete) function readShapeSpatial)

In action:

library(raster)
library(sf)

f <- system.file("external/lux.shp", package="raster")
s1 <- shapefile(f) 
s2 <- st_read(f)

If this does not work, you need to check if your file exists:

file.exists(f)

To get a list of shapefiles in a directory, you can do

path <- "c:/temp"  # change with your directory name
ff <- list.files(path, pattern='\\.shp$', full.names=TRUE)
Robert Hijmans
  • 24,233
  • 3
  • 36
  • 43
0

This is still a problem. I solve it more directly calling the shapefile using file.choose() and manually selecting the file. Hope this helps for anyone.

library (rgdal)
a = readOGR (file.choose()) #then selecting the shape file manually 
adiga
  • 28,937
  • 7
  • 45
  • 66