The “error in ogrlistlayers(dsn = dsn) : cannot open data source” error message occurs when you are trying to open a file with the ogrListLayers function. It behaves like a file not found message. The problem is that it seems to produce this message regardless of how you deal with the situation. Furthermore, there is no straightforward way of fixing the problem.
Description of the problem
The ogrListLayers function is a function of the rgdal package. The problem with this error is that there is extraordinarily little documentation on this function. This error message seems to come about when the function cannot load in a data layer. By default, it looks like it cannot find the file. Unfortunately, it could easily be a file-type problem. The error message does not seem to distinguish between the two. This makes evaluating and fixing the problem at best difficult and in most circumstances may be impossible. This message seems to show up regardless of the circumstances where the function cannot load in any datum.
Explanation of the problem
Here is some sample code the produces the “error in ogrlistlayers(dsn = dsn) : cannot open data source” error message.
> df1 = data.frame(z = c(“A”, “B”, “C”, “D”, “E”),
+ x = c(1, 2, 3, 4, 5),
+ y = c(2, 3, 4, 5, 6))
> df2 = data.frame(x = c(1, 2, 3, 4, 5, 6, 7, 8),
+ y = c(2, 3, 4, 5, 6, 7, 8, 9))
> df3 = data.frame(X = c(“A”, “B”, “C”, “D”, “A”, “D”),
+ Y = c(111, 211, 311, 411, 122, 444))
> save(list = c(“df1”, “df2”, “df3”), file = “test1.rda” )
> dsn = “test1.rda”
> r1_lyrlist = ogrListLayers(dsn = dsn)
Error in ogrListLayers(dsn = dsn) : Cannot open data source
As you can see this code reproduces the problem by trying to load “test1.rda” into the ogrListLayers function. It behaves like it cannot find “test1.rda”, but it can also be a problem with loading the data itself. Therefore fixing this problem is so tough, because this problem can come about from many sources.
How to fix the problem
One possible solution to this problem is that the ogrListLayers function is designed to analyze oracle spatial data. This means it should be capable of successfully loading in a kml file, but the spatial data must be exactly right including the presence of a layer name in these shapefiles. If this spatial data is not right, you will still get this message. This is why this problem is nearly impossible to fix, not only can it be caused by a variety of problems, including the wrong type of data, the data file not being in the right place, having the wrong filetype, having the right filetype but having problems with the content. What makes this problem so difficult is that most of the causes of this problem have no way of being fixed. Only if you have the right type of file, and either find the correct path or are able to access the data to correct any problems with the data itself.
This problem is extremely difficult to deal with because there are so many ways that this problem can occur most of which have no way of being fixed. While it may be possible to fix this problem under the right circumstances, they are an extremely narrow range of circumstances that are unlikely to occur. If you get this message, beyond making sure you have the right data type, and the right file location there really is nothing else that can be done.