rshapefiler-sfrgdalterra

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


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.


Solution

  • 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)