如何在R工具中读取.shp文件

时间:2015-02-06 14:27:10

标签: r

link for the .shp file dataset

我想从R工具中读取.shp文件,使用以下脚本:

>scot_LL<-readOGR("C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.shp","scot"),

但是当我运行脚本时,我收到以下错误:

>Error in file(paste(DSN, .Platform$file.sep, layer, ".dbf", sep = ""),  <br/>: 
  cannot open the connection<br/>: 
In addition: Warning message:<br/>: 
In file(paste(DSN, .Platform$file.sep, layer, ".dbf", sep = ""),  :<br/>: 
  cannot open file 'C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.dbf': No such file or directory
你能帮我吗?

1 个答案:

答案 0 :(得分:3)

以下是使用maptools包的解决方案:

library(maptools)

scot_mp <- readShapeSpatial('C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.shp')

proj4string(scot_mp) <- "+proj=longlat +datum=WGS84" # specify projection

plot(scot_mp)

结果:

enter image description here


修改

使用rgdal包的解决方案:

scot_mp <- readOGR('C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot', 'scot')

第一个参数不一定要包含shape文件的名称,但主要问题似乎是至少dbf文件不在你的文件夹中,所有这些都是必需的并且应该是相同的文件夹:scot.shp, scot.dbf, scot.shx。之后,它继续作为另一个解决方案:

proj4string(scot_mp) <- "+proj=longlat +datum=WGS84"
plot(scot_mp)