问题
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.
回答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.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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)
回答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
回答7:
Both the person who asked this question and this one as well: readOGR() cannot open file were using files from naturalearthdata.com. I am posting this answer because so many people use the site to get base layer data. Something is quirky with at least the two layers I downloaded. The shapefiles showed up fine when queried in r with getinfo.shape, but readOGR failed. The following steps worked for me but require access to ESRI software.
Open the file(s) in ArcMap. They opened with no problem.
Export the shapefile to a geodatabase layer.
Back in r, import the layer from the geodatabase. There are instructions here: https://gis.stackexchange.com/questions/151613/how-to-read-feature-class-in-file-geodatabase-using-r
After that the imported spatial data worked fine in r.
来源:https://stackoverflow.com/questions/21614443/error-when-opening-shapefile