英文:
r - readOGR(), terra::vect and sf::st_read : cannot open shapefile while file.exists = TRUE and .shp, .dbf and .shx files in the same folder
问题
我不明白为什么会出现错误:"无法打开数据源",尽管我的路径至少曾经正确,而且.shp、.dbf 和.shx 文件都在同一个文件夹中。
我尝试了以下方法:
使用rgdal:
rgdal::readOGR(dsn=path.expand("C:/Users/User/Documents/mémoire/pixels_de_reference_1"), layer="pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire/pixels_de_reference_1",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "pixels_de_reference_1",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = ".",layer = "pixels_de_reference_1")
rgdal::readOGR("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
rgdal::readOGR(dsn=path.expand("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp"))
# 错误信息:Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
# 无法打开数据源
使用terra:
f <- system.file("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp", package="terra")
terra::vect(f)
# 错误:[vect] 文件不存在
我不知道是否正常,但当我打印f时,我有一种文件为空的印象:
f
#[1] ""
尽管我得到了:
file.exists('C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp')
#[1] TRUE
list.files("C:/Users/User/Documents/mémoire/")
# [1] "ok analyse orthomosaïque.R" "pixels_de_reference_1.cpg"
# [3] "pixels_de_reference_1.dbf" "pixels_de_reference_1.prj"
# [5] "pixels_de_reference_1.sbn" "pixels_de_reference_1.sbx"
# [7] "pixels_de_reference_1.shp" "pixels_de_reference_1.shx"
# [9] "python RF ortho rel.py" "sécurité code classification image.py"
# [11] "TIFF_ortho.tif"
还有:
SHP <- raster::shapefile("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
plot(SHP)
运行正常。当我尝试以下操作时,出现了相同的错误:
sf::st_layers(dsn = system.file(SHP , package = "spData"))$name
# 无法打开数据源 Error: Open failed.
此外,当我尝试检查文件名时,我也遇到了相同的错误:
rgdal::ogrListLayers("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
# 错误 in rgdal::ogrListLayers("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp") :
# 无法打开数据源
sf::st_layers(dsn = system.file("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp", package = "spData"))$name
# 无法打开数据源 Error: Open failed.
r1_gdb <- file_path_as_absolute("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
sf::st_layers(dsn = system.file(r1_gdb , package = "spData"))$name
# 无法打开数据源 Error: Open failed.
我的工作目录是包含这些文件的目录。在ArcGIS中,shapefile正常工作。我唯一的线索是可能有一个扩展名缺失在所需的伴随文件中。但我已经拿到了ArcGIS发布的所有文件夹。
英文:
I don't understand why I get the error : "Cannot open data source" while my path should have been at least once correct and that the .shp, .dbf and .shx files are in the same folder.
I've tried :
with rgdal:
rgdal::readOGR(dsn=path.expand("C:/Users/User/Documents/mémoire/pixels_de_reference_1"), layer="pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire/pixels_de_reference_1",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "C:/Users/User/Documents/mémoire",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = "pixels_de_reference_1",layer = "pixels_de_reference_1")
rgdal::readOGR(dsn = ".",layer = "pixels_de_reference_1")
rgdal::readOGR("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
rgdal::readOGR(dsn=path.expand("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp"))
# Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
# Cannot open data source
with terra:
f <- system.file("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp", package="terra")
terra::vect(f)
# Error: [vect] file does not exist:
I don't know if it is normal but when I print f, I have the impression the file is empty:
f
#[1] ""
Even though I get :
#[1] TRUE
list.files("C:/Users/User/Documents/mémoire/")
# [1] "ok analyse orthomosaïque.R" "pixels_de_reference_1.cpg"
# [3] "pixels_de_reference_1.dbf" "pixels_de_reference_1.prj"
# [5] "pixels_de_reference_1.sbn" "pixels_de_reference_1.sbx"
# [7] "pixels_de_reference_1.shp" "pixels_de_reference_1.shx"
# [9] "python RF ortho rel.py" "sécurité code classification image.py"
# [11] "TIFF_ortho.tif"
And :
SHP <- raster::shapefile("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
plot(SHP)
worked. I've got the same error when I tred:
sf::st_layers(dsn = system.file(SHP , package = "spData"))$name
# Cannot open data source Error: Open failed.
Moreover, when I'm trying to check the name of the file I get the same error:
# Error in rgdal::ogrListLayers("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp") :
# Cannot open data source
sf::st_layers(dsn = system.file("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp", package = "spData"))$name
# Cannot open data source Error: Open failed.
r1_gdb <- file_path_as_absolute("C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp")
sf::st_layers(dsn = system.file(r1_gdb , package = "spData"))$name
# Cannot open data source Error: Open failed.
My working directory is the directory containing the files. The shapefile is working properly in Arcgis.
The only clue I have is that maybe one extension is missing in the required accompanying files. But I have taken all the folders relesaed by Arcgis.
答案1
得分: 0
你在使用 system.file
来获取你自己的文件。这是不正确的。system.file
是用于 R 包内置文件的。它有助于找到路径,无论 R 安装在哪里。
正如你所看到的,它返回 ""
作为文件名,这是行不通的。
你只需要提供文件名。
f <- "C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp"
terra::vect(f)
英文:
You are using system.file
for your own file. That is not correct. `system.file is for files that ship with R packages. It helps in finding the path, no matter where R was installed.
As you saw that returned ""
for a filename, and that is not going to work.
All you need to do is provide the filename.
f <- "C:/Users/User/Documents/mémoire/pixels_de_reference_1.shp"
terra::vect(f)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论