-1

I downloaded a SHAPE file from here. I added to my working directory:

> list.files('/home/lucho/data/EnglandGIS/', pattern='\\.shp$')
[1] "england_gor_2011.shp"
> file.exists('/home/lucho/data/EnglandGIS/england_gor_2011.shp')
[1] TRUE

However, I cannot read it:

library("rgdal")
shape <- readOGR(dsn = path.expand("/home/lucho/data/EnglandGIS/england_gor_2011"), layer = "england_gor_2011")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open file

The only other similar question with accepted answer is of not help. What is the problem? Is the data corrupted? How can I tell? (if you could download the data and try it yourself, that might be the best way)

I am using latest R with latest Rstudio, in Ubuntu 16.04.

Community
  • 1
  • 1
luchonacho
  • 6,759
  • 4
  • 35
  • 52
  • 1
    I think you need `dsn = path.expand("/home/lucho/data/EnglandGIS")`, this is just the folder containing the files, not the layer name (which goes in the `layer` argument). Alternatively, you can use "/home/lucho/data/EnglandGIS/england_gor_2011.shp" and get rid of the `layer` argument altogether – konvas May 15 '17 at 10:59

3 Answers3

2

To import shape files with readOGR you can either use

readOGR(dsn = "/home/lucho/data/EnglandGIS/", layer = "england_gor_2011")

where dsn is the folder containing england_gor_2011.shp (and other files with the same name but different extensions, e.g. england_gor_2011.dbf, etc.) or you can specify the full path to the shape file (including the extension):

readOGR("/home/lucho/data/EnglandGIS/england_gor_2011.shp")

The second method won't work for earlier versions of rgdal as far as I remember.

konvas
  • 14,126
  • 2
  • 40
  • 46
1

Don't forget to specifiy the extension of the shape file in the readOGR command:

library("rgdal")
shape <- readOGR(dsn = path.expand("england_gor_2011.shp"), 
                layer = "england_gor_2011")

#############
OGR data source with driver: ESRI Shapefile 
Source: "england_gor_2011.shp", layer: "england_gor_2011"
with 9 features
It has 3 fields

Hope this can help you.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
1

Although this question seems to be answered, here are a couple of other options on how to read in the shapefile:

you can also try the function shapefile from the rasterpackage:

library(raster)
shp <- shapefile("/home/lucho/data/EnglandGIS/england_gor_2011.shp")

or the function st_read from the new sfpackage (very efficient):

library(sf)
shp <- st_read(system.file("/home/lucho/data/EnglandGIS/england_gor_2011.shp", package="sf"))
maRtin
  • 6,336
  • 11
  • 43
  • 66