无法在R中打开shapefile

时间:2018-11-25 22:02:09

标签: r rgdal

我不确定为什么,但是我根本无法在r中读取shapefile!

在MacOS Mojave上,

我尝试过:

afg <- readOGR("afg.shp")
afg <- readOGR(dsn = ".", layer = "afg")

shapefile来自以下网站:http://www.diva-gis.org/gdata,阿富汗公路。

此错误不断发生:

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

但是,此代码有效:

countries <- readOGR("countries.geojson")

有人能弄清楚为什么吗?

2 个答案:

答案 0 :(得分:2)

我在Mojave上,效果很好:

httr::GET(
  url = "http://biogeo.ucdavis.edu/data/diva/rds/AFG_rds.zip",
  httr::write_disk("AFG_rds.zip"),
  httr::progress()
) -> res

fils <- unzip("AFG_rds.zip", exdir = "afg-roads")

fils
## [1] "afg-roads/AFG_roads.dbf" "afg-roads/AFG_roads.prj" 
## [3] "afg-roads/AFG_roads.shp" "afg-roads/AFG_roads.shx"

rgdal::readOGR("afg-roads/AFG_roads.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/bob/Development/afg-roads/AFG_roads.shp", layer: "AFG_roads"
## with 1409 features
## It has 5 fields
## class       : SpatialLinesDataFrame 
## features    : 1409 
## extent      : 60.6075, 74.67168, 29.38582, 38.48983  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## variables   : 5
## names       :     MED_DESCRI,    RTT_DESCRI, F_CODE_DES, ISO,  ISOCOUNTRY 
## min values  :        Unknown, Primary Route,       Road, AFG, AFGHANISTAN 
## max values  : Without Median,       Unknown,      Trail, AFG, AFGHANISTAN 

答案 1 :(得分:0)

我也面临着同样的问题。不要尝试重命名文件。解压缩后,您将获得许多文件,其中一个将是shapefile。就我而言,所有这些文件都具有相同的名称,但扩展名不同。然后做你已经在做的事情:

afg <- readOGR(dsn = ".", layer = "AFG_roads")

希望这会起作用。