无法在 R 中打开 shapefile

Cannot open shapefile in R

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

在 MacOS Mojave 上,

我试过:

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

形状文件来自此网站: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")

有人能知道为什么吗?

我在 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 

我也遇到了同样的问题。不要尝试重命名文件。解压后,你会得到一些文件,其中一个是shapefile。就我而言,所有这些文件都具有相同的名称但扩展名不同。然后就做你已经在做的事情:

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

希望这会奏效。