readOGR()无法打开文件

Rit*_*apa 25 r gdal ogr rgdal

wmap <- readOGR(dsn="~/R/funwithR/data/ne_110m_land", layer="ne_110m_land")
Run Code Online (Sandbox Code Playgroud)

此代码未加载形状文件,并生成错误

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
Cannot open file
Run Code Online (Sandbox Code Playgroud)

我确信该目录是正确的.最后/也不存在,图层名称也正确.

在ne_110m_land目录文件中我有:

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
Run Code Online (Sandbox Code Playgroud)

Rob*_*ans 52

你可以证明你有正确的道路:

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

也许试试:

readOGR(dsn=path.expand("~/R/funwithR/data/ne_110m_land"), layer="ne_110m_land")
Run Code Online (Sandbox Code Playgroud)

或者更简单的替代方案:

library(raster)
s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!path.expand() 工作。对不起,我不能投票给答案。 (2认同)

Tom*_*gan 8

对我来说,Cannot open layer当我包含dsnlayer标签时,该命令返回了错误。

所以当我把它全部包括在内时 readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp')

请注意,我的文件是一个 gjson,所以我只看到了这个 readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.gjson')


小智 7

我有同样的错误。要读入 shapefile,您的文件夹中需要有三个文件:.shp、.dbf 和 .shx 文件。