I am trying to plot a shapefile on top of a raster using the command readOGR. My current code is:

    CEZ <- readOGR(".", "Shapefile/CEZ1")

This is followed by the error code

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

I am unsure what this code means - Is it a problem with the shapefile, the command or the way I have structured the path?

  • The first argument dsn is the path and the layer argument is the name of the shapefile without the .shp extension. You are passing a partial path argument to the layer argument where it should just be "CEZ1". If you need to get to a sub-directory in your wd you can use something like file.path(getwd(), "Shapefile") for the dsn argument. The suggestion by @Spacedman removes these complications. Jul 2, 2020 at 14:08

1 Answer 1


The raster package has a handy wrapper function for reading shapefiles. You should be able to do:

CEZ <- shapefile("./Shapefile/CEZ1.shp")

if that's where your shapefile really is. You might get a better error message.

  • Hi thanks very much for your response. I'm still getting the same error. I've realised that the shape file is a polyline rather than a polygon, do you think this could be the reason why I am getting the error code?
    – fionant97
    Jul 2, 2020 at 13:23
  • @fionant97 No this is not the problem, rgdal::readOGR and the raster::shapefile wrapper will read any valid feature geometry. Are you sure that your data is residing in the "Shapefile" subdirectory of your current working directory? You can check using list.files(file.path(getwd(), "Shapefile"), "shp$") if you data is not listed then it is not in the specified directory. Jul 2, 2020 at 14:14
  • Hi, I've moved the shapefile and its associated files to be in a folder by itself. It has now come up with the error Error in .local(x, ...) : file.exists(extension(x, ".shp")) is not TRUE from what I have found on the forum, this is usually because the other files are not in the same folder e.g. .shx and .dbf, however I have made sure mine area... really not sure what's going on!
    – fionant97
    Jul 3, 2020 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.