Error when opening shapefile

我只是一个虾纸丫 提交于 2019-12-05 11:52:36

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.

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.

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.

cartmangreko

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.

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)
Kita Romero-Álvarez

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 

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!