user3281487
user3281487

Reputation: 97

Error when opening shapefile

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.

Upvotes: 4

Views: 12987

Answers (6)

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 

Upvotes: 0

Robert Hijmans
Robert Hijmans

Reputation: 47071

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)

Upvotes: 0

Andre Elrico
Andre Elrico

Reputation: 11480

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.

Upvotes: 1

cartmangreko
cartmangreko

Reputation: 9

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.

Upvotes: 0

Dan Johnson
Dan Johnson

Reputation: 77

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.

Upvotes: 0

jprockbelly
jprockbelly

Reputation: 1607

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.

Upvotes: 0

Related Questions