readOGR() cannot open file

给你一囗甜甜゛ 提交于 2019-12-17 17:44:27

问题


wmap <- readOGR(dsn="~/R/funwithR/data/ne_110m_land", layer="ne_110m_land")

This code is not loading the shape file and error is generated as

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
Cannot open file

I am sure that the directory is correct one. At the end / is also not there and layer name is also correct.

Inside the ne_110m_land directory files I have are:

ne_110m_land.dbf
ne_110m_land.prj
ne_110m_land.shp
ne_110m_land.shx
ne_110m_land.VERSION.txt
ne_110m_land.README.html

回答1:


You could have shown that you have the right path with:

list.files('~/R/funwithR/data/ne_110m_land', pattern='\\.shp$')
file.exists('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp')

perhaps try:

readOGR(dsn=path.expand("~/R/funwithR/data/ne_110m_land"), layer="ne_110m_land")

or a simpler alternative that is wrapped around that:

library(raster)
s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")



回答2:


For me, the command returned the Cannot open layer error when I included the dsn and layer tags.

So when I included it all just as readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp') it worked.

Note that my file was a gjson, so I've only seen this with readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.gjson')




回答3:


Here's what worked for me (with a real example)

require(rgdal)
shape <- readOGR(dsn = "1259030001_ste11aaust_shape/STE11aAust.shp", layer = "STE11aAust")

The exact data is available here (download the .zip file called 'State and Territory ASGC Ed 2011 Digital Boundaries in MapInfo Interchange Format')




回答4:


As I commented in other post (Error when opening shapefile), using file.choose() and selecting manually will help in the case one file selection is needed. Apparently is related with NaturalEarth shapefiles




回答5:


I had the same error. To read in a shapefile, you need to have three files in your folder: the .shp, .dbf and .shx files.



来源:https://stackoverflow.com/questions/30561889/readogr-cannot-open-file

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