【发布时间】:2014-04-12 22:39:17
【问题描述】:
我已经使用maptools 包中的readShapePoly 读取了一个shapefile,但无法使用readOGR 读取同一个文件。我希望有人可以通过readOGR 帮助我阅读 shapefile。
我从这里下载了文件orcounty.shp:http://geography.uoregon.edu/geogr/topics/maps.htm
我还下载了相关文件:orcounty.shx、orcounty.sbx、orcounty.sbn 和 orcounty.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.shp 和 readOGR 提供任何建议。我的问题类似于这里的问题:rgdal / readOGR - unable to read shapefile from .zip
【问题讨论】:
-
可以正常打开。试试
readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example', layer = 'orcounty') -
@PauloCardoso 成功了!我不敢相信解决方案如此简单。如果您想将其发布为答案,我可以接受,或者我可以删除问题。