Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot open data source #673

Closed
allthesignals opened this issue Mar 14, 2018 · 9 comments
Closed

Cannot open data source #673

allthesignals opened this issue Mar 14, 2018 · 9 comments

Comments

@allthesignals
Copy link

allthesignals commented Mar 14, 2018

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?

@allthesignals
Copy link
Author

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

@edzer
Copy link
Member

edzer commented Mar 14, 2018

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

@allthesignals
Copy link
Author

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.

@allthesignals
Copy link
Author

More info:

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

@tim-salabim
Copy link
Member

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.

@allthesignals
Copy link
Author

allthesignals commented Mar 14, 2018

@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 :)

@allthesignals
Copy link
Author

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!

@tim-salabim
Copy link
Member

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.

@HeHuangDortmund
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants