【问题标题】:Read shape file with readOGR verses readShapePoly使用 readOGR 和 readShapePoly 读取形状文件
【发布时间】:2014-04-12 22:39:17
【问题描述】:

我已经使用maptools 包中的readShapePoly 读取了一个shapefile,但无法使用readOGR 读取同一个文件。我希望有人可以通过readOGR 帮助我阅读 shapefile。

我从这里下载了文件orcounty.shphttp://geography.uoregon.edu/geogr/topics/maps.htm

我还下载了相关文件:orcounty.shxorcounty.sbxorcounty.sbnorcounty.dbf,并将所有五个文件放在文件夹中:c:/users/mark w miller/gis_in_R/shapefile_example/

以下代码读取 shapefile 并显示一些属性:

library(maptools)

setwd('c:/users/mark w miller/gis_in_R/shapefile_example/')

# Oregon county census data (polygons)
orcounty.poly <- readShapePoly('orcounty.shp', proj4string=CRS("+proj=longlat"))
orcounty.line <- readShapeLines('orcounty.shp', proj4string=CRS("+proj=longlat"))

# see projection
summary(orcounty.poly)

Object of class SpatialPolygonsDataFrame
Coordinates:
         min        max
x -124.55840 -116.46944
y   41.98779   46.23626
Is projected: FALSE 
proj4string : [+proj=longlat]
Data attributes:

但是,当我尝试使用以下代码读取同一个 shapefile 时,我收到一个错误:

library(rgdal)

# read shapefile
oregon.map <- readOGR(dsn="c:/users/mark w miller/gis_in_R/shapefile_example/", layer="orcounty")

# convert to dataframe
oregon.map_df <- fortify(oregon.map)

错误信息说:

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

我可以阅读 Natural Earth http://www.naturalearthdata.com/ shapefiles 使用:

library(rgdal)

setwd("c:/users/mark w miller/gis_in_R/")

# read shapefile
wmap <- readOGR(dsn="ne_110m_physical", layer="ne_110m_land")

因此,自然地球 shapefile 和俄勒冈 shapefile orcounty.shp 之间显然存在差异。

感谢您就如何阅读 orcounty.shpreadOGR 提供任何建议。我的问题类似于这里的问题:rgdal / readOGR - unable to read shapefile from .zip

【问题讨论】:

  • 可以正常打开。试试readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example', layer = 'orcounty')
  • @PauloCardoso 成功了!我不敢相信解决方案如此简单。如果您想将其发布为答案,我可以接受,或者我可以删除问题。

标签: r shapefile rgdal


【解决方案1】:

尝试从文件路径中删除最后一个“/”。

readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example',
        layer = 'orcounty')

【讨论】:

    【解决方案2】:

    我使用了文件 ne_110m_land

    试试这个:

    setwd('D:/JMSR/codes.R/mapas')
    unzip("ne_110m_land.zip")
    ogrInfo(".", "ne_110m_land")
    wmap <- readOGR(".", "ne_110m_land")
    

    【讨论】:

      【解决方案3】:

      对于任何在 Linux 机器上遇到此错误的人,我发现问题出在使用主路径快捷方式。即

      # Works
      readOGR(dsn="/home/user/dir", layer="file")
      
      # Doesn't work
      readOGR(dsn="~/dir", layer="file")
      

      我不知道为什么。

      【讨论】:

      • 波浪线表达式 (~) 通常由 Unix shell 扩展。如果您将它传递给 R 库调用,它可能会按字面意思阅读。
      • 好的,谢谢。它通常在 R 中工作,用于读取文件、设置工作目录。在这种情况下它没有。
      【解决方案4】:

      raster::shapefile 环绕 readOGR 以处理路径和波浪号;只需传递完整的文件名。

       library(raster)
       x <- shapefile("c:/users/orcounty.shp')
      

       y <- shapefile("~/users/orcounty.shp")
      

      【讨论】:

        猜你喜欢
        • 2016-11-10
        • 2018-05-11
        • 2021-12-05
        • 1970-01-01
        • 2011-01-05
        • 2017-11-18
        • 2012-08-18
        • 2015-11-24
        • 2019-10-27
        相关资源
        最近更新 更多