Sf: Cannot open data source

Created on 14 Mar 2018  路  9Comments  路  Source: r-spatial/sf

I want to read a Shapefile so I can do some analysis on it. This seems like an OGR problem, but I'm not sure how to troubleshoot.

I'm on MacOS.

Error:

Cannot open data source 
Error in CPL_read_ogr(dsn, layer, as.character(options), quiet, type,  : 
  Open failed.

Code:

fname <- system.file("data/tracts/RegionalLabs_CensusTracts_withData.shp", package="sf")
file.exists("data/tracts/RegionalLabs_CensusTracts_withData.shp") # TRUE
nc <- st_read(fname) # Cannot open data source

Is there a way to test whether the other global dependencies like GDAL are set up correctly?

Most helpful comment

The file you're linking to with system.file is not part of sf.
file.exists(fname) should be FALSE hence the error message. However

st_read("data/tracts/RegionalLabs_CensusTracts_withData.shp")

should work.

All 9 comments

$ ogr2ogr --version
GDAL 2.2.3, released 2017/11/20

Are the other files in the same directory (same name, extensions .prj, .shp, .dbf, .shx)?

Hi @edzer thanks for follow-up, yes they are:

RegionalLabs_CensusTracts_withData.cpg
RegionalLabs_CensusTracts_withData.dbf
RegionalLabs_CensusTracts_withData.prj
RegionalLabs_CensusTracts_withData.sbn
RegionalLabs_CensusTracts_withData.sbx
RegionalLabs_CensusTracts_withData.shp
RegionalLabs_CensusTracts_withData.shx

馃 maybe there's a file in there that is throwing it off (having a sudden flashback). FWIW, using a GeoJSON yielded the same error.

More info:

> library('sf')
Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

The file you're linking to with system.file is not part of sf.
file.exists(fname) should be FALSE hence the error message. However

st_read("data/tracts/RegionalLabs_CensusTracts_withData.shp")

should work.

@tim-salabim whoa! That was it. I was originally expecting that, but I was blindly following guides here: https://r-spatial.github.io/sf/articles/sf2.html#using-st_read.

library(sf)
## Loading required package: methods
## Linking to GEOS 3.5.1, GDAL 2.2.1, proj.4 4.9.3
fname <- system.file("shape/nc.shp", package="sf")
fname
## [1] "/home/edzer/R/x86_64-pc-linux-gnu-library/3.4/sf/shape/nc.shp"
nc <- st_read(fname)
## Reading layer `nc' from data source `/home/edzer/R/x86_64-pc-linux-gnu-library/3.4/sf/shape/nc.shp' using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## epsg (SRID):    4267
## proj4string:    +proj=longlat +datum=NAD27 +no_defs

The file you're linking to with system.file is not part of sf.

Not sure I follow. This is a much better API though! Thanks everyone :)

OH. I think I understand now. That is looking for an example shapefile within the package. fname was also always blank. But file.exists separately returned true.

Thanks again!

nc.shp is an example file that ships with the package. As such we need system.file to create the file path to the file in your library path (differs by machine). For local files where the path is known a simple st_read("path/to/file.ship") or other supported file formats is sufficient.

I have got the same problem.
The reason is that i used a chinese OS, which means that in the PATH include chinese, eventhough I used path lile this './data'.
I solved this problem by changing the language of my OS to english.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

duleise picture duleise  路  3Comments

dpprdan picture dpprdan  路  4Comments

thiagoveloso picture thiagoveloso  路  3Comments

tiernanmartin picture tiernanmartin  路  3Comments

kendonB picture kendonB  路  4Comments