R: readOGR() 无法打开文件
R: readOGR() cannot open file
我从 here 下载了一个 SHAPE 文件。我添加到我的工作目录:
> list.files('/home/lucho/data/EnglandGIS/', pattern='\.shp$')
[1] "england_gor_2011.shp"
> file.exists('/home/lucho/data/EnglandGIS/england_gor_2011.shp')
[1] TRUE
但是,我看不懂:
library("rgdal")
shape <- readOGR(dsn = path.expand("/home/lucho/data/EnglandGIS/england_gor_2011"), layer = "england_gor_2011")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
其他类似问题的答案已被接受,但无济于事。问题是什么?数据是否损坏?我怎么知道? (如果你能下载数据自己试试,那可能是最好的方法)
我在 Ubuntu 16.04 中使用最新的 R 和最新的 Rstudio。
不要忘记在 readOGR
命令中指定形状文件的扩展名:
library("rgdal")
shape <- readOGR(dsn = path.expand("england_gor_2011.shp"),
layer = "england_gor_2011")
#############
OGR data source with driver: ESRI Shapefile
Source: "england_gor_2011.shp", layer: "england_gor_2011"
with 9 features
It has 3 fields
希望对您有所帮助。
虽然这个问题似乎得到了回答,但这里有几个关于如何读取 shapefile 的其他选项:
您还可以尝试 raster
包中的函数 shapefile
:
library(raster)
shp <- shapefile("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
或来自新 sf
包的函数 st_read
(非常有效):
library(sf)
shp <- st_read(system.file("/home/lucho/data/EnglandGIS/england_gor_2011.shp", package="sf"))
要使用 readOGR
导入形状文件,您可以使用
readOGR(dsn = "/home/lucho/data/EnglandGIS/", layer = "england_gor_2011")
其中 dsn
是包含 england_gor_2011.shp
的文件夹(以及其他具有相同名称但扩展名不同的文件,例如 england_gor_2011.dbf
等),或者您可以指定完整路径到形状文件(包括扩展名):
readOGR("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
据我所知,第二种方法不适用于 rgdal
的早期版本。
我从 here 下载了一个 SHAPE 文件。我添加到我的工作目录:
> list.files('/home/lucho/data/EnglandGIS/', pattern='\.shp$')
[1] "england_gor_2011.shp"
> file.exists('/home/lucho/data/EnglandGIS/england_gor_2011.shp')
[1] TRUE
但是,我看不懂:
library("rgdal")
shape <- readOGR(dsn = path.expand("/home/lucho/data/EnglandGIS/england_gor_2011"), layer = "england_gor_2011")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
我在 Ubuntu 16.04 中使用最新的 R 和最新的 Rstudio。
不要忘记在 readOGR
命令中指定形状文件的扩展名:
library("rgdal")
shape <- readOGR(dsn = path.expand("england_gor_2011.shp"),
layer = "england_gor_2011")
#############
OGR data source with driver: ESRI Shapefile
Source: "england_gor_2011.shp", layer: "england_gor_2011"
with 9 features
It has 3 fields
希望对您有所帮助。
虽然这个问题似乎得到了回答,但这里有几个关于如何读取 shapefile 的其他选项:
您还可以尝试 raster
包中的函数 shapefile
:
library(raster)
shp <- shapefile("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
或来自新 sf
包的函数 st_read
(非常有效):
library(sf)
shp <- st_read(system.file("/home/lucho/data/EnglandGIS/england_gor_2011.shp", package="sf"))
要使用 readOGR
导入形状文件,您可以使用
readOGR(dsn = "/home/lucho/data/EnglandGIS/", layer = "england_gor_2011")
其中 dsn
是包含 england_gor_2011.shp
的文件夹(以及其他具有相同名称但扩展名不同的文件,例如 england_gor_2011.dbf
等),或者您可以指定完整路径到形状文件(包括扩展名):
readOGR("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
据我所知,第二种方法不适用于 rgdal
的早期版本。