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

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

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

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

在我拥有的 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
4

8 回答 8

62

您可以通过以下方式证明您拥有正确的道路:

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

也许尝试:

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

或者一个更简单的替代方案:

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

更新:

rgdal已经发生了一些变化,您不再需要将路径和图层分开(至少对于某些格式)。所以你可以做

x <- readOGR("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")

(也许仍在使用 path.expand)

另外,如果你还在使用readOGR,你就有点落后了。最好使用terra::vector sf::st_read

于 2015-05-31T19:53:10.450 回答
8

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

因此,当我将其全部包含在内时,就像 readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp') 它起作用一样。

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

于 2018-06-07T01:37:31.240 回答
8

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

于 2019-10-24T08:12:56.667 回答
1

这对我有用(有一个真实的例子)

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

确切数据可在此处获得(下载名为“State and Territory ASGC Ed 2011 Digital Boundaries in MapInfo Interchange Format”的 .zip 文件)

于 2019-02-24T04:55:58.930 回答
1

语法: library(raster) s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")完美运行!托达拉巴!

于 2020-08-06T12:45:00.460 回答
0

正如我在其他帖子(打开 shapefile 时出错)中评论的那样,在需要选择一个文件的情况下,使用 file.choose() 并手动选择将有所帮助。显然与 NaturalEarth shapefiles 有关

于 2019-05-31T15:53:22.263 回答
0

在我看来,这是解决方案,至少在将其上传到云端之前

  ######################################
  #             Server
  ######################################
  #I tell R where to extract the data from
  #Le digo al R donde debe jalar la data

  dirmapas <- "E:/Tu-carpeta/Tu-sub-carpeta/ESRI" #Depende donde tengas tú tus 
                                                  #archivos de cartografía 

  setwd(dirmapas)

  #The raw map
  # El mapa de polígonos en blanco y negro
  departamentos<-readOGR(dsn="BAS_LIM_DEPARTAMENTO.shp", layer="BAS_LIM_DEPARTAMENTO")
于 2020-12-08T17:06:53.127 回答
0

强制文件应该都在同一个目录中

.shp — 形状格式

.shx — 形状索引格式;

.dbf — 属性格式;

然后我们可以将路径作为参数提供给它将起作用的函数。

global_24h =readOGR('/Users/m-store/Desktop/R_Programing/global_24h.shp')

于 2021-09-12T17:00:38.870 回答