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

huangapple go评论72阅读模式
英文:

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(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1&quot;), layer=&quot;pixels_de_reference_1&quot;)
rgdal::readOGR(dsn = &quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;,layer = &quot;pixels_de_reference_1&quot;)
rgdal::readOGR(dsn = &quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1&quot;,layer = &quot;pixels_de_reference_1&quot;)
rgdal::readOGR(dsn = &quot;C:/Users/User/Documents/m&#233;moire&quot;,layer = &quot;pixels_de_reference_1&quot;)
rgdal::readOGR(dsn = &quot;pixels_de_reference_1&quot;,layer = &quot;pixels_de_reference_1&quot;)
rgdal::readOGR(dsn = &quot;.&quot;,layer = &quot;pixels_de_reference_1&quot;)
rgdal::readOGR(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;)
rgdal::readOGR(dsn=path.expand(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;))

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

with terra:

f &lt;- system.file(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;, package=&quot;terra&quot;)
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] &quot;&quot;

Even though I get :

#[1] TRUE
list.files(&quot;C:/Users/User/Documents/m&#233;moire/&quot;)
# [1] &quot;ok analyse orthomosa&#239;que.R&quot;            &quot;pixels_de_reference_1.cpg&quot;            
# [3] &quot;pixels_de_reference_1.dbf&quot;             &quot;pixels_de_reference_1.prj&quot;            
# [5] &quot;pixels_de_reference_1.sbn&quot;             &quot;pixels_de_reference_1.sbx&quot;            
# [7] &quot;pixels_de_reference_1.shp&quot;             &quot;pixels_de_reference_1.shx&quot;            
# [9] &quot;python RF ortho rel.py&quot;                &quot;s&#233;curit&#233; code classification image.py&quot;
# [11] &quot;TIFF_ortho.tif&quot;  

And :

SHP &lt;- raster::shapefile(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;)
plot(SHP) 

worked. I've got the same error when I tred:

sf::st_layers(dsn = system.file(SHP , package = &quot;spData&quot;))$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(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;) : 
#  Cannot open data source
sf::st_layers(dsn = system.file(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;, package = &quot;spData&quot;))$name

# Cannot open data source Error: Open failed.
r1_gdb &lt;- file_path_as_absolute(&quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;)
sf::st_layers(dsn = system.file(r1_gdb , package = &quot;spData&quot;))$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 安装在哪里。

正如你所看到的,它返回 &quot;&quot; 作为文件名,这是行不通的。

你只需要提供文件名。

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 &quot;&quot; for a filename, and that is not going to work.

All you need to do is provide the filename.

f &lt;- &quot;C:/Users/User/Documents/m&#233;moire/pixels_de_reference_1.shp&quot;
terra::vect(f)

huangapple
  • 本文由 发表于 2023年2月23日 20:47:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定