0

I'm trying to open ne_10m_urban_areas.shp with rgdal::readOGR, but it gives me the bellow error

library(rgdal)
a <- readOGR(dsn = path.expand("C:/.../Desktop/"),
layer = "ne_10m_urban_areas.shp")

> Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :

> Cannot open layer

a <- shapefile("C:/.../Desktop/ne_10m_urban_areas.shp")

> Error: file.exists(extension(x, ".shx")) is not TRUE

file.exists("C:/.../Desktop/ne_10m_urban_areas.shp")

> [1] TRUE

1
  • All you need is the name of the shapefile so, remove ".shp" from the name of dataset. May 16, 2018 at 21:25

3 Answers 3

4

This bit:

a <- shapefile("C:/.../Desktop/ne_10m_urban_areas.shp")
 Error: file.exists(extension(x, ".shx")) is not TRUE

tells us that there is no ne_10m_urban_areas.shx file - note the file extension SHX not SHP. A valid shapefile needs a .shp, a .shx and usually a .dbf file (as well as possible others). Do you have all those in the same folder? I don't think so. There's your problem.

0

It is also possible you don't have even read permissions over the folder you are trying to open a layer from. This problem is especially pesky if running stuff from a docker container. In order to give yourself permission just type:

chmod -R 777 /path/to/folder

in terminal.

0

Make sure your file names match exactly. I had the same issue and I had all the files in the same folder, but the names didn't match exactly. Once they matched then it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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