【问题标题】:readOGR() cannot open filereadOGR() 无法打开文件
【发布时间】:2026-01-21 22:40:01
【问题描述】:
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

【问题讨论】:

    标签: r gdal rgdal ogr


    【解决方案1】:

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

    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::vectsf::st_read

    【讨论】:

    • 谢谢! path.expand() 工作。抱歉,我无法投票给答案。
    • @RiteshJungThapa 你可以接受这个作为正确答案(即使你不能投票)
    • 为什么问题中发布的代码不起作用?我有同样的问题,但文件夹中有多个具有相同名称和不同扩展名的文件,因此path.expand 将不起作用。
    • 它不起作用,因为 readOGR 不使用 path.expand 因为它不知道第一个参数是否实际上是文件路径(它可能是数据库)。没有理由这不适合您。
    • 在我的情况下,上面的代码只有在我添加绝对路径时才有效。使用相对路径会发生相同的错误
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          【解决方案5】:

          语法:库(光栅) s &lt;- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp") 完美运行!托达拉巴!

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

                ######################################
                #             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")
              

              【讨论】:

                【解决方案8】:

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

                .shp — 形状格式

                .shx — 形状索引格式;

                .dbf——属性格式;

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

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

                【讨论】:

                • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center